Entity Framework 4 and SQL Compact 4: How to generate database?

后端 未结 7 1703
别跟我提以往
别跟我提以往 2020-12-12 17:30

I am developing an app with Entity Framework 4 and SQL Compact 4, using a Model First approach. I have created my EDM, and now I want to generate a SQL Compact 4.0 database

相关标签:
7条回答
  • 2020-12-12 17:58

    You can use IDatabaseInitializer and create the database in code using

    if (File.Exists("Test.sdf"))
        File.Delete("Test.sdf");
    
    string connStr = "Data Source = Test.sdf; Password = <password>";
    
    SqlCeEngine engine = new SqlCeEngine(connStr);
    engine.CreateDatabase();
    engine.Dispose();
    
    SqlCeConnection conn = null;
    
    try {
        conn = new SqlCeConnection(connStr);
        conn.Open();
    
        SqlCeCommand cmd = conn.CreateCommand();
        cmd.CommandText = "CREATE TABLE myTable (col1 int, col2 ntext)";
        cmd.ExecuteNonQuery();
    
    catch {
    finally {
        conn.Close();
    

    http://msdn.microsoft.com/en-us/library/system.data.sqlserverce.sqlceengine(v=vs.80).aspx

    0 讨论(0)
  • 2020-12-12 18:00

    Probably need the SQL Server Compact 4 tools installed as well, if it did not install directly when Visual Studio 2010 SP1 was installed, you can install it. This is what worked for me when I had the problem.

    0 讨论(0)
  • 2020-12-12 18:02

    To answer the comments below and address them according to: http://blogs.msdn.com/b/sqlservercompact/archive/2011/01/12/microsoft-sql-server-compact-4-0-is-available-for-download.aspx

    These wizards to not work.

    Designers in the VB or C# Windows projects in Visual Studio 2010 SP1 Beta: The following wizards do not work with Compact 4.0 in the Windows project system. Developers can manually add reference to the ADO.NET provider for Compact 4.0 (System.Data.SqlServerCe) to develop programs for Compact 4.0 in the Windows projects:

    1. The Data Source Configuration Wizard that is used to configure datasets.
    2. The Configure Data Configuration wizard that is used to setup the syncing of data and schema with SQL Server using Sync FX.
    3. The Entity Data Model wizard that is used to generate entities from a Compact database.

    Here are the accepted work arounds for these issues. http://erikej.blogspot.com/2010/11/using-entity-framework-with-sql-server.html

    0 讨论(0)
  • 2020-12-12 18:02

    As I understand it current version of VS 2010 doesn't have support for SQL CE 4.0. It should be included in VS 2010 SP1 (currently in Beta). Check this blog post which also describes using EF Model with SQL CE 4.0 in SP1.

    Edit:

    I found this workaround.

    0 讨论(0)
  • 2020-12-12 18:03

    I can run Generate Database Wizard with out any issue from my Chinook.Data project, make sure you have a proper connection string in your app.config. http://erikej.blogspot.com/2010/11/using-entity-framework-with-sql-server.html

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <connectionStrings>
        <add name="ChinookEntities" connectionString="metadata=res://*/ChinookModel.csdl|res://*/ChinookModel.ssdl|res://*/ChinookModel.msl;provider=System.Data.SqlServerCe.4.0;provider connection string=&quot;Data Source=C:\projects\Chinook\Chinook40.sdf&quot;" providerName="System.Data.EntityClient" />
      </connectionStrings>
    </configuration>
    
    0 讨论(0)
  • 2020-12-12 18:05

    Yet another, use WebMatrix database tool.

    Microsoft WebMatrix

    0 讨论(0)
提交回复
热议问题