无法首先使用 Entity Framework 4.1 代码更改连接字符串
我在更改实体框架代码首先为我的项目使用的连接字符串时遇到问题。我创建了一个 MVC3 项目,然后添加了两个项目:DomainClasses 和 DataAccess。
DomainClasses 项目有一个名为 Classes.cs 的类文件,其中包含一个类:
public class User
{
public int ID { get; set; }
public string Username { get; set; }
public string EmailAddress { get; set; }
public string Password { get; set; }
public int StatusID { get; set; }
public DateTime DateCreated { get; set; }
}
DataAccess 项目有一个名为 PLNQDB.cs 的类文件,其中包含一个类:
public class PLNQDB : DbContext
{
public DbSet<User> Users { get; set; }
}
我在我的 MVC 项目中引用了这些项目中的每一个,并且一切都构建良好并运行,甚至创建我能够保存和检索数据的数据库文件。
运行项目时,我可以在“自动”窗口中看到 this.db.base.Database.Connection.base.ConnectionString =
"Data Source=.\\SQLEXPRESS;Initial Catalog=PLNQ.DataAccess.PLNQDB;Integrated Security=True;MultipleActiveResultSets=True"
我的问题是如何覆盖此自动生成的连接字符串以使用我的远程服务器。我已将连接字符串添加到 MVC 项目的 web.config 文件中。
<connectionStrings>
<add name="ApplicationServices"
connectionString="Data Source=255.255.255.255;Initial Catalog=PLNQ;User ID=blah;Password=blah"
providerName="System.Data.SqlClient" />
</connectionStrings>
但每次我运行该项目时,它仍然使用自动生成的连接字符串。我还尝试在 DataAccess 和 DomainClasses 项目中添加具有相同连接字符串的 App.config 文件。
我缺少什么?
I am having trouble changing the connection string used by entity framework code first for my project. I created a MVC3 project and then added two projects, DomainClasses and DataAccess.
The DomainClasses project has one class file named Classes.cs with one class:
public class User
{
public int ID { get; set; }
public string Username { get; set; }
public string EmailAddress { get; set; }
public string Password { get; set; }
public int StatusID { get; set; }
public DateTime DateCreated { get; set; }
}
The DataAccess project has one class file named PLNQDB.cs with one class:
public class PLNQDB : DbContext
{
public DbSet<User> Users { get; set; }
}
I have a reference to each of these projects inmy MVC project and everything builds fine and runs and even creates the database file that I am able to save to and retrieve data from.
When running the project I can see in the Autos window the connection string being used under this.db.base.Database.Connection.base.ConnectionString =
"Data Source=.\\SQLEXPRESS;Initial Catalog=PLNQ.DataAccess.PLNQDB;Integrated Security=True;MultipleActiveResultSets=True"
My question is how do I override this autogenerated connectionstring to use my remote server. I have added a connection string to the web.config file of the MVC project.
<connectionStrings>
<add name="ApplicationServices"
connectionString="Data Source=255.255.255.255;Initial Catalog=PLNQ;User ID=blah;Password=blah"
providerName="System.Data.SqlClient" />
</connectionStrings>
but everytime I run the project it still uses the auto generated connectionstring. I have also tried adding App.config files with the same connection string in both the DataAccess and DomainClasses projects.
What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
有几个选项
1 您需要命名与您的
DbContext
类名称相匹配的连接字符串。例如
2 在您的
DbContext
构造函数中使用连接字符串名称调用基类构造函数There are couple of options
1 You need to name the connection string that matches the your
DbContext
class name.eg
2 In your
DbContext
constructor call the base class constructor with the connection string name