【问题标题】:EF6: Schema specified is not valid; ambiguous mappingEF6:指定的架构无效;模糊映射
【发布时间】:2015-06-12 16:41:19
【问题描述】:

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

模板.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

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

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

【问题讨论】:

    标签: c# entity-framework entity-framework-6 mef


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-05-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-16
      • 2017-10-12
      相关资源
      最近更新 更多