简体   繁体   English

ASP.NET MVC 5 中自动生成的 EDMX class 的构造函数

[英]Constructors for Auto Generated EDMX class in ASP.NET MVC 5

I have an auto generated context class in my ASP.NET MVC 5 project.我的 ASP.NET MVC 5 项目中有一个自动生成的上下文 class。 I want some operation to be done on object creation of this class.我想对这个 class 的 object 创建做一些操作。 Instead of editing the constructor in this auto generated class(Which I heard is not recommended), Is there some other way to do this?而不是在这个自动生成的类中编辑构造函数(我听说不推荐),还有其他方法可以做到这一点吗?

When I use,当我使用时,

  var cntx = new DataBaseEntities();

I want some other operation to be taken care of.我想处理其他一些操作。 but This DatabaseEntities is an auto generated class.但是这个 DatabaseEntities 是一个自动生成的 class。 Is there any way to do this?有没有办法做到这一点?

The best I can suggest is a factory method:我能建议的最好的方法是工厂方法:

private DataBaseEntities(string contextName) : base(contextName) { }

public static DataBaseEntities Create() {
    return new DataBaseEntities(ContextName);
}

and use DataBaseEntities.Create() rather than new DataBaseEntities()并使用 DataBaseEntities.Create() 而不是 new DataBaseEntities()

var cntx = new DataBaseEntities.Create();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在MVC自动生成的类中注释asp.net属性 - Annotate asp.net properties in MVC auto generated class 如何在asp.net MVC中自动生成的部分类中的View的iputs中设置属性的值 - How to set value of a property from iputs of View in auto generated partial class in asp.net MVC 将DataAnnotations添加到自动生成的DBML类? MVC 2.0 ASP.NET - Adding DataAnnotations to auto-generated DBML Class? MVC 2.0 ASP.NET 通过ASP.NET MVC中的参数化构造函数访问其他类的属性 - Accessing other class's properties through paremeterized constructors in ASP.NET MVC ASP.net MVC 5实体框架自动生成的代码。 如何更改为更新用户信息? - ASP.net MVC 5 Entity framework Auto generated code. How to change to Update user information? 如何在自动生成的视图中使ASP.NET MVC 3呈现具有复杂类型的对象? - How do I make ASP.NET MVC 3 render objects with complex types in the auto-generated views? 如何保持属性属性插入到ASP.NET MVC中自动生成的文件中 - How to keep properties attributes inserted into auto-generated files in ASP.NET MVC 将用户定向到 ASP.Net MVC 应用程序中的另一个页面会导致自动生成的代码出现语法错误 - Directing user to another page in ASP.Net MVC application leads to syntax error in auto-generated code ASP.Net MVC auto slugify Routes - ASP.Net MVC auto slugify Routes ASP.NET MVC中的自动递增ID - Auto Increment ID in ASP.NET MVC
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM