Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity Framework 4.2 "The type is not attributed with EdmEntityTypeAttribute but is contained in an assembly attributed with EdmSchemaAttribute

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

like image 467
bulltorious Avatar asked Dec 13 '11 16:12

bulltorious


1 Answers

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!

like image 116
bulltorious Avatar answered Nov 06 '22 00:11

bulltorious