如何扩展 Entity Framework 6.1.3 生成的类?

How to extend an Entity Framework 6.1.3 generated class?(如何扩展 Entity Framework 6.1.3 生成的类?)
本文介绍了如何扩展 Entity Framework 6.1.3 生成的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以扩展 Entity Framework 6.1.3 生成的类?

Is it possible to extend an Entity Framework 6.1.3 generated class?

我有一个现有的数据库,我在其中创建了一个 ADO.NET 实体数据模型,而 Visual Studio 2015 又生成了一组类.

I have an existing database to which I have created an ADO.NET Entity Data Model, which in turn Visual Studio 2015 has generated a set of classes.

public partial class WebApplication1Entities : DbContext
{
    public WebApplication1Entities()
        : base("name=WebApplication1Entities")
    {
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }
}

我可以手动覆盖 WebApplication1Entities 以允许动态运行时连接:

I can manually override the WebApplication1Entities to allow a dynamic runtime connection as so:

    public WebApplication1Entities(string connectionString) : base(connectionString)
    {
    }

然而,这涉及编辑 Visual Studio 2015 生成的类,但如果我希望将来更新 ADO.NET 实体数据模型,Visual Studio 将覆盖我对之前生成的类所做的任何手动更改,并且我回到第一方,不得不手动编辑生成的类.

This however, involves editing the class that Visual Studio 2015 has generated, but should I wish to update the ADO.NET Entity Data Model in the future, Visual Studio will overwrite any manual changes I have made to the previous generated class and I'm back to square one, having to manually edit the generated class.

是否可以创建一个帮助类或类似的东西来扩展现有的 WebApplication1Entities : DbContext,并允许添加新的重载方法并继承 Visual Studio 2015 生成的现有方法类,例如虚拟 DbSets.

Is it possible to create a helper class or something similar to extend the existing WebApplication1Entities : DbContext, and allow the addition of the new overloaded method and also inherit the existing methods of the Visual Studio 2015 generated class such as virtual DbSets.

任何帮助将不胜感激:-)

Any help would be much appreciated :-)

推荐答案

正如你在声明中看到的

public partial class WebApplication1Entities : DbContext

这个类是partial.

因此您可以创建另一个 *.cs 文件并将您的代码放在那里(使用相同的命名空间!):

So you can create another *.cs file and put your code there (use the same namespace!):

public partial class WebApplication1Entities
{
     public WebApplication1Entities(string connectionString) : base(connectionString)
     {
     }        
}

因此,当设计器覆盖包含原始"类的文件时,您的代码将保持不变.

So when the designer overwrites the file containing the "original" class, your code stays untouched.

更多关于partial类和方法.

这篇关于如何扩展 Entity Framework 6.1.3 生成的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)