Entity Framework 3.5:更改实体类的构造函数

发布于 2024-12-17 15:21:33 字数 441 浏览 0 评论 0原文

生成的实体框架实体文件中的默认构造函数是这样的:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

云之铃。 2024-12-24 15:21:33

您需要在自动创建的 ProjectEntities.Designer.cs 旁边创建另一个文件,例如 ProjectEntities.cs。因为您使用部分来扩展实体类的功能,如下所示:

public partial class ProjectEntities : ObjectContext
{
  partial void OnContextCreated()
  {
    this.Connection.ConnectionString = UtilClass.GetEnvDependantConnectionString();
  }
}

当您重新生成 .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:

public partial class ProjectEntities : ObjectContext
{
  partial void OnContextCreated()
  {
    this.Connection.ConnectionString = UtilClass.GetEnvDependantConnectionString();
  }
}

The file won't then get changed when you regenerate the .Designer.cs file. You'll have to fetch the connection string yourself...

阿楠 2024-12-24 15:21:33

我们通过调用我们的实体 ProjectEntitiesPrivate 来修复它,之前的 partial class ProjectEntities 现在是一个非partial class ProjectEntities : ProjectEntitiesPrivate,具有我需要的构造函数:

public class ProjectEntities: ProjectEntitiesPrivate
{
    public ProjectEntities():base(UtilClass.GetEnvDependantConnectionStringName())
    {

    }

....

We fixed it by calling our entities ProjectEntitiesPrivate, and what was partial class ProjectEntities before, is now a non partial class ProjectEntities : ProjectEntitiesPrivate, with the constructor I need:

public class ProjectEntities: ProjectEntitiesPrivate
{
    public ProjectEntities():base(UtilClass.GetEnvDependantConnectionStringName())
    {

    }

....
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文