i have been using entity framework code first migration in my mvc project. But recently after modifying my model class and adding new ones, when i try to use add-migration in package manager console, i keep getting this error : 'Object reference not set to an instance of an object' i am using entity framework version 5
and when i try using version 6 but it wont see my DbContext,here is my stack trace, please every suggestion will be welcomed.
我一直在我的mvc项目中使用实体框架代码首次迁移。但最近在修改我的模型类并添加新的模型类之后,当我尝试在包管理器控制台中使用add-migration时,我不断收到此错误:'对象引用未设置为对象的实例'我正在使用实体框架版本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.
1 个解决方案
#1
1
There was a missing piece in your repro but I found someone else reporting the same/similar issue here so I will use their example. Here is the repro:
你的repro中有一个缺失的部分,但我发现其他人报告了相同/类似的问题,所以我将使用他们的例子。这是repro:
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 }));
}
}
}
There is a bug in EF5 that causes the NullReferenceException. This does not work in EF6 either due to a different bug but I believe none of this is actually relevant here. I think the intention here was to map one navigation property from the Person entity to two navigation properties from the Meeting entity and this is not supported by EF (similarly you would not be able to tell just by looking at the contents of the Person.Meeting collection whether the user is in a meeting as a customer or as a sales agent (or both?) - you would have to compare key values/references - EF just does not do it). To fix this the model needs to be changed as follows:
EF5中存在导致NullReferenceException的错误。由于不同的bug,这在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);
}
Now we have two navigation properties on the Person property that are mapped to the corresponding navigation properties on the Meeting entity.
现在,我们在Person属性上有两个导航属性,这两个属性映射到Meeting实体上的相应导航属性。
#1
1
There was a missing piece in your repro but I found someone else reporting the same/similar issue here so I will use their example. Here is the repro:
你的repro中有一个缺失的部分,但我发现其他人报告了相同/类似的问题,所以我将使用他们的例子。这是repro:
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 }));
}
}
}
There is a bug in EF5 that causes the NullReferenceException. This does not work in EF6 either due to a different bug but I believe none of this is actually relevant here. I think the intention here was to map one navigation property from the Person entity to two navigation properties from the Meeting entity and this is not supported by EF (similarly you would not be able to tell just by looking at the contents of the Person.Meeting collection whether the user is in a meeting as a customer or as a sales agent (or both?) - you would have to compare key values/references - EF just does not do it). To fix this the model needs to be changed as follows:
EF5中存在导致NullReferenceException的错误。由于不同的bug,这在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);
}
Now we have two navigation properties on the Person property that are mapped to the corresponding navigation properties on the Meeting entity.
现在,我们在Person属性上有两个导航属性,这两个属性映射到Meeting实体上的相应导航属性。