当我创建一个具有地址关系(多对多)的事件时,我会得到错误:对象引用没有设置为对象的实例
本文关键字:错误 对象引用 实例 对象 设置 创建 一个 地址 关系 事件 | 更新日期: 2023-09-27 17:50:11
可能重复:
中的NullReferenceException是什么。NET?
我有一个类Events,它与地址有关系,这是一个多对多的关系。当我试图将地址添加到我的事件中时,它会给出错误:
对象引用未设置为对象的实例。
这是我的代码:
事件类
public class Event
{
public int EventID { get; set; }
public string EventName { get; set; }
public string EventImage { get; set; }
public DateTime EventDate { get; set; }
public int ItemID { get; set; }
public virtual Item Item { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
}
地址类
public class Address
{
public int Address_id { get; set; }
public string Address_street { get; set; }
public int Address_housenumber { get; set; }
public string Address_bus { get; set; }
public Nullable<int> Messages_id { get; set; }
public virtual ICollection<Event> Events { get; set; }
}
上下文
public class SocialGEOContext : DbContext
{
public DbSet<Event> Events { get; set; }
public DbSet<Address> Addresses { get; set; }
public DbSet<Item> Items { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
//EVENTS
modelBuilder.Entity<Event>().HasKey(t => t.EventID);
modelBuilder.Entity<Event>().ToTable("events", "geodatabase");
modelBuilder.Entity<Event>().Property(t => t.EventID).HasColumnName("event_id");
modelBuilder.Entity<Event>().Property(t => t.EventName).HasColumnName("event_name").IsRequired().IsUnicode(false).HasMaxLength(64);
modelBuilder.Entity<Event>().Property(t => t.EventDate).HasColumnName("event_date");
modelBuilder.Entity<Event>().Property(t => t.EventImage).HasColumnName("event_image").IsUnicode(false).HasMaxLength(128);
modelBuilder.Entity<Event>().Property(t => t.ItemID).HasColumnName("item_id");
modelBuilder.Entity<Event>().HasRequired(t => t.Item).WithMany(t => t.Events).HasForeignKey(d => d.ItemID);
//ADDRESSES
modelBuilder.Entity<Address>().HasKey(t => t.Address_id);
modelBuilder.Entity<Address>().ToTable("addresses", "geodatabase");
modelBuilder.Entity<Address>().Property(t => t.Address_id).HasColumnName("address_id");
modelBuilder.Entity<Address>().Property(t => t.Address_street).HasColumnName("address_street").IsRequired().IsUnicode(false).HasMaxLength(128);
modelBuilder.Entity<Address>().Property(t => t.Address_housenumber).HasColumnName("address_housenumber");
modelBuilder.Entity<Address>().Property(t => t.Address_bus).HasColumnName("address_bus").IsUnicode(false).IsFixedLength().HasMaxLength(1);
//modelBuilder.Entity<Address>().Property(t => t.messages_id).HasColumnName("messages_id");
//modelBuilder.Entity<Address>().HasOptional(t => t.message).WithMany(t => t.addresses).HasForeignKey(d => d.messages_id);
modelBuilder.Entity<Address>()
.HasMany(t => t.Events)
.WithMany(t => t.Addresses)
.Map(m =>
{
m.ToTable("event_has_address", "geodatabase");
m.MapLeftKey("address_id");
m.MapRightKey("event_id");
});
事件控制器
// POST: /Event/Create
[HttpPost]
public ActionResult Create(EventAddressViewModel ViewModel)
{
if (ModelState.IsValid)
{
Item viewItem = ViewModel.Item;
Address viewAddress = ViewModel.Address;
Event viewEvent = ViewModel.Event;
//ADD ITEM
viewItem.ItemCreateddate = DateTime.Now;
db.Items.Add(viewItem);
//ADD EVENT
viewEvent.Item = viewItem;
db.Addresses.Add(viewAddress);
db.SaveChanges();
Address addres1 = db.Addresses.Find(viewAddress.Address_id);
viewEvent.Addresses.Add(addres1);
db.Events.Add(viewEvent);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(ViewModel);
}
在我的控制器中,在"viewEvent.Addresses.Add(address(;"行上,我得到了错误。当我把鼠标悬停在地址上时,它有正确的值,但它一直说没有设置对象引用。。。
您没有实例化ICollection
。当您尝试添加地址时,Addresses
变量为null,这将导致NullReferenceException
。将其初始化为具体类型:
if(viewEvent.Addresses == null)
{
viewEvent.Addresses = new List<Address>();
}
viewEvent.Addresses.Add(addres1);
将此行添加到失败行之上:
viewEvent.Addresses = new List<Address>();
问题是,你目前没有一个已初始化的地址列表,所以你需要先构建一个,然后再添加。