实体框架4.2“该类型不归因于EdmEntityTypeAttribute,但包含在归属于EdmSchemaAttribute的程序集中 [英] Entity Framework 4.2 "The type is not attributed with EdmEntityTypeAttribute but is contained in an assembly attributed with EdmSchemaAttribute

查看:191
本文介绍了实体框架4.2“该类型不归因于EdmEntityTypeAttribute,但包含在归属于EdmSchemaAttribute的程序集中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:


System.InvalidOperationException未处理Message =类型
'Judge'不是归因于EdmEntityTypeAttribute,但在由EdmSchemaAttribute归因的程序集中包含

不使用EdmEntityTypeAttribute的POCO实体不能包含在与使用EdmEntityTypeAttribute的非POCO实体相同的
程序集中。

Source = EntityFramework StackTrace:
at System。 Data.Entity.Internal.InternalContext.UpdateEntitySetMappingsForType(Type
entityType)
在System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type
entityType)....




  public class GenericRepository< TEntity>其中TEntity:class 
{
internal z context;
内部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(); //在这里获取错误!
}
}

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);
删除(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> 
///没有元数据文档可用。
///< / summary>
[EdmEntityTypeAttribute(NamespaceName =standaloneModel,Name =Judge)]
[Serializable()]
[DataContractAttribute(IsReference = true)]
public partial class Judge: EntityObject
{

有一点我在另一个程序集中有另一个类Judge,但是我已经改名了我已经尝试清理这两个项目。除了EF之外,不应该有其他法官级别。



所以我无法确定这个其他法官课程来自哪里?



谢谢

解决方案

想出来。



当我第一次启动该程序时,我使用的是.ContextContext与.edmx。



然后我阅读了关于EF 4.2并决定使用DbContext。



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



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



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



希望这个问题有助于别人在未来! b $ b

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

解决方案

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!

这篇关于实体框架4.2“该类型不归因于EdmEntityTypeAttribute,但包含在归属于EdmSchemaAttribute的程序集中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆