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

编程入门 行业动态 更新时间:2024-10-28 04:16:47
本文介绍了如何在EntityFramework Core中使用部分类和部分OnModelCreating方法扩展DbContext的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

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

我需要在dbContext中添加一些新的DbSet并添加到 OnModelCreating 方法提供了一些其他代码,但是在删除每个添加代码的脚手架之后,我不得不再次添加它。

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.

我想要的这样做是创建另一个局部dbContext类,并将受保护的重写void OnModelCreating(ModelBuilder modelBuilder)方法标记为局部

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

,但会出现错误:

部分方法不能具有访问修饰符或虚拟,抽象,重写,新的,密封的或外部的修饰符。

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

这是一个伪代码:

MyDbContext1.cs -由 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 -我每次在脚手架上添加到dbContext后的这段代码:

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); }); } }

推荐答案

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

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

发布评论

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

>www.elefans.com

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