Entity Framework 3.5:更改实体类的构造函数
生成的实体框架实体文件中的默认构造函数是这样的:
public ProjectEntities() : base("name=ProjectEntities", "ProjectEntities")
{
this.OnContextCreated();
}
我想将其更改为:
public ProjectEntities() : base(UtilClass.GetEnvDependantConnectionStringName(), "ProjectEntities")
{
this.OnContextCreated();
}
这是因为我希望所有开发环境和生产环境都有不同的连接字符串,并且不可能将它们混淆(这是我的自定义方法检查的内容)。
我该怎么做?每次重新生成设计器文件时都会丢弃此代码。
The default constructor in a generated Entity Framework Entities file is like this:
public ProjectEntities() : base("name=ProjectEntities", "ProjectEntities")
{
this.OnContextCreated();
}
I want to change it to:
public ProjectEntities() : base(UtilClass.GetEnvDependantConnectionStringName(), "ProjectEntities")
{
this.OnContextCreated();
}
This is because I want to have a different connection string for all the dev environments and the production environment, and have no chance they are mixed up (which is what my custom method checks).
How do I do that? This code is thrown away every time the designer file is regenerated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要在自动创建的 ProjectEntities.Designer.cs 旁边创建另一个文件,例如 ProjectEntities.cs。因为您使用部分来扩展实体类的功能,如下所示:
当您重新生成 .Designer.cs 文件时,该文件不会发生更改。您必须自己获取连接字符串......
You need to create another file alongside the auto-created ProjectEntities.Designer.cs, say ProjectEntities.cs. In that you use partial to extend the functionality of your entities class like this:
The file won't then get changed when you regenerate the .Designer.cs file. You'll have to fetch the connection string yourself...
我们通过调用我们的实体 ProjectEntitiesPrivate 来修复它,之前的
partial class ProjectEntities
现在是一个非partialclass ProjectEntities : ProjectEntitiesPrivate
,具有我需要的构造函数:We fixed it by calling our entities ProjectEntitiesPrivate, and what was
partial class ProjectEntities
before, is now a non partialclass ProjectEntities : ProjectEntitiesPrivate
, with the constructor I need: