我使用的是EF6和数据库第一方法。数据库有三个简单的表: Person、Token和Template
对于数据库,我已经生成了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,并在需要时加载使用DataProviderModule提供数据的EF6。应用工作流分为两个阶段。第一阶段初始化所需模块并加载这些模块的配置。然后创建一些数据并用DataProviderModule保存在数据库中。第一阶段到此为止-到目前为止还不错。
当第二阶段开始时,我清除旧模块引用,并使用新的DataProviderModule实例加载新的一组模块。但是现在,当我试图在DataProviderModule中使用EF时,就会抛出MetadataException (当我试图从数据库中获取数据时)。
using (var entities = new Entities(_connectionString))
{
return entities.People.Count() // <-- MetadataException thrown here
}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我已经搜索过了,如何解决这个问题,我只发现不同名称空间中相同的类名可能会导致类似的问题,但这不是我的情况,因为类在同一个名称空间中。
如有任何建议,将不胜感激。谢谢。
发布于 2015-06-12 08:06:55
经过几个小时的绝望探索到底出了什么问题,我终于找到了解决办法。问题是我写了两次MEF部分。每次在我的工作流中的每个阶段,我都认为同一个库因此在AppDomain中加载了两次。然后,EF在同一个名称空间中看到两个相同的类。
解决方案非常简单--在我的代码中,我创建组装目录和组成部件,我需要添加一个标志,以确保只执行该部件一次。
https://stackoverflow.com/questions/30780123
复制相似问题