Can't separate entity framework models have the same table names?

.net c# entity-framework sql-server

Question

There are two separate SQL 2008 databases used by my application. A few tables with the same name exist in the databases, i.e.Users . For both of these databases, EF4 is what I would want to utilize. However, the following problem occurs while my program is running and it reaches the objectcontext setup of the second database:

Multiple types with the name 'User' exist in the EdmItemCollection in different namespaces. Convention based mapping requires unique names without regard to namespace in the EdmItemCollectionto namespace in the EdmItemCollection

Does this imply that I can't combine two databases with (partly) similar table names into one application? They are involved in several projects, namespaces, and edmx models.

P.S. One model was built by the designer and employs POCO classes, while the other was inferred from the database and is closely tied to EF.

1
16
11/3/2010 12:53:25 PM

Accepted Answer

He is correct when he argues that the problem implies you cannot utilize the default mapping using conventions in your case. Instead, use specialized database mapping. The blog post by Scott Guthrie on this is comprehensive.

10
11/3/2010 2:48:50 PM

Popular Answer

The following two methods will function when using the "default convention based mapping":

1) The connection string's usage of a wild card results in the collision:

    metadata=res://*/Repositories.EntityFramework.Model.csdl|res://*/Repositories.EntityFramework.Model.ssdl|res://*/Repositories.EntityFramework.Model.msl;

You may declare numerous connection strings to hard code the assembly containing the edmx because * does not work in your app.

2) Design an assistant.

    public static EntityConnection GetEfConnectionString(this string sqlConnectionString)
    {
        var cs = string.Format(@"metadata=res://{0}/Repositories.EntityFramework.Model.csdl|res://{0}/Repositories.EntityFramework.Model.ssdl|res://{0}/Repositories.EntityFramework.Model.msl;provider=System.Data.SqlClient;provider connection string=""" + sqlConnectionString + @"""",
            Assembly.GetCallingAssembly().FullName
        );

        return new EntityConnection(cs);
    }

Revision 2017:

    public static string GetEfConnectionString(this string sqlConnectionString, Type type)
    {
        string cs =
            string.Format(
                @"metadata=res://{0}/Models.Model.csdl|res://{0}/Models.Model.ssdl|res://{0}/Models.Model.msl;provider=System.Data.SqlClient;provider connection string=""" +
                sqlConnectionString + @"""",
                type.Assembly.FullName
                );
        return cs;
    }


    // usage: don't "new" EntityConnection.  See 2012 comment.
    string connString = ConfigurationManager.ConnectionStrings["sqlConnection"].ConnectionString.GetEfConnectionString();
    using(var entities = new XyzEntities(connString))


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow