Dr.Guo
发布于 2024-01-10 / 22 阅读
0
0

20210723—Entity Framework配置

应用的Nuget库

<PackageReference Include="Microsoft.EntityFrameworkCore" Version="5.0.8" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="5.0.8">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="5.0.8">
      <PrivateAssets>all</PrivateAssets>
      <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
    </PackageReference>
    <PackageReference Include="OpenCvSharp4.Windows" Version="4.5.2.20210404" />
    <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="5.0.1" />

Dao:

using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;

namespace GearSmartScanner.Controller.Database.Dao
{
    public class Gear
    {
        [Key]
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        public int Z { get; set; }
        public int Mn { get; set; } = 2;
        public double Alpha { get; set; } = 20;
        public double Beta { get; set; } = 10;
        public double Ha { get; set; } = 1.0;
        public double Hf { get; set; } = 0.25;
    }
}

DatabaseContext

usingSystem;
usingSystem.Collections.Generic;
usingGearSmartScanner.Controller.Database.Dao;
usingMicrosoft.EntityFrameworkCore;

namespaceGearSmartScanner.Controller.Database
{
public classDatabaseContext : DbContext
    {
private readonlyIApplicationSettingconfig;

/// <inheritdoc />
publicDatabaseContext(IApplicationSettingconfig)
        {
this.config = config;
        }

/// <inheritdoc />
protectedDatabaseContext()
        {
        }

publicDbSet<Dao.Gear> Gears { get; set; }
publicDbSet<SingleMeasureRecord> SingleResults { get; set; }

/// <inheritdoc />
protected override voidOnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseMySql(this.config.MysqlConnectString,newMySqlServerVersion(newVersion(5, 7, 32))).EnableSensitiveDataLogging().EnableDetailedErrors();
// optionsBuilder.UseLazyLoadingProxies();
base.OnConfiguring(optionsBuilder);
        }

/// <inheritdoc />
protected override voidOnModelCreating(ModelBuilder builder)
        {
base.OnModelCreating(builder);
            builder.Entity<Dao.Gear>()
                   .HasData(newList<Dao.Gear>
                   {
new()
                       {
                           Id = 1,
                           Name = "G1",
                           Z = 20
                       },
new()
                       {
                           Id = 2,
                           Name = "G2",
                           Z = 20
                       }
                   });
// builder.Entity<SingleMeasureRecord>()
            //        .HasData(new List<SingleMeasureRecord>()
            //        {
            //            new()
            //            {
            //                Id = 1,
            //                GearId = 1,
            //                DateTime = DateTime.Now,
            //                Degree = 10.0,
            //                ImageFileName = "aaa",
            //                Note = "",
            //                IntensityFileName = "aaa",
            //                ProfileFileName = "aaa",
            //                ProfilePlotFileName = "aaa",
            //                TeethId = 2
            //            }
            //        });
}
    }
}

Autofac配置

builder.RegisterType<DatabaseContext>();
DatabaseContext databaseContext = Scope.Resolve<DatabaseContext>();
databaseContext.Database.EnsureCreated();

链接字符串

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <appSettings>
        <add key="GocatorIp" value="192.168.8.118" />
        <add key="HuaweiAk" value="V5JnaGMcAqCsmTpjgNxQLpdFEEViQ/6DZGuEDWEF+IbDrU9l95X0jkpEciqteMV36TdxkFfjpshjOmO75wTRZg==" />
        <add key="HuaweiSk" value="mON1sqBq3cVX/VfDwJUepoW+qMaBC44C6Hn/LY9bgyjuCNvFq3x+kYpKY9hsY4bMoIn5FXh99i1SyPXP7pRTIWksMhzwhlcZVHR5qgjhvCE=" />
        <add key="HuaweiObsEndpoint" value="obs.cn-north-1.myhuaweicloud.com" />
        <add key="HuaweiObsBucketName" value="gear-linear-scanner" />
        <add key="DataFolder" value="D:\GearScanner\" />
        <add key="SingleDataFolder" value="Single\" />
        <add key="ProfileFolder" value="Profile\" />
        <add key="IntensityFolder" value="Intensity\" />
        <add key="ImageFolder" value="Image\" />
        
        
    </appSettings>
    <connectionStrings>
        <add name="MysqlConnectString" connectionString="server=117.78.27.64;port=6612;user=gs;password=ejNHjR7BURf8sx_;database=gear_scanner"/>
    </connectionStrings>
</configuration>

评论