Entity Framework 4.2 "The type is not attributed w

2019-03-27 06:06发布

I am receiving the following error:

System.InvalidOperationException was unhandled Message=The type 'Judge' is not attributed with EdmEntityTypeAttribute but is contained in an assembly attributed with EdmSchemaAttribute. POCO entities that do not use EdmEntityTypeAttribute cannot be contained in the same assembly as non-POCO entities that use EdmEntityTypeAttribute.
Source=EntityFramework StackTrace: at System.Data.Entity.Internal.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();
    }
}

The weird part is Judge is attributed with the EdmEntityTypeAttribute, because it is automatically generated as part of the DbContext T-4 jazz.

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

At one point I did have another class Judge in a different assembly, but I have since renamed it. I have tried cleaning both projects. There should be no other Judge class besides the EF one.

So I can not figure out where this other Judge class is coming from??

Thanks

2条回答
仙女界的扛把子
2楼-- · 2019-03-27 06:51

Figured it out.

When I first started the program I was using an ObjectContext with the .edmx.

Then I read about EF 4.2 and decided to use DbContext.

The problem was my .edmx file was generating classes, as well as the DbContext T-4s.

The solution was to turn off code generation in the .edmx.

So now, only the DbContext T-4s are generating my POCO classes.

Hope this questions helps someone else in the future!

查看更多
Bombasti
3楼-- · 2019-03-27 06:55

I had a similar problem - it seems that in some cases (for example, when using WCF Data Services 5.2.0), it's a problem to have code-first/DbContext classes in the same assembly as EDMX/model-first/generated classes. For me, moving the DbContext classes into a separate assembly fixed the problem.

Note that I didn't have a problem with code-first + model-first in the same assembly when just accessing the DB. But as soon as I added another layer (WCF Data Services) I ran into the EdmSchemaAttribute error.

查看更多
登录 后发表回答