开发者

EF 4 - same context, different store?

we have a .net solution which runs on different views (WinForms, asp.net).

As data access layer we use the entity framework 4. As backend db's we use sql server and sqlCe.

Sqlce is used to store a copy of the original db for the fat client.

So through this constellation arrives my question:

Is it possible to use the same ObjectContext for two different data stores?

(the same db in sql server and sqlce)

or is it better to use POCO's or a开发者_如何学编程 different architecture?

Thanks


If your local database (SQL CE) has exactly same structure as big database (SQL Server) you can use same mapping by passing different connection string into ObjectContext constructor. Entities / POCOs will be the same.

If your local database doesn't have the same structure you need two mapping sets (EDMXs) and you would probably need two different contexts (unless you use ObjectContext directly). If your local mapping uses the same entities as your server mapping you will be able to map them to the same POCO classes.


Ok, worked fine for me.

1.Replaced the ssdl file. As described in this Article: Preparing an Entity Framework model for multi provider support

2.Set the build action of the *.ssdl file as embedded ressource.

3.Changed my connectionstring

    public static String GetEntityConnectionString()
    {
        string csdl = "Test";
        string ssdl = "Namespace.NewSsdl.ssdl"; //multi provider support (.ssdl embedded in ressource)
        string msl = "Test";

        EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder();

        //Provider
        builder.Provider = "System.Data.SqlClient";
        builder.ProviderConnectionString = GetProviderConnectionStringPL();

        //Metadata
        builder.Metadata = string.Format("res://*/{0}.csdl|res://*/{1}|res://*/{2}.msl", csdl, ssdl, msl);

        return builder.ToString();

    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜