为什么 LINQ 突然开始对数据库连接表现得很奇怪

发布于 2024-08-19 23:30:12 字数 440 浏览 4 评论 0原文

这是一个奇怪的现象。突然,当我在 Visual Studio 中创建 LINQ to SQL 项目时,它不会在设计器中创建以下行,大约三周前,它曾经创建过这一行,现在我必须手动执行此操作。然后,如果我在 dbml 文件中进行更改,它会将其删除,并且我必须再次手动添加。巨大的痛苦。这是我所说的几行:

   public DataContext():
                    base(global::System.Configuration.ConfigurationManager.ConnectionString["SiteSqlServer"].ConnectionString, mappingSource)  
        {  
            OnCreated();  
        }  

我正在使用 Visual Studio 2008 sp1。

This is an odd one. All the sudden when I create LINQ to SQL projects in Visual Studio would it not create the following line in the designer, up to three weeks ago or so, it used to create this line, now I have to manually do it. Then if I make changes in the dbml file it removes it and I have to manuallhy add again. huge pain. Here is the lines I am speaking of:

   public DataContext():
                    base(global::System.Configuration.ConfigurationManager.ConnectionString["SiteSqlServer"].ConnectionString, mappingSource)  
        {  
            OnCreated();  
        }  

I am using Visual Studio 2008 sp1.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

暮凉 2024-08-26 23:30:13

这是一个已知问题。

您必须为数据库上下文实现某种工厂。否则你会一直和设计者纠缠,永远不知道你使用的是哪种连接。

public partial class CustomDataContext {
    public static CustomDataContext Create()
    {
         return new CustomDataContext(ConfigurationManager.ConnectionStrings["CustomConnectionString"].ConnectionString);
    }
}

另一种选择是从 CustomDataContext 继承,并创建一个默认构造函数:

public CustomDataContext : InternalCustomDataContext /* created in designer */
{
 protected string GetCustomConnectionString() {
   return ConfigurationManager.ConnectionStrings["CustomConnectionString"].ConnectionString;
 }
 public CustomDataContext() : base(GetCustomConnectionString())
 {}
}

It's a known problem.

You have to implement sort of factory for DB context. Otherwise you will always stuggle with the designer and never know which connection you are using.

public partial class CustomDataContext {
    public static CustomDataContext Create()
    {
         return new CustomDataContext(ConfigurationManager.ConnectionStrings["CustomConnectionString"].ConnectionString);
    }
}

Another option is inheriting from your CustomDataContext, and creating a default constructor:

public CustomDataContext : InternalCustomDataContext /* created in designer */
{
 protected string GetCustomConnectionString() {
   return ConfigurationManager.ConnectionStrings["CustomConnectionString"].ConnectionString;
 }
 public CustomDataContext() : base(GetCustomConnectionString())
 {}
}
夜光 2024-08-26 23:30:13

查看 linq2sql 设计器中的连接属性,尝试不同的设置,直到找到在上下文中创建初始化连接的设置。

我记得在一个项目中收到过它,据我记得它与您建立/配置连接的方式有关。我当时修复了它,但我不记得确切的属性/地点。

Look at the connection properties in the linq2sql designer, try the different settings until you find the one that creates that initializes the connection in the context.

I recall receiving that in a project, and as far as I remember its related to the way you make/configure the connection. I fixed it at the time, but I don't recall the exact property/place.

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