获取以下错误:由于程序集包含EdmSchemaAttribute,程式无效并且类型无法加载
指定的模式无效。错误:
The types in the assembly 'x, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' cannot be loaded because the assembly contains the EdmSchemaAttribute, and the closure of types is being loaded by name. Loading by both name and attribute is not allowed.
这个错误究竟意味着什么? 我试图从现有数据库中将EF模型插入我的应用程序中。 在此应用程序基于CodeFirst并使用存储库模式之前,但对于我的生活,我无法得到这个工作。
之前,我有:
public class BaseModelContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Role> Roles { get; set; }
}
但在EF模型的第一个方案(即一个表中的数据块是否已经存在),我不得不删除这些,因为它似乎不喜欢有一个仓库DbSet属性上的模式。 所以我将它们分离出来,然后存储库可以使用已在.designer.cs上下文类(EF模型)上定义的类上的存储库。这在生成的代码中设置了EdmSchemaAttribute。
那么如何让我的存储库模式在模型优先的场景中工作呢?上述错误意味着什么?
编辑
增加了新的代码:
public class BaseModelContext : DbContext
{
// public DbSet<Location> Locations { get; set; }
public BaseModelContext(string nameOrConnection)
: base(nameOrConnection)
{
}
public BaseModelContext()
{
}
}
public class VisitoriDataContext : BaseModelContext
{
public VisitoriDataContext()
: base("visitoriDataConnection")
{
}
}
public interface IVisitoriDataContextProvider
{
VisitoriDataContext DataContext { get; }
}
public class VisitoriDataContextProvider : IVisitoriDataContextProvider
{
public VisitoriDataContext DataContext { get; private set; }
public VisitoriDataContextProvider()
{
DataContext = new VisitoriDataContext();
}
}
public class VisitoriRepository<T> : IRepository<T> where T : class
{
protected readonly IVisitoriDataContextProvider _ctx;
public VisitoriRepository(IVisitoriDataContextProvider ctx)
{
_ctx = ctx;
}
public T Get(int id)
{
return _ctx.DataContext.Set<T>().Find(id);
}
}
public interface ILocationRepo : IRepository<Location>
{
IEnumerable<Location> GetSuggestedLocationsByPrefix(string searchPrefix);
}
public class LocationRepo : VisitoriRepository<Location>, ILocationRepo
{
public LocationRepo(IVisitoriDataContextProvider ctx)
: base(ctx)
{
}
public IEnumerable<Location> GetSuggestedLocationsByPrefix(string searchPrefix)
{
return Where(l => l.name.Contains(searchPrefix)).ToList();
}
}
好吧,那么如何首先创建一个带有数据库的存储库模式? – jaffa
版本库模式对您如何使用EF的方式没有依赖性。发布存在问题的存储库示例。 –
嗨,你可以看看我的代码编辑,我已经添加了包装/上下文提供者类。应用程序抛出'GetSuggestedLocationsByPrefix'方法。 – jaffa