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

查看:448
本文介绍了如何在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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