我正在使用 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
发布评论