Windows窗体应用程序中的连接字符串EF 4.1代码优先SQL紧凑

发布于 2024-11-24 16:55:11 字数 942 浏览 2 评论 0原文

我创建了一个 Windows 窗体应用程序:

  1. 具有多个 Windows 窗体的演示库
  2. 具有数据层的类库
  3. 访问数据库的类库

我正在使用带有 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:

  1. A presentation library with several windows forms
  2. A class library with a data layer
  3. 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 技术交流群。

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

发布评论

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

评论(3

羅雙樹 2024-12-01 16:55:11

您需要将 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...

带刺的爱情 2024-12-01 16:55:11

您的连接字符串错误,应该是这样的:

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.

日记撕了你也走了 2024-12-01 16:55:11

您是否尝试过为数据库播种?您必须注入一些数据,以便 EF 创建数据库,如果您不这样做,模型定义就像一个意图声明。

public class ContextInitializer : DropCreateDatabaseIfModelChanges<DBContext>
{
    protected override void Seed(DBContext context)    
    {
        context.Add(new Customers()); //add a Customer, for example
    }
}

然后查看您的调试/发布文件夹并检查数据库是否已正确创建。

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.

public class ContextInitializer : DropCreateDatabaseIfModelChanges<DBContext>
{
    protected override void Seed(DBContext context)    
    {
        context.Add(new Customers()); //add a Customer, for example
    }
}

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

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