首页 文章

实体框架4.2“该类型不归因于EdmEntityTypeAttribute,但包含在使用EdmSchemaAttribute归属的程序集中

提问于
浏览
8

我收到以下错误:

System.InvalidOperationException未处理Message =类型'Judge'未归因于EdmEntityTypeAttribute,但包含在使用EdmSchemaAttribute归属的程序集中 . 不使用EdmEntityTypeAttribute的POCO实体不能与使用EdmEntityTypeAttribute的非POCO实体包含在同一程序集中 . Source = EntityFramework StackTrace:System.Data.Entity.InternalContext.UpdateEntitySetMappingsForType(Type entityType)at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType)....

public class GenericRepository<TEntity> where TEntity : class
{
    internal z context;
    internal DbSet<TEntity> dbSet;

    public GenericRepository(z context)
    {
        this.context = context;
        this.dbSet = context.Set<TEntity>();
    }

    public GenericRepository()
    {
        this.context = new z();
        this.dbSet = context.Set<TEntity>();
    }

    public virtual IEnumerable<TEntity> Get(
        Expression<Func<TEntity, bool>> filter = null,
        Func<IQueryable<TEntity>, IOrderedQueryable<TEntity>> orderBy = null,
        string includeProperties = "")
    {

        IQueryable<TEntity> query = dbSet;

        if (filter != null)
        {
            query = query.Where(filter);
        }

        foreach (var includeProperty in includeProperties.Split
            (new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
        {
            query = query.Include(includeProperty);
        }

        if (orderBy != null)
        {
            return orderBy(query).ToList();
        }
        else
        {
            return query.ToList(); //Getting error here!!
        }
    }

    public virtual TEntity GetByID(object id)
    {
        return dbSet.Find(id);
    }

    public virtual void Insert(TEntity entity)
    {
        dbSet.Add(entity);
    }

    public virtual void Delete(object id)
    {
        TEntity entityToDelete = dbSet.Find(id);
        Delete(entityToDelete);
    }

    public virtual void Delete(TEntity entityToDelete)
    {
        if (context.Entry(entityToDelete).State == EntityState.Detached)
        {
            dbSet.Attach(entityToDelete);
        }
        dbSet.Remove(entityToDelete);
    }

    public virtual void Update(TEntity entityToUpdate)
    {
        dbSet.Attach(entityToUpdate);
        context.Entry(entityToUpdate).State = EntityState.Modified;
    }

    public virtual void Save()
    {
        context.SaveChanges();
    }
}

奇怪的部分是Judge归因于EdmEntityTypeAttribute,因为它是作为DbContext T-4爵士乐的一部分自动生成的 .

/// <summary>
/// No Metadata Documentation available.
/// </summary>
[EdmEntityTypeAttribute(NamespaceName="standaloneModel", Name="Judge")]
[Serializable()]
[DataContractAttribute(IsReference=true)]
public partial class Judge : EntityObject
{

有一次,我在另一个集合中确实有另一个级别的法官,但我已经重命名了 . 我试过清理这两个项目 . 除了EF之外,不应该有其他的法官级别 .

所以我无法弄清楚这个其他法官班来自哪里?

谢谢

2 回答

  • 15

    弄清楚了 .

    当我第一次启动程序时,我正在使用带有.edmx的ObjectContext .

    然后我读了EF 4.2并决定使用DbContext .

    问题是我的.edmx文件正在生成类,以及DbContext T-4s .

    解决方案是关闭.edmx中的代码生成 .

    所以现在,只有DbContext T-4正在生成我的POCO类 .

    希望这个问题能在将来帮助别人!

  • 0

    我遇到了类似的问题 - 似乎在某些情况下(例如,使用WCF Data Services 5.2.0时),在与EDMX / model-first / generated类相同的程序集中使用代码优先/ DbContext类是个问题 . 对我来说,将DbContext类移动到一个单独的程序集中可以解决问题 .

    请注意,在访问数据库时,我在同一个程序集中的代码优先模型优先没有问题 . 但是一旦我添加了另一个层(WCF数据服务),我就遇到了EdmSchemaAttribute错误 .

相关问题