实体框架:在包管理器控制台VS 2012中添加迁移时引发Null引用异常
本文关键字:迁移 添加 异常 引用 Null 2012 框架 管理器 VS 控制台 实体 | 更新日期: 2023-09-27 18:26:01
我一直在我的mvc项目中使用实体框架代码优先迁移。但最近,在修改了我的模型类并添加了新的模型类之后,当我尝试在包管理器控制台中使用添加迁移时,我不断收到这样的错误:"Object reference not set to a instance of a Object"我正在使用entity framework version 5
,当我试着使用版本6时,它看不到我的DbContext,这是我的堆栈跟踪,请欢迎所有建议。
System.NullReferenceException: Object reference not set to an instance of an object.
at System.Data.Entity.ModelConfiguration.Configuration.Types.EntityTypeConfiguration.Configure(EdmEntityType entityType, EdmModel model)
at System.Data.Entity.ModelConfiguration.Configuration.ModelConfiguration.ConfigureEntities(EdmModel model)
at System.Data.Entity.DbModelBuilder.Build(DbProviderManifest providerManifest, DbProviderInfo providerInfo)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Infrastructure.EdmxWriter.WriteEdmx(DbContext context, XmlWriter writer)
at System.Data.Entity.Migrations.Extensions.DbContextExtensions.<>c__DisplayClass1.<GetModel>b__0(XmlWriter w)
at System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(Action`1 writeXml)
at System.Data.Entity.Migrations.Extensions.DbContextExtensions.GetModel(DbContext context)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration, DbContext usersContext)
at System.Data.Entity.Migrations.DbMigrator..ctor(DbMigrationsConfiguration configuration)
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.GetMigrator()
at System.Data.Entity.Migrations.Design.ToolingFacade.GetPendingMigrationsRunner.RunCore()
at System.Data.Entity.Migrations.Design.ToolingFacade.BaseRunner.Run()
Object reference not set to an instance of an object.
您的repo中缺少一个部分,但我发现其他人在这里报告了相同/类似的问题,所以我将使用他们的示例。这是复印件:
public class Person
{
public int PersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Meeting> Meeting { get; set; }
}
public class Meeting
{
public int MeetingID { get; set; }
[ForeignKey("Customer")]
public int CustomerID { get; set; }
public virtual Person Customer { get; set; }
[ForeignKey("SalesAgent")]
public int SalesAgentID { get; set; }
public virtual Person SalesAgent { get; set; }
}
public class MyContext : DbContext
{
public DbSet<Person> People { get; set; }
public DbSet<Meeting> Meetings { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Meeting>().HasRequired(m => m.Customer).WithMany(p => p.Meeting);
modelBuilder.Entity<Meeting>().HasRequired(m => m.SalesAgent).WithMany(p => p.Meeting);
}
}
class Program
{
static void Main(string[] args)
{
using (var ctx = new MyContext())
{
EdmxWriter.WriteEdmx(ctx, XmlWriter.Create(Console.Out, new XmlWriterSettings { Indent = true }));
}
}
}
EF5中存在一个导致NullReferenceException的错误。由于另一个错误,这在EF6中也不起作用,但我相信这些都与此无关。我认为这里的意图是将Person实体的一个导航属性映射到Meeting实体的两个导航属性,EF不支持这一点(类似地,您无法仅通过查看Person.Meeting集合的内容来判断用户是作为客户还是作为销售代理(或两者兼有?)-您必须比较密钥值/引用-EF只是不起作用)。为了解决这个问题,模型需要更改如下:
public class Person
{
public int PersonID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<Meeting> MeetingsAsCustomer { get; set; }
public virtual ICollection<Meeting> MeetingAsSalesAgent { get; set; }
}
public class Meeting
{
public int MeetingID { get; set; }
[ForeignKey("Customer")]
public int CustomerID { get; set; }
public virtual Person Customer { get; set; }
[ForeignKey("SalesAgent")]
public int SalesAgentID { get; set; }
public virtual Person SalesAgent { get; set; }
}
public class MyContext : DbContext
{
public DbSet<Person> People { get; set; }
public DbSet<Meeting> Meetings { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Meeting>().HasRequired(m => m.Customer).WithMany(p => p.MeetingsAsCustomer);
modelBuilder.Entity<Meeting>().HasRequired(m => m.SalesAgent).WithMany(p => p.MeetingAsSalesAgent);
}
现在,我们在Person属性上有两个导航属性,它们映射到Meeting实体上相应的导航属性。