WTM icon indicating copy to clipboard operation
WTM copied to clipboard

我建议EF的数据库迁移, 还是统一弄成跟配置文件一致. 已经附上代码

Open strugglesharp opened this issue 2 years ago • 2 comments

在代码里的DataContextFactory写DBConnStr, 万一那天改了配置文件, 代码里忘了改了,那不是出大问题了? 我建议让DataContextFactory直接读配置文件. 当前只测试了sqlserver,其他没测. 需要的朋友自己加就行了.

public class DataContextFactory : IDesignTimeDbContextFactory<DataContext>
{
    public DataContext CreateDbContext(string[] args)
    {
        var cfgJson = new ConfigurationBuilder().AddJsonFile("appsettings.json").Build();

        var dbconnstr = cfgJson.GetSection("Connections:0:Value").Value;
        var dbtype = cfgJson.GetSection("Connections:0:DBType").Value;
        var db2 = cfgJson.GetSection("Connections:1:Value").Value;
  
        Debug.Assert(db2 == null, "当前EF database迁移仅支持一个数据库");
        Debug.Assert(dbconnstr != null, "appsettings.json 必须已经设定好Connections节的Value");
        Debug.Assert(dbtype != null, "appsettings.json 必须已经设定好Connections节的dbtype");
        Debug.Assert(dbtype == "SqlServer", "当前EF database仅支持sqlserver");
         
        return new DataContext(dbconnstr, DBTypeEnum.SqlServer);
    }
}

strugglesharp avatar Mar 15 '22 12:03 strugglesharp

这个代码是WTM生成器弄出来的, 生成器代码没公开, 我也没法弄PR . 作者自己看吧. 有必要就自己弄上去.

strugglesharp avatar Mar 15 '22 12:03 strugglesharp

你完全可以自己去读取配置文件的嘛...

ArchitectAllenGitHub avatar Jul 07 '23 01:07 ArchitectAllenGitHub