【发布时间】:2015-03-03 03:26:19
【问题描述】:
假设我有以下三个表:
public class Book
{
public int Id { get; set; }
public string Title { get; set; }
public virtual ICollection<BookAuthor> BookAuthors { get; set; }
}
public class Author
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public virtual ICollection<BookAuthor> AuthorBooks { get; set; }
}
public class BookAuthor
{
public int Id { get; set; }
public int BookId { get; set; }
public Book Book { get; set; }
public int AuthorId { get; set; }
public Author Author { get; set; }
}
使用以下映射配置:
public BookAuthorConfiguration()
{
// BookAuthor has a composite key:
HasKey(a => new { a.BookId, a.AuthorId });
// BookAuthor has 1 Book, Books have many BookAuthor records
HasRequired(a => a.Book)
.WithMany(s => s.BookAuthors)
.HasForeignKey(a => a.BookId)
.WillCascadeOnDelete(false);
// BookAuthor has 1 Author, Author have many BookAuthor records
HasRequired(a => a.Author)
.WithMany(p => p.AuthorBooks)
.HasForeignKey(a => a.AuthorId)
.WillCascadeOnDelete(false);
}
我现在想做的是在我的连接 BookAuthor 表中创建一本新书和一个新注册表,我不知道微风会自动做什么以及我必须自己做什么。
如果已经创建了 Author,我是否要创建两个新实体 Book 和 BookAuthor,设置 BookAuthor 的属性(BookId,AuthorId,Book,Author),然后推送新创建的 Book 实体的 BookAuthors 数组,还是我结束了干吗?
我希望我为您提供了足够的信息,如果没有让我知道缺少什么。
提前致谢!
【问题讨论】:
-
虽然不是答案,但我想知道
BookAuthor.Id属性的意义何在?当你有一个组合键{BookId, AuthorId}时,它没有明显的用途。
标签: entity-framework ef-code-first breeze