如何在 EntityFramework Core 中使用部分类和部分 OnModelCreating 方法扩展 DbContext

编程入门 行业动态 更新时间:2024-10-28 06:25:45
本文介绍了如何在 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

本文发布于:2023-11-08 16:33:31,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1569896.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   如何在   EntityFramework   Core   OnModelCreating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!