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

EF6: Schema specified is not valid; ambiguous mapping

我将 EF6 与数据库优先方法结合使用。该数据库具有三个简单的表: 人员、令牌和模板

对于数据库,我生成了 edmx 模型和实体

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

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

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

该应用程序基于 MEF,需要时它会加载使用 EF6 提供数据的 DataProviderModule。应用程序工作流程分为两个阶段。第一阶段初始化所需的模块并为这些模块加载配置。然后我创建一些数据并用 DataProviderModule 将其保存在数据库中。第一阶段到此结束 - 到目前为止一切顺利。

当第二阶段开始时,我清除旧模块引用并使用 DataProviderModule 的新实例加载新模块集。但是现在,当我尝试在 DataProviderModule 中使用 EF 时,会抛出 MetadataException(当我尝试从数据库中获取数据时)

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

元数据异常

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'.

堆栈跟踪

   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

我已经搜索过,如何解决这个问题,我只发现不同名称空间中的相同 class 名称可能会导致类似的问题,但这不是我的情况,因为 classes在同一个命名空间中。

任何建议将不胜感激。谢谢。

经过数小时的拼命搜索,我终于找到了解决办法。问题是我正在两次编写 MEF 部分。每次在我的工作流程中的每个阶段,我认为同一个库因此在 AppDomain 中加载两次。然后 EF 在同一个命名空间中看到两个相同的 类。

解决方案非常简单——在我创建程序集目录和组合部件的代码中,我需要添加一个标志以确保该部件只执行一次。