如何在 EntityFramework Core 中使用部分类和部分 OnModelCreating 方法扩展 DbContext [英] How to extend DbContext with partial class and partial OnModelCreating method in EntityFramework Core

查看:18
本文介绍了如何在 EntityFramework Core 中使用部分类和部分 OnModelCreating 方法扩展 DbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 EF Core 和 DatabaseFirst 方法.我的 dbContext 是由 Scaffold-DbContext 命令自动创建的.

我需要将一些新的 DbSet 添加到 dbContext 中,并将一些额外的代码添加到 OnModelCreating 方法中,但是在每个脚手架之后,添加的代码都被删除了,我每次都必须再次添加.

我想要做的是创建另一个部分 dbContext 类并将 protected override void OnModelCreating(ModelBuilder modelBuilder) 方法标记为部分

但得到错误:

<块引用>

部分方法不能有访问修饰符或 virtual、abstract、override、new、sealed 或 extern 修饰符.

<块引用>

一个分部方法不能有多个实现声明

这是一个伪代码:

MyDbContext1.cs - 由 Scaffold-DbContext

生成

公共部分类 MyDbContext : DbContext{公共 MyDbContext(){}公共 MyDbContext(DbContextOptions 选项):基础(选项){}公共虚拟 DbSet客户{得到;放;}protected override partial void OnModelCreating(ModelBuilder modelBuilder){modelBuilder.Entity(entity =>{//一些代码...}}}

MyDbContext2.cs - 这段代码我每次都在脚手架后添加到 dbContext 中:

公共部分类 MyDbContext{公共虚拟 DbSet另一个实体 { 得到;放;}protected override partial void OnModelCreating(ModelBuilder modelBuilder){modelBuilder.Entity(entity =>{entity.HasKey(e => new {e.Id, e.IdAction, e.IdState}).ForSqlServerIsClustered(false);});}}

解决方案

另一种方法是创建另一个从 MyDbContext 继承的上下文类,该类实际上包含所有自定义代码.然后使用这个新类作为您的上下文.这样就不需要更新生成的代码了.

公共类 MyDbContext2 : MyDbContext{公共 MyDbContext2(){}public MyDbContext2(DbContextOptions options):基础(选项){}公共虚拟 DbSet另一个实体 { 得到;放;}protected override void OnModelCreating(ModelBuilder modelBuilder){base.OnModelCreating(modelBuilder);modelBuilder.Entity(entity =>{entity.HasKey(e => new {e.Id, e.IdAction, e.IdState}).ForSqlServerIsClustered(false);});}}

I'm using EF Core and DatabaseFirst approach. My dbContext is created automatically by Scaffold-DbContext command.

I need to add some new DbSets into a dbContext and add into OnModelCreating method some additional code but after each scaffolding that added code are erased and I have to add it each time again.

What I want to do is to create another partial dbContext class and mark protected override void OnModelCreating(ModelBuilder modelBuilder) method as partial

but get errors:

A partial method cannot have access modifiers or the virtual, abstract, override, new, sealed, or extern modifiers.

A partial method may not have multiple implementing declarations

Here is a pseudo code:

MyDbContext1.cs - generated by Scaffold-DbContext

public partial class MyDbContext : DbContext
{
    public MyDbContext()
    {
    }

    public MyDbContext(DbContextOptions<MyDbContext> options)
        : base(options)
    {
    }

    public virtual DbSet<Client> Clients { get; set; }

    protected override partial void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Client>(entity =>
        {
            // some code ...
        }
    }
}

MyDbContext2.cs - this code I added each time into dbContext after scaffolding:

public partial class MyDbContext
{
    public virtual DbSet<JustAnotherEntity> AnotherEntity { get; set; }

    protected override partial void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<JustAnotherEntity>(entity =>
        {
            entity.HasKey(e => new {e.Id, e.IdAction, e.IdState})
                .ForSqlServerIsClustered(false);
        });
    }
}

解决方案

An alternative would be creating another context class that inherit from MyDbContext that actually include all the custom code. and then use this new class as your context. This way, there is no need to update the generated code.

public class MyDbContext2 : MyDbContext 
{
    public MyDbContext2()
    {
    }

    public MyDbContext2(DbContextOptions<MyDbContext> options)
        : base(options)
    {
    }

    public virtual DbSet<JustAnotherEntity> AnotherEntity { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);
        modelBuilder.Entity<JustAnotherEntity>(entity =>
        {
            entity.HasKey(e => new {e.Id, e.IdAction, e.IdState})
                .ForSqlServerIsClustered(false);
        });
    }
}

这篇关于如何在 EntityFramework Core 中使用部分类和部分 OnModelCreating 方法扩展 DbContext的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆