在模型创建中,EntityFramework对象未设置为对象的实例
本文关键字:对象 设置 实例 EntityFramework 模型 创建 | 更新日期: 2023-09-27 18:28:15
我一直在努力将在模型优先中创建的相当大的EntityFramework数据库转换为代码优先。我有一个问题似乎无法解决。通过调用堆栈上的以下过程,我得到了一个未设置为对象实例的Object引用。
ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure
ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntities
为了简化帖子,我创建了一个测试项目,将问题归结为最简单的形式
我有三个班
- 具有an的a具有可选的b和可选的c
- b拥有a的集合,以及c的同谋
c具有可选的b和a的集合
public class a { public int Id { get; set; } [Required] public string name { get; set; } public virtual b b { get; set; } [ForeignKey("b")] public int? b_Id { get; set; } public virtual c c { get; set; } [ForeignKey("c")] public int? c_Id { get; set; } } public class b { public int Id { get; set; } public string name { get; set; } public virtual ICollection<a> a_s { get; set; } public virtual ICollection<c> c_s { get; set; } } public class c { public int Id { get; set; } public virtual b b { get; set; } [ForeignKey("b")] public int? b_Id { get; set; } public virtual ICollection<a> a_s { get; set; } } public class MyContext : DbContext { public DbSet<a> a { get; set; } public DbSet<b> b { get; set; } public DbSet<c> c { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<a>() .HasOptional(m => m.b) .WithMany(m => m.a_s); modelBuilder.Entity<b>() .HasMany(m => m.c_s) .WithRequired(m => m.b); modelBuilder.Entity<c>() .HasMany(m => m.a_s) .WithOptional(m => m.c); base.OnModelCreating(modelBuilder); } }
当我在db.aselecto中执行代码vara=fromo时,我会得到上面描述的错误。完全没有关于正在发生的事情的信息,所以我真的不知道该向哪里求助。有人能帮我解决这个问题吗?因为我真的想离开Model First。
namespace MvcApplication2.Controllers { public class HomeController : Controller { public ActionResult Index() { var db = new MyContext(); var a = from o in db.a select o; return View(); } } }
fluent配置和数据注释的混淆导致了这个问题。EF团队应该已经处理了这个异常,并给出了一个有意义的错误消息。
删除数据注释并使用流畅的配置,如下所示
public class a
{
public int Id { get; set; }
[Required]
public string name { get; set; }
public virtual b b { get; set; }
public int? b_Id { get; set; }
public virtual c c { get; set; }
public int? c_Id { get; set; }
}
public class b
{
public int Id { get; set; }
public string name { get; set; }
public virtual ICollection<a> a_s { get; set; }
public virtual ICollection<c> c_s { get; set; }
}
public class c
{
public int Id { get; set; }
public virtual b b { get; set; }
public int? b_Id { get; set; }
public virtual ICollection<a> a_s { get; set; }
}
public class NreContext : DbContext
{
public DbSet<a> a { get; set; }
public DbSet<b> b { get; set; }
public DbSet<c> c { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<a>()
.HasOptional(m => m.b)
.WithMany(m => m.a_s)
.HasForeignKey(m => m.b_Id);
modelBuilder.Entity<a>()
.HasOptional(m => m.c)
.WithMany(m => m.a_s)
.HasForeignKey(m => m.c_Id);
modelBuilder.Entity<c>()
.HasOptional(m => m.b)
.WithMany(m => m.c_s)
.HasForeignKey(m => m.b_Id);
base.OnModelCreating(modelBuilder);
}
}
尝试将"a"放入本地内存:
var a = from o in db.a.ToList() select o;