[英]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.