Windows窗体应用程序中的连接字符串EF 4.1代码优先SQL紧凑
我创建了一个 Windows 窗体应用程序:
- 具有多个 Windows 窗体的演示库
- 具有数据层的类库
- 访问数据库的类库
我正在使用带有 Code First 方法的 EntityFramework 4.1 和 SQL Compact 4.0 数据库。
我在类库项目的 app.config 文件中创建了一个连接字符串,用于连接数据库。问题是连接字符串显然对数据库创建没有影响。我的意思是程序一切正常,但即使我指定数据库的位置也没有任何效果!
我写的 app.config 正确吗? 我是否需要以特定方式初始化 DbContext 类? (今天我没有在构造函数中传递任何连接字符串)
DbContext 类:
public class MyDB : DbContext
{
public DbSet<ContactPerson> ContactPersons { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Project> Projects { get; set; }
public DbSet<Quotation> Quotations { get; set; }
public MyDB()
: base("MyDatabase")
{
}
}
App.config 连接字符串:
<add name="MyDatabase" connectionString="Data Source=MyDB.sdf"
providerName="System.Data.SqlServerCE.4.0">
I have created a windows form application:
- A presentation library with several windows forms
- A class library with a data layer
- A class library to access a database
I'm using EntityFramework 4.1 with Code First Approach and SQL Compact 4.0 database.
I created a connection string in the app.config file in the class library project used to connect to the database. The problem is that the connection string has apparently no influence on the database creation. I mean that everything is working fine with the program but even if I specify a location for the database this does not have any effect!
Am I writing in the right app.config?
Do I need to initialize my DbContext class in a specific way? (today I do not pass any connection string in the constructor)
DbContext class:
public class MyDB : DbContext
{
public DbSet<ContactPerson> ContactPersons { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<Project> Projects { get; set; }
public DbSet<Quotation> Quotations { get; set; }
public MyDB()
: base("MyDatabase")
{
}
}
App.config connection string:
<add name="MyDatabase" connectionString="Data Source=MyDB.sdf"
providerName="System.Data.SqlServerCE.4.0">
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您需要将 app.config 放入应用程序 (.exe) 项目中。 app.config 文件应类似于此博客文章(区分大小写):http://erikej.blogspot.com/2011/04/ saving-images-to-sql-server-compact.html 名称应该是 MyDB,而不是 MyDatabase ...
You need to put the app.config in the application (.exe) project. The app.config file should look like in this blog post (case sensitive): http://erikej.blogspot.com/2011/04/saving-images-to-sql-server-compact.html and the names should be MyDB, not MyDatabase...
您的连接字符串错误,应该是这样的:
add name="MyDB" connectionString="Data Source=MyDB.sdf"providerName="System.Data.SqlServerCE.4.0"
请注意,名称必须与上下文类的名称匹配让它自动检测连接。
Your connection string is wrong, it should be this:
add name="MyDB" connectionString="Data Source=MyDB.sdf" providerName="System.Data.SqlServerCE.4.0"
Note that the Name must match the name of your context class for it to auto detect the connection.
您是否尝试过为数据库播种?您必须注入一些数据,以便 EF 创建数据库,如果您不这样做,模型定义就像一个意图声明。
然后查看您的调试/发布文件夹并检查数据库是否已正确创建。
Microsoft 有一篇文章更详细地解释了所有这些过程,他们有一个新教程取代了“Magic Unicorn”教程,请参阅 http://msdn.microsoft.com/en-US/data/jj193542
Have you tried seeding the DB? You have to inject some data so EF creates the DB, if you don't the model definition is just like a statement of intention.
Then look in your Debug/Release folder and check if the DB is created properly.
There is an article by Microsoft explaining all this process in more detail, they have a neew tutorial that replaces the "Magic Unicorn" tutorial, see http://msdn.microsoft.com/en-US/data/jj193542