简体   繁体   English

EF6:指定的架构无效; 模糊映射

[英]EF6: Schema specified is not valid; ambiguous mapping

I am using EF6 with the database first approach. 我将EF6与数据库优先方法一起使用。 The database has three simple tables: Person, Token and Template 该数据库具有三个简单表:Person,Token和Template

For the database I have generated the edmx model and entities 对于数据库,我生成了edmx模型和实体

Person.cs Person.cs

public partial class Person
{
    public Person()
    {
        this.Tokens = new HashSet<Token>();
    }
    public long Id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string PersonImage { get; set; }
    public string Repository { get; set; }
    public byte[] RepositoryHash { get; set; }
    public virtual ICollection<Token> Tokens { get; set; }
}

Token.cs Token.cs

public partial class Token
{
    public long Id { get; set; }
    public string TokenImage { get; set; }
    public string NormalizedTokenImage { get; set; }
    public long PersonId { get; set; }
    public long TemplateId { get; set; }
    public string TokenType { get; set; }
    public string Repository { get; set; }
    public byte[] RepositoryHash { get; set; }
    public virtual Person Person { get; set; }
    public virtual Template Template { get; set; }
}

Template.cs Template.cs

public partial class Template
{
    public Template()
    {
        this.Tokens = new HashSet<Token>();
    }
    public long Id { get; set; }
    public string TemplateFile { get; set; }
    public string TemplateType { get; set; }
    public string Repository { get; set; }
    public byte[] RepositoryHash { get; set; }
    public virtual ICollection<Token> Tokens { get; set; }
}

The application is based on MEF and when needed it loads the DataProviderModule that provides data using EF6. 该应用程序基于MEF,并在需要时加载使用EF6提供数据的DataProviderModule The application workflow is divided in two phases. 应用程序工作流程分为两个阶段。 Phase one initializes required modules and loads config for these modules. 第一阶段初始化所需的模块,并加载这些模块的配置。 Then I create some data and save it with DataProviderModule in database. 然后,我创建一些数据并将其与DataProviderModule一起保存在数据库中。 Phase one ends here - so far so good. 第一阶段到此结束-到目前为止一切顺利。

When the Phase Two starts, I clear the old modules references and load new set of modules with the new instance of DataProviderModule . 当第二阶段开始时,我将清除旧模块引用,并使用DataProviderModule的新实例加载新的模块集。 But now, when I try to use EF inside the DataProviderModule , the MetadataException is thrown (when I try to get data from database) 但是现在,当我尝试在DataProviderModule使用EF时,会抛出MetadataException(当我尝试从数据库中获取数据时)

using (var entities = new Entities(_connectionString))
{
    return entities.People.Count() // <-- MetadataException thrown here
}

MetadataException MetadataException

Schema specified is not valid. Errors: 
The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type 'Person'. 
Previously found CLR type 'MSSQLDBLib.Model.Person', 
newly found CLR type 'MSSQLDBLib.Model.Person'.

The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type 'Template'. 
Previously found CLR type 'MSSQLDBLib.Model.Template', 
newly found CLR type 'MSSQLDBLib.Model.Template'.

The mapping of CLR type to EDM type is ambiguous because multiple CLR types match the EDM type 'Token'. 
Previously found CLR type 'MSSQLDBLib.Model.Token', 
newly found CLR type 'MSSQLDBLib.Model.Token'.

StackTrace 堆栈跟踪

   at System.Data.Entity.Core.Metadata.Edm.ObjectItemCollection.LoadAssemblyFromCache(Assembly assembly, Boolean loadReferencedAssemblies, EdmItemCollection edmItemCollection, Action`1 logLoadMessage)
   at System.Data.Entity.Core.Metadata.Edm.ObjectItemCollection.ExplicitLoadFromAssembly(Assembly assembly, EdmItemCollection edmItemCollection, Action`1 logLoadMessage)
   at System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.ExplicitLoadFromAssembly(Assembly assembly, ObjectItemCollection collection, Action`1 logLoadMessage)
   at System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.LoadFromAssembly(Assembly assembly, Action`1 logLoadMessage)
   at System.Data.Entity.Core.Metadata.Edm.MetadataWorkspace.LoadFromAssembly(Assembly assembly)
   at System.Data.Entity.Core.Metadata.Edm.MetadataOptimization.TryUpdateEntitySetMappingsForType(Type entityType)
   at System.Data.Entity.Internal.InternalContext.TryUpdateEntitySetMappingsForType(Type entityType)
   at System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type entityType)
   at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)
   at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize()
   at System.Data.Entity.Internal.Linq.InternalSet`1.get_InternalContext()
   at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
   at System.Linq.Queryable.Count[TSource](IQueryable`1 source, Expression`1 predicate)
   at MSSQLDBLib.MsSqlPersonalDataProvider.GetPersonsCount()
   at DirectoryCompareApp.ViewModel.MainViewModel.<Start>d__5f.MoveNext() in d:\pzajic\SecufaceCollection\SecufaceNG\Sources\SecufaceNG\DirectoryCompareApp\ViewModel\MainViewModel.cs:line 810

I've already searched, how to solve this problem and I only found that same class names within different namespaces may cause similar problem, but this is not my case because the classes are in the same namespace. 我已经搜索过如何解决此问题的方法,但我只发现不同名称空间中的相同类名可能会导致类似的问题,但这不是我的情况,因为这些类位于同一名称空间中。

Any advise will be greatly appreciated. 任何建议将不胜感激。 Thank you. 谢谢。

After hours of desperate searching what's wrong I finally found a solution. 经过数小时的拼命搜索,终于找到了解决方案。 The problem is that I am composing MEF parts twice. 问题是我要两次构造MEF零件。 Every time for each phase in my workflow and I think the same library is therefore loaded twice in the AppDomain. 每次在工作流程的每个阶段,我都认为同一库在AppDomain中加载两次。 EF then sees two same classes in the same namespace. 然后,EF在同一名称空间中看到两个相同的类。

The solution was very simple – in my code where I create assembly catalog and compose parts I need to add a flag to ensure this part is executed only once. 解决方案非常简单–在我的代码中,我创建装配目录并编写零件,我需要添加一个标志以确保该零件仅执行一次。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM