The mapping of CLR type to EDM type is ambiguous with EF 6 & 5?

后端 未结 14 1563
长发绾君心
长发绾君心 2020-11-27 05:38

Please any one can help me to fix this error?

Schema specified is not valid. Errors:

The mapping of CLR type to EDM type is ambiguous because mult

相关标签:
14条回答
  • 2020-11-27 06:08

    For me this was because I was attempting to access a type with the same name on the wrong context instance.

    Say both ContextA and ContextB have SomeType. I was trying to access ContextA.SomeType on an instance of ContextB.

    0 讨论(0)
  • 2020-11-27 06:10

    I got the error above because for both connection strings, I had the same value for metadata specified in my main project's config file, like below:

    <add name="EntitiesA" connectionString="metadata=res://*/EntitiesA.csdl|res://*/EntitiesA.ssdl|res://*/EntitiesA.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost;initial catalog=MyDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    
    <add name="EntitiesB" connectionString="metadata=res://*/EntitiesA.csdl|res://*/EntitiesA.ssdl|res://*/EntitiesA.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=localhost;initial catalog=MyDatabase;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
    

    I ended up copying the correct connection string from the EntitiesB's project's config file.

    0 讨论(0)
  • 2020-11-27 06:16

    In some cases this is more of a symptom than the actual problem. For me, it usually pops up when I try to call a function inside a Linq query without calling .ToList() first.

    E.g. the error that brought me here was caused because I did this:

    var vehicles = DB.Vehicles.Select(x => new QuickSearchResult()
    {
        BodyText = x.Make + " " + x.Model + "<br/>"
        + "VIN: " + x.VIN + "<br/>"
        + "Reg: " + x.RegistrationNumber +"<br/>"
        + x.AdditionalInfo
        type = QuickSearchResultType.Vehicle,//HERE. Can't use an enum in an IQueryable.
        UniqueId = x.VehicleID
    });
    

    I had to call .ToList(), then iterate through each item and assign the type to it.

    0 讨论(0)
  • 2020-11-27 06:21

    I was able to solve this issue without renaming the classes, properties, or metadata.

    I had my project setup with a T4 transform creating entity objects in a DAL project, and a T4 transform creating domain objects in a Domain project, both referencing the EDMX to generate identical objects, and then I was mapping the DAL objects to the Domain objects.

    The error only occurred when I was referencing other classes (enums in my case) from the Domain assembly in my queries. When I removed them, the error went away. It looks like EF was loading up my Domain assembly because of this, seeing the other identically named classes, and blowing up.

    To resolve this, I made a separate assembly that only contained my T4 transformed Domain classes. Since I never need to use these inside a query (only after the query to map to), I no longer have this issue. This seems cleaner and easier than the answers below.

    0 讨论(0)
  • 2020-11-27 06:21

    if you have 2 connection string in web config but you want to use one connection string You use dynamic create connection string other entities. I have edmx(db first) and code first Entities in my solution. I use this class in Code first entities.

    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.Data.Entity.Core.EntityClient;
    using System.Data.SqlClient;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    namespace Data
    {
        public class SingleConnection
        {
            private SingleConnection() { }
            private static SingleConnection _ConsString = null;
            private String _String = null;
    
            public static string ConString
            {
                get
                {
                    if (_ConsString == null)
                    {
                        _ConsString = new SingleConnection { _String = SingleConnection.Connect() };
                        return _ConsString._String;
                    }
                    else
                        return _ConsString._String;
                }
            }
    
            public static string Connect()
            {
                string conString = ConfigurationManager.ConnectionStrings["YourConnectionStringsName"].ConnectionString;
    
                if (conString.ToLower().StartsWith("metadata="))
                {
                    System.Data.Entity.Core.EntityClient.EntityConnectionStringBuilder efBuilder = new System.Data.Entity.Core.EntityClient.EntityConnectionStringBuilder(conString);
                    conString = efBuilder.ProviderConnectionString;
                }
    
                SqlConnectionStringBuilder cns = new SqlConnectionStringBuilder(conString);
                string dataSource = cns.DataSource;
                SqlConnectionStringBuilder sqlString = new SqlConnectionStringBuilder()
                {
                    DataSource = cns.DataSource, // Server name
                    InitialCatalog = cns.InitialCatalog,  //Database
                    UserID = cns.UserID,         //Username
                    Password = cns.Password,  //Password,
                    MultipleActiveResultSets = true,
                    ApplicationName = "EntityFramework",
    
                };
                //Build an Entity Framework connection string
                EntityConnectionStringBuilder entityString = new EntityConnectionStringBuilder()
                {
                    Provider = "System.Data.SqlClient",
                    Metadata = "res://*",
                    ProviderConnectionString = sqlString.ToString()
                };
                return entityString.ConnectionString;
            }
        }
    }
    

    And when I call entities

    private static DBEntities context
    {
    get
    {
        if (_context == null)
            _context = new DBEntities(SingleConnection.ConString);
    
        return _context;
    
    }
    set { _context = value; }
    }
    
    0 讨论(0)
  • 2020-11-27 06:23

    I Think u Have a Class X named "MyClass" in Entity Models and Another Class Called "MyClass" in the same WorkFolder or Extended of the first Class. That is my problem and i fix it.

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