Connection string for using SQL Server Compact with Entity Framework?

泪湿孤枕 提交于 2019-11-29 04:02:05
Rowinson Gallego

You could try this in your App.config file (EF5 Code first migrations and SQL Server CE 4.0):

<connectionStrings>
    <add name="DefaultConnection"
         providerName="System.Data.SqlServerCe.4.0"
         connectionString="Data Source=|DataDirectory|\Data\ProjectDb.sdf"/>
</connectionStrings>

And in you ProjectDb class:

class ProjectDb : DbContext
{
    public ProjectDb()
        : base("DefaultConnection")
    {

    }
}

It will work like a charm.

You can find more information here: http://blogs.msdn.com/b/adonet/archive/2011/01/27/using-dbcontext-in-ef-feature-ctp5-part-2-connections-and-models.aspx

I’d stuck with the same problem on Entity Framework 5.0 Code First approach with SQL Server Compact Edition 4.0

To solve it I pass instance of SqlCeConnection to DbContext's constructor:

public class Storage : DbContext
{
    public Storage()
        : base(new SqlCeConnection("Data Source=Database.sdf;Persist Security Info=False;"),
             contextOwnsConnection: true)
    { }

    //...
}

If you don’t want to inline connection string you could get it from App.config through ConfigurationManager And of course you should add reference to the right version of System.Data.SqlServerCe (look inside Extentions tab in the Add Reference dialog)

This TechNet article outlines the steps for using EF with SQL Server Compact. This line jumped out at me, and may solve your problems:

make sure that provider=System.Data.SqlServerCe.3.5 in the connection string.

(I would assume that you would want 4.0 instead of 3.5)

Give that a try and see how things go.

The Lu55's solution didn't work for me, but I found fow to fix it. To still use the automatic code generation (which can overwrite changes in a generated file), I added one more file with the following code:

using System.Data.Entity;
using System.Data.Objects;

namespace MyProject.Data
{
    internal partial class Entities : DbContext
    {
        public Entities(string connectionString)
            : base(
                new ObjectContext(
                        @"metadata=res://*/Data.Model.csdl|
                                   res://*/Data.Model.ssdl|
                                   res://*/Data.Model.msl;
                          provider=System.Data.SqlServerCe.4.0;"),
                true)
        {
            Database.Connection.ConnectionString = connectionString;
        }
    }
}
Vadym Kyrylkov

You have mixed a bit.

For SQL Server CE connection string can be quite easy (possibly replace DbContext with YourNameSpace.DbContextName):

<add name="DbContext" 
     connectionString="MyDbFile.sdf" 
     providerName="System.Data.SqlServerCe.4.0" />

I also had a lot of trouble with connections, so I did the following:

  • installed Entity Framework for SQL Server CE
  • Checked that web.config contains provider with invariant System.Data.SqlServerCe.4.0
  • Verified that there are NO string fields (which will be represented as columns in db) with length max or mode than 4000 symbols (to add restriction use annotation [MaxLength(4000)], by default == MAX)

Metadata are necessary when you use database-first model instead of code-first, so there is .edmx file which represents generated model of the database.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!