Entity Framework 4.2 "Type does not apply to EdmEntityTypeAttribute, but is contained in an assembly related to EdmSchemaAttribute

I get the following error:

System.InvalidOperationException is not handled Message = The judge type is not assigned to the EdmEntityTypeAttribute attribute, but is contained in the assembly associated 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: in System.Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType (Type object type) in 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: the judge is attributed to the EdmEntityTypeAttribute attribute because it is automatically created 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 some point, I had a different class judge in a different assembly, but since then I renamed it. I tried to clean both projects. There should be no other class of judges than EF.

So, I can’t understand where this other class of judge comes from

thanks

+8
c # repository entity-framework entity-framework-4
source share
2 answers

Figured it out.

When I first started the program, I used an ObjectContext with .edmx.

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

The problem was that my .edmx file generated classes as well as DbContext T-4.

The solution was to disable code generation in .edmx.

So, now only the DbContext T-4 classes generate my POCO classes.

Hope this question helps someone else in the future!

+15
source share

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

Please note that I did not have a problem with the first code + first the first in the same assembly when you just accessed the database. But as soon as I added another layer (WCF data services), I ran into the EdmSchemaAttribute error.

0
source share

All Articles