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

前端 未结 5 1568
心在旅途
心在旅途 2021-01-07 23:13

My application uses two different SQL 2008 databases. The databases have a few tables with the same name, ie. Users. I would like to use EF4 for both these data

相关标签:
5条回答
  • 2021-01-07 23:29

    To use the "default convention based mapping" the following 2 approaches will work:

    1) The collision is caused by the connection string using a wild card:

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

    Since * does not work your project you can define multiple connection strings to hard code the assembly containing the edmx.

    2) create a helper

        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);
        }
    

    Update 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))
    
    0 讨论(0)
  • 2021-01-07 23:43

    For DB first approach, Luckily there are couple of workarounds which are pretty straightforward:

    1. Make sure that that both contexts don't share tables with the same name - not the most practical approach.
    2. Restructure the solution by isolating the database contexts within their own assemblies.

    For more detail, please refer this link

    Explained very clearly.

    0 讨论(0)
  • 2021-01-07 23:48

    I had the same problem, but my solution was to delete the DLL of the MODEL\edmx from the project, because it was copied from a different project, and then rebuild it. Solved everything!

    0 讨论(0)
  • 2021-01-07 23:51

    The error means, what he says: You can't use the default convention based mapping in your scenario. Use custom database mapping instead. Scott Guthrie has a detailed blog item about this.

    0 讨论(0)
  • 2021-01-07 23:53

    Another reason for that error is what happened to me: My project was copied from somewhere else and I renamed the project, so after compiling there were 2 DLL's in my bin folder. The one from the previous project and a brand new one, named according to the new project. Once I deleted the previous/old DLL the namespace ambiguity disappeared and the error went away.

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