LINQ to SQL DataContext 和连接问题

发布于 2024-08-20 13:26:18 字数 695 浏览 4 评论 0原文

我的网站使用 LINQ to SQL 来访问两个数据库。第一个数据库是网站(我将其称为 WEBSITE)数据,第二个数据库是交易历史记录(我将其称为 WEBSITE_HISOTRY)。不久前,当我将 WEBSITE_HISOTRY 中的表添加到我的数据上下文时,我收到了某种警报,我单击了“确定”(可能不是最好的主意)。我确实记得 Visual Studio 抱怨数据库的连接与我的配置或类似的内容不同。一切工作正常,直到我发布到我的服务器。我不断收到数据库未找到错误,当我记录连接字符串时,我发现了这一点。

WEBSITE_HISTORY

数据源=MYCOMPUTER\SQLEXPRESS;初始目录=WebSiteHistory;集成安全性=TRUE

WEBSITE

数据源=my.dyndns.net;网络库=DBMSSOCN;初始目录=网站;用户ID=网站用户;密码=********;

我还发现 WEBSITE_HISTORY 数据上下文的构造函数需要连接字符串(与具有无参数构造函数的 WEBSITE 不同)。我更改了构造函数,但每次将表添加到数据上下文时,它都会变回来。我读过另一个关于将 DataContext 连接属性设置为 Application = true 的问题。我已经尝试过此操作,但无法将“设置属性名称”设置为正确的连接。

我不确定我做了什么煽动了这种行为。任何帮助将不胜感激。

I am using LINQ to SQL for my website to access two databases. The first database is the website (which i'll call WEBSITE) data, the second is a history of transactions (which i'll call WEBSITE_HISOTRY). When I added a table from the WEBSITE_HISOTRY to my datacontext not so long ago i recieved some sort of alert that I clicked OK on (probably not the best idea). I do recall it was something about Visual Studio complaining that the connection for the database differed from my config or something along those lines. Everything worked fine until I published to my server. I kept getting a database not found error and when I logged the connection strings I found this.

WEBSITE_HISTORY

Data Source=MYCOMPUTER\SQLEXPRESS;Initial Catalog=WebSiteHistory;Integrated Security=TRUE

WEBSITE

Data Source=my.dyndns.net; Network Library=DBMSSOCN; Initial Catalog=Website; User ID=WebsiteUser;Password=*******;

I also found that the constructor for the WEBSITE_HISTORY datacontext required a connection string (unlike the WEBSITE which has a parameterless constructor). I altered the constructor but everytime a I add a table to the datacontext, it changes back. I had read in another question about setting the DataContext connection properties to Application = true. I have tried this but I cannot set the "Settings Property Name" to the correct connection.

Im not sure what I did do incite this behavior. Any help would be greatly appreciated.

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

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

发布评论

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

评论(3

轻许诺言 2024-08-27 13:26:18

数据上下文应该有多个可用的重载构造函数;其中之一要求提供连接字符串。我建议一直使用这个;原因是这允许您更改为指向具有相同结构的新数据库,并且它将以这种方式工作。我倾向于创建一个静态类来创建我的数据上下文类,以使这更容易:

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

The data context should have multiple overloaded constructors available to you; one of them asks for a connection string. I would recommend using this one all the time; the reason is this allows you to change to point to a new database that has the same structure, and it will work that way. I tend to create a static class to create my data context classes to make this easier:

public static class ContextCreator
{
   public static CustomDataContext Create()
   {
     return new CustomDataContext(ConfigurationManager.ConnectionStrings["CName"].ConnectionString);
   }
}
揽月 2024-08-27 13:26:18

我放弃了,不再和设计师抗争了。我现在按照 Brian 的建议传递连接字符串(即 DataAccess.Properties.Settings.Default.WEBSITEHISTORY_ConnectionString)并且它有效。当我检查属性文件时,我发现 WEBSITE_HISTORY 属性丢失了,

[global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]

我还没有调查它,但我打赌这会导致它使用我的 DataAccess 属性中的默认连接,而不是 Web 配置。

根据布莱恩的评论,我认为他所描述的场景就是导致这种情况的原因。

感谢您的所有帮助!

I gave up and stopped fighting with the designer. I am now passing the connection string as Brian had suggested (i.e. DataAccess.Properties.Settings.Default.WEBSITEHISTORY_ConnectionString) and it works. When i examined the Properties file i found that the WEBSITE_HISTORY property was missing this

[global::System.Configuration.SpecialSettingAttribute(global::System.Configuration.SpecialSetting.ConnectionString)]

I havent looked into it yet but I am betting that this was causing it to use the default connection in my DataAccess properties and not the web config.

Per Brian's comments, the scenario that he described is what i believe to have caused this to begin with.

Thank you for all of the help!

奢望 2024-08-27 13:26:18

你所做的不会起作用。数据库上下文是到单个数据库的连接。您不应尝试将其他数据库中的表添加到特定上下文。事实上,这可能根本行不通。如果您发现自己正在处理多个数据库,则将需要有多个数据库上下文对象,每个数据库一个。

顺便说一句,我想如果您接受更多对您问题的回答作为答案,您会发现您会得到更多问题的答案。你的录取率只有25%左右。这可能会阻止人们回复您。

What you did is not going to work. A database context is a connection to a single database. You should not attempt to add tables from other databases to a particular context. In fact, this probably simply won't work. If you find yourself dealing with multiple databases, you are going to need to have multiple database context objects, one for each database.

BTW, I think you'll find you will get more answers to your questions, if you accept more of the responses to your questions as answers. Your acceptance rate is only about 25%. This might discourage people from responding to you.

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