实体类型< type>不是当前上下文模型的一部分 [英] The entity type <type> is not part of the model for the current context

查看:353
本文介绍了实体类型< type>不是当前上下文模型的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在进入实体框架,但我不确定我是否缺少代码优先方法中的关键点。



我正在使用通用存储库基于 https://genericunitofworkandrepositories.codeplex.com/ 的代码,并创建了我的实体。 p>

但是当我尝试访问或修改实体时,我遇到以下情况:


System.InvalidOperationException:实体类型Estate不是当前上下文模型的
的一部分。


我试图从我的存储库访问它:

  public virtual void Insert(TEntity entity)
{
((IObjectState)实体).ObjectState = ObjectState.Added;
_dbSet.Attach(entity); //< - 这里出现错误
_context.SyncObjectState(entity);
}

数据库(./SQLEXPRESS)创建的很好,但实体表)在启动时不会创建。



我想知道我是否需要显式设置实体的映射? EF自己不能这样做吗?



我的实体是:

  public class Estate:EntityBase 
{
public int EstateId {get;组; }
public string Name {get;组;
}

我的上下文是这样的:

  public partial class DimensionWebDbContext:DbContextBase // DbContextBase inherits DbContext 
{
public DimensionWebDbContext():
base(DimensionWebContext)
{
Database.SetInitializer&DimensionWebDbContext>(new CreateDatabaseIfNotExists&DimensionWebDbContext>());
Configuration.ProxyCreationEnabled = false;
}

public new IDbSet< T>设置< T>()其中T:class
{
return base.Set< T>();
}

}

有什么具体原因发生错误?我尝试启用迁移并启用自动迁移,无需任何帮助。

解决方案

将其放在您的自定义 DbContext class:

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< Estate>()。ToTable(Estate);
}

如果您的表在启动时未创建,这就是为什么。您需要在OnModelCreating方法覆盖中告诉DbContext关于它们。



您可以在此处执行自定义的每个实体映射,也可以将它们分离为单独的 EntityTypeConfiguration< T> 类。


I am getting into the Entity Framework, but I am unsure if I am missing a critical point in the code-first approach.

I am using a generic repository pattern based on the code from https://genericunitofworkandrepositories.codeplex.com/ and have created my entities.

But when I try to access or modify the entity I run into the following:

System.InvalidOperationException: The entity type Estate is not part of the model for the current context.

It happens when I am trying to access it from my repository:

public virtual void Insert(TEntity entity)
{
    ((IObjectState)entity).ObjectState = ObjectState.Added;
    _dbSet.Attach(entity); // <-- The error occurs here
    _context.SyncObjectState(entity);
}

The database (./SQLEXPRESS) is created just fine, but the entities (tables) is just not created on startup.

I am wondering if I need to explicit set the mapping of the entities? Is EF not able to this by its own?

My Entity is:

public class Estate : EntityBase
{
    public int EstateId { get; set; }
    public string Name { get; set; }
} 

My context is as so:

public partial class DimensionWebDbContext : DbContextBase // DbContextBase inherits DbContext
{
    public DimensionWebDbContext() :
        base("DimensionWebContext")
    {
        Database.SetInitializer<DimensionWebDbContext>(new CreateDatabaseIfNotExists<DimensionWebDbContext>());
        Configuration.ProxyCreationEnabled = false;
    }

    public new IDbSet<T> Set<T>() where T : class
    {
        return base.Set<T>();
    }

}

Is there any specific reason why this error occurs? I have tried enable migrations and enable automatic migrations without any help either.

解决方案

Put this in your custom DbContext class:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<Estate>().ToTable("Estate");
}

If your tables are not created on startup, this is why. You need to tell the DbContext about them in the OnModelCreating method override.

You can either do custom per-entity mappings here, or separate them out into separate EntityTypeConfiguration<T> classes.

这篇关于实体类型&lt; type&gt;不是当前上下文模型的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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