C# .NET核心实体框架数据库首先生成要从基类继承的模型

C# .NET核心实体框架数据库首先生成要从基类继承的模型,c#,entity-framework,inheritance,entity-framework-6,core,C#,Entity Framework,Inheritance,Entity Framework 6,Core,我成功地做到了这一点,并更新了下面的内容来展示我是如何做到的。如果有更优雅的解决方案,我感兴趣:) 前言:我正试图从实体框架6转移到实体框架核心。我有一个现有的数据库。我正在做第一代数据库。为此,我在控制台中使用“scaffold dbContext”命令生成类。好的,那很好。但是,我希望所有生成的类都从一个简单的基类继承。这是整个基类的要点: public class MyEntityBase { public MyEntityBase() { } } 因此,如果数据

我成功地做到了这一点,并更新了下面的内容来展示我是如何做到的。如果有更优雅的解决方案,我感兴趣:)

前言:我正试图从实体框架6转移到实体框架核心。我有一个现有的数据库。我正在做第一代数据库。为此,我在控制台中使用“scaffold dbContext”命令生成类。好的,那很好。但是,我希望所有生成的类都从一个简单的基类继承。这是整个基类的要点:

public class MyEntityBase
{
    public MyEntityBase()
    {
    }
}
因此,如果数据库中有一个名为customer的表,我希望生成的类从以下开始:

public partial class Customer : MyEntityBase
我可以在EF6中通过在适当的位置更改T4文件来实现这一点。我通过以下方法在EF Core中实现了这一点

先多了解一些信息。在我的启动方案中,我有两个项目。一个是“Web”项目,创建为ASP.NET核心Web应用程序。另一个是作为.NET核心类库创建的“核心”项目。这是上下文文件和实体类的所在地。它也是MyEntityBase生活的地方

好的,我在Web项目中添加了一个名为MyDesignTimeServices.cs的类文件。该文件的内容如下:

using System;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Scaffolding.Internal;
using Microsoft.EntityFrameworkCore.Scaffolding.Configuration.Internal;

namespace MySolution.Web
{
public class MyDesignTimeServices : IDesignTimeServices
{
    public void ConfigureDesignTimeServices(IServiceCollection serviceCollection)
    {
        //serviceCollection.AddSingleton<EntityTypeWriter, MyEntityTypeWriter>();
        serviceCollection.AddSingleton<DbContextWriter, MyDbContextWriter>();
    }
}

public class MyDbContextWriter : DbContextWriter
{
    public MyDbContextWriter(ScaffoldingUtilities scaffoldingUtilities, CSharpUtilities cSharpUtilities) : base(scaffoldingUtilities, cSharpUtilities) { }

    public override string WriteCode(ModelConfiguration modelConfiguration)
    {
        var code = base.WriteCode(modelConfiguration);
        code = code.Replace("});", "\tentity.HasBaseType<" + typeof(MySolution.Core.MyEntityBase).Name + ">();" + Environment.NewLine + "\t\t\t});");
        return code;
    }
}

public class MyEntityTypeWriter : EntityTypeWriter
{
    public MyEntityTypeWriter(CSharpUtilities cSharpUtilities) : base(cSharpUtilities) { }

    public override string WriteCode(EntityConfiguration entityConfiguration)
    {
        // Get the default code of the entity class.
        var code = base.WriteCode(entityConfiguration);

        // We need to find the spot after the name of the class so we can add its inheritance.
        var classDeclaration = "public partial class ";
        var classDeclarationIndex = code.IndexOf(classDeclaration);
        if (classDeclarationIndex != -1)
        {
            // Remove anything in front of the class name, so that we can look for the first carriage return.
            var startsWithClassName = code.Substring(classDeclarationIndex + classDeclaration.Length);
            var afterClassNameIndex = startsWithClassName.IndexOf((char)13);
            if (afterClassNameIndex != -1)
            {
                // This is the spot where we want to insert our inheritance declaration.
                var insertIndex = classDeclarationIndex + classDeclaration.Length + afterClassNameIndex;

                // Insert our inheritance declaration.
                code = code.Substring(0, insertIndex) + " : " + typeof(MyEntityBase.Core.MyEntityBase).Name + code.Substring(insertIndex);
            }
        }

        return code;
    }
}
}
使用系统;
使用Microsoft.Extensions.DependencyInjection;
使用Microsoft.EntityFrameworkCore.Infrastructure;
使用Microsoft.EntityFrameworkCore.Scaffolding.Internal;
使用Microsoft.EntityFrameworkCore.Scaffolding.Configuration.Internal;
名称空间MySolution.Web
{
公共类MyDesignTimeServices:IDesignTimeServices
{
public void配置DesignTimeServices(IServiceCollection服务集合)
{
//serviceCollection.AddSingleton();
serviceCollection.AddSingleton();
}
}
公共类MyDbContextWriter:DbContextWriter
{
公共MyDbContextWriter(scaffoldingulities scaffoldingulities,CSharpUtilities CSharpUtilities):base(scaffoldingulities,CSharpUtilities){}
公共重写字符串写代码(模型配置模型配置)
{
var代码=base.WriteCode(modelConfiguration);
code=code.Replace(“}”),“\tentity.HasBaseType();”+Environment.NewLine+“\t\t}”);”;
返回码;
}
}
公共类MyEntityTypeWriter:EntityTypeWriter
{
公用MyEntityTypeWriter(CSharpUtilities CSharpUtilities):基本(CSharpUtilities){}
公共重写字符串写代码(EntityConfiguration EntityConfiguration)
{
//获取实体类的默认代码。
var代码=base.WriteCode(entityConfiguration);
//我们需要在类的名称后找到spot,以便添加其继承。
var classDeclaration=“公共部分类”;
var classDeclarationIndex=code.IndexOf(classDeclaration);
如果(classDeclarationIndex!=-1)
{
//删除类名前面的任何内容,以便我们可以查找第一个回车符。
var startsWithClassName=code.Substring(classDeclarationIndex+classDeclaration.Length);
var afterClassNameIndex=startsWithClassName.IndexOf((char)13);
如果(afterClassNameIndex!=-1)
{
//这是我们要插入继承声明的地方。
var insertIndex=classDeclarationIndex+classDeclaration.Length+afterClassNameIndex;
//插入我们的继承声明。
code=code.Substring(0,insertIndex)+“:”+typeof(MyEntityBase.Core.MyEntityBase).Name+code.Substring(insertIndex);
}
}
返回码;
}
}
}

构建解决方案。然后,当我在package manager控制台中运行Scaffold DbContext命令时,我确保将默认项目下拉选项设置为核心项目。

您看了这个问题吗?谢谢,这帮我指明了正确的方向。我能够完成我所需要的,我已经更新了我的问题来展示我是如何做到的。这不是用core 2.0编译的,我是缺少了一些包含,还是它们又改变了一切?我不知道关于2.0,我能告诉你的是,我在core 1.1In 2.0中做了上述操作,类型已更改DBContextWriter现在是CSharpDbContextGenerator EntityTypeWriter现在是CSharpEntityTypeGenerator文档还声明它可能会更改,这些不是稳定的API