实体框架有很多很多问题 [英] Entity framework many to many problem
本文介绍了实体框架有很多很多问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
i首先在实体框架代码中向我的上下文添加新实体有问题
问题是当我想添加新作者预订或预订时作者说:
对象引用未设置为对象的实例。因为书的作者或作者的书是空的...而我设定的那个。
这里是我的代码:
< span class =code-keyword> class 计划
{
静态 void Main( string [] args)
{
var db = new Source();
发布商发布商= db.Publisher。单(p = > p.Title == 按);
Book book = new Book(){ISBN = 123424,Publisher = publisher,Title = .NET简介 };
book.Authors.Add( new 作者(){FirstName = Bill,LastName = Gates});
db.Books.Add(book);
db.SaveChanges();
}
}
public class 来源: DbContext
{
public Source(): base ( Main)
{
}
< span class =code-keyword> public DbSet< Publisher>发布商{获取; set ; }
public DbSet< Author>作者{获取; set ; }
public DbSet< Book>图书{获取; set ; }
受保护 覆盖 void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity< Publisher>()。HasKey(p = > p.ID );
modelBuilder.Entity< Book>()。Property(p = > p.Title).HasMaxLength( 200 跨度>);
}
}
public class Publisher
{
public long ID {获得跨度>; set ; }
public string 标题{获得跨度>; set ; }
public string 电子邮件{ get 跨度>; set ; }
public string 地址{ get 跨度>; set ; }
}
public class 预订
{
public long ID { get ; set ; }
public string 标题{获得跨度>; set ; }
public string ISBN { get 跨度>; set ; }
public 列表<作者>作者{获取; set ; }
public Publisher Publisher { get ; set ; }
}
public class 作者
{
public long ID { get ; set ; }
public string FirstName {获得跨度>; set ; }
public string LastName { get 跨度>; set ; }
public 列表< Book>图书{获取; set ; }
}
解决方案
Hello RedSakura,
I在这里纠正了你的代码。你错过了在作者类中创建书籍的新对象。
注意:我已经检查过静态数据,而不是数据库。这是完整的工作代码。
var db = new 来源(){作者= 新列表<作者>(),发布商= 新列表<发布商>(),图书= 新列表<图书>()};
发布商发布商= 新发布商(){标题= 按,地址= India,Email = this@gmail.com,ID = 1 } ;
db.Publisher.Add(publisher);
publisher = db.Publisher。 Single (p = > p.Title == < span class =code-string> 按);
Book book = new Book(){ISBN = 123424,Publisher = publisher,Title = .NET简介 ,Authors = new List< Author>()};
book.Authors.Add( new 作者(){FirstName = Bill,LastName = Gates});
db.Books.Add(book);
谢谢,
Imdadhusen
hi,
i have problem on adding new entities to my context in entity framework code first
problem is that when i want to add new author to book or book to author is says :
"Object reference not set to an instance of an object." because author of book or book of author is null... while i`v set that.
here is my code:
class Program
{
static void Main(string[] args)
{
var db = new Source();
Publisher publisher = db.Publisher.Single( p=> p.Title == "Press");
Book book = new Book() { ISBN = "123424", Publisher = publisher, Title = "Intro to .NET" };
book.Authors.Add(new Author() { FirstName = "Bill", LastName = "Gates"});
db.Books.Add(book);
db.SaveChanges();
}
}
public class Source: DbContext
{
public Source(): base("Main")
{
}
public DbSet<Publisher> Publisher { get; set; }
public DbSet<Author> Authors { get; set; }
public DbSet<Book> Books { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Publisher>().HasKey(p => p.ID);
modelBuilder.Entity<Book>().Property(p => p.Title).HasMaxLength(200);
}
}
public class Publisher
{
public long ID { get; set; }
public string Title { get; set; }
public string Email { get; set; }
public string Address { get; set; }
}
public class Book
{
public long ID { get; set; }
public string Title { get; set; }
public string ISBN { get; set; }
public List<Author> Authors { get; set; }
public Publisher Publisher { get; set; }
}
public class Author
{
public long ID { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public List<Book> Books { get; set; }
}
解决方案
Hello RedSakura,
I have corrected your code here. you have missed creating new object of Books in Author class.
Note: I have checked with static data only not with Database. and this is complete working code.
var db = new Source() { Authors = new List<Author>(), Publisher = new List<Publisher>(), Books = new List<Book>() }; Publisher publisher = new Publisher() { Title = "Press", Address = "India", Email = "this@gmail.com", ID = 1 }; db.Publisher.Add(publisher); publisher = db.Publisher.Single(p => p.Title == "Press"); Book book = new Book() { ISBN = "123424", Publisher = publisher, Title = "Intro to .NET", Authors = new List<Author>() }; book.Authors.Add(new Author() { FirstName = "Bill", LastName = "Gates" }); db.Books.Add(book);
Thanks,
Imdadhusen
这篇关于实体框架有很多很多问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文