English 中文(简体)
EF 核心8要求在管理移民时不设DbContext Constructionor,以便限制出入
原标题:EF Core 8 requires default DbContext constructor when managing migrations so can t access Configuration

我将我的网和EF项目从5个升级到8个,并立即处理移民管理问题(我使用包裹管理员康索尔)。

此前,我有以下DbContext类:

public class PortalDbContext : DbContext {

    private readonly IConfiguration _config;

    public PortalDbContext(
        DbContextOptions<PortalDbContext> options,
        IConfiguration config) : base(options) {

        _config = config;
    }

    protected override void OnModelCreating(ModelBuilder modelBuilder) {
        //... assorted entity defs

        base.OnModelCreating(modelBuilder);
    }

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {
        if(!optionsBuilder.IsConfigured) {
            optionsBuilder.UseSqlServer(_config.GetConnectionString("PortalDBConnection"));
        }
    }   
}

在升级到8个之后,我不得不增加一个没有参数的构造,使欧洲复兴开发银行能够管理移民(补充、更新等),否则它将造成无法解决服务错误。

public PortalDbContext() {}

但是,由于这是使用定制构造者与混凝土参数的灯塔,因此,我可以在“OnConfiguring”电话中使用“GetConnection String”。

我通过直接通过“使用SqlServer”电话接通的连接而来。

if(!optionsBuilder.IsConfigured) {
    optionsBuilder.UseSqlServer("server=xxxxx, etc.");
}

But I really want to be able to have the config string defined solely in the appsettings.json file as I regularly swap to different databases for dev and testing.

在管理移民时,是否有办法使欧洲自由贸易联盟能够利用这种结构?

请注意,我正在将“Console”计划管理人用作国家扫盲委员会,并将“DbContext”项目设定为默认项目。

Also, I have the following in startup but that doesn t get used when managing migrations in the PMC console:

services.AddDbContext<PortalDbContext>(options =>                
     options.UseSqlServer(_config.GetConnectionString("PortalDBConnection")));
问题回答

当然,在我提出问题之后,我找到答案(比什卡·查普)。 需要建立一个配置,以备评估。 json in the OnConfiguring calls:

    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) {

        if(!optionsBuilder.IsConfigured) {

            var configuration = new ConfigurationBuilder()
                .SetBasePath(Directory.GetCurrentDirectory())
                .AddJsonFile("appsettings.json")
                .Build();

            optionsBuilder.UseSqlServer(configuration.GetConnectionString("PortalDBConnection"));
        }
    }




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签