[英]How to create the sequence and apply two tables in SQL Server using Entity framework 6 code first approach?
[英]How handle these two models in code first approach using Entity Framework 7?
我正在编写将模拟库的概念ASP.NET 5 MVC6的证明。 我正在使用CTP6和beta3。
如何使用代码优先方法在Entity Framework 7中定义它们的关系?
货架和书籍需要连接表。 在我看来,这个应用程序将一次考虑一个架子和自己的书籍。 换句话说,一个书架与书籍有一对多的关系。
货架型号:
public class Shelf
{
public int ShelfId { get; set; }
public string ShelfName { get; set; }
public ShelfType MyProperty { get; set; }
public virtual List<Book> Books { get; set; }
public DateTime Created { get; set; }
public string CreatedBy { get; set; }
public DateTime Updated { get; set; }
public string UpdatedBy { get; set; }
}
书籍型号:
public class Book
{
public int BookId { get; set; }
public string BookName { get; set; }
public string BookAuthor { get; set; }
public string BookGenre { get; set; }
public DateTime BookPublishedDate { get; set; }
public string Description { get; set; }
public DateTime Created { get; set; }
public string CreatedBy { get; set; }
public DateTime Updated { get; set; }
public string UpdatedBy { get; set; }
}
我想实现像这样在ASP.NET 5.由于TPH和其他功能没有在EF7还没有实现,我是在正确的轨道上?
换句话说,我在想以下几点:
我无法让以下工作如下所示:
modelBuilder.Entity<Shelf>()
.HasMany(p => p.Shelfs)
.WithMany(t => t.Books)
.Map(mc =>
{
mc.ToTable("ShelfJoinBook");
mc.MapLeftKey("ShelfId");
mc.MapRightKey("BookId");
});
感谢您抽出宝贵时间阅读我的问题。 我希望其他人觉得这很有用。
要回答有关多对多的问题,您应该按如下方式定义映射(并且必须更新模型类):
modelBuilder.Entity<Shelf>()
.HasMany(p => p.Books)
.WithMany(t => t.Shelves) // doesn't exist
.Map(mc =>
{
mc.ToTable("BookShelves");
mc.MapLeftKey("ShelfId");
mc.MapRightKey("BookId");
});
这需要你添加一个ICollection<Shelf> Shelves { get; set; }
ICollection<Shelf> Shelves { get; set; }
ICollection<Shelf> Shelves { get; set; }
来Book
。 但是,如果您这样做,那么您将拥有以下内容(这是我的示例的MCVE版本):
public class Shelf
{
public int ShelfId { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
public class Book
{
public int BookId { get; set; }
public virtual ICollection<Shelf> Shelves { get; set; }
}
然后您不再需要映射,因为EF会按惯例为您生成所有关系,包括仅在数据库本身中可见的连接表。
但是 - 从您的实际模型类和围绕它们的讨论 - 看起来您真的只需要一对多,在这种情况下,您应该接受Pynt的答案,并忽略我的。
编辑
正如Gert Arnold在评论中所指出的那样,EF7目前(不再是?)支持生成的连接表,这意味着我们必须自己动手,这无论如何都可能更好 。
这是我认为我可以从现在开始使用我自己的简单连接表的一种方式,如果EF不再为我做这些(警告 - 我只是想在这个问题上关闭循环......我不是打扰在编译器中尝试这个,所以YMMV):
public class Shelf
{
public int ShelfId { get; set; }
public virtual ICollection<BookShelf> BookShelves { get; set; }
public virtual IQueryable<Book> Books
{
get
{
return BookShelves.Where(s => s.ShelfId == ShelfId)
.Select(s => s.Book);
}
}
}
public class Book
{
public int BookId { get; set; }
public virtual ICollection<BookShelf> BookShelves { get; set; }
public virtual IQueryable<Shelf> Shelves
{
get
{
return BookShelves.Where(s => s.BookId == BookId)
.Select(s => s.Shelf);
}
}
}
public class BookShelf
{
[Key]
public int BookId { get; set; }
[ForeignKey("BookId")]
public Book Book { get; set; }
[Key]
public int ShelfId { get; set; }
[ForeignKey("ShelfId")]
public Shelf Shelf { get; set; }
}
我解决了类似的情况,尽管它可能不符合您的需求。 我使用数据注释,因为我没有费心去学习如何通过lambda映射设置外键。 通过以下设置,您可以为每个书架设置一个书籍实例,并且书架可以包含许多书籍。 我已将关系属性移动到类的底部以便于可视化。
货架型号:
public class Shelf
{
public int ShelfId { get; set; }
public string ShelfName { get; set; }
public ShelfType MyProperty { get; set; }
public DateTime Created { get; set; }
public string CreatedBy { get; set; }
public DateTime Updated { get; set; }
public string UpdatedBy { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
书籍型号:
using System.ComponentModel.DataAnnotations.Schema;
public class Book
{
public int BookId { get; set; }
public string BookName { get; set; }
public string BookAuthor { get; set; }
public string BookGenre { get; set; }
public DateTime BookPublishedDate { get; set; }
public string Description { get; set; }
public DateTime Created { get; set; }
public string CreatedBy { get; set; }
public DateTime Updated { get; set; }
public string UpdatedBy { get; set; }
public int ShelfId { get; set; }
[ForeignKey("ShelfId")]
public Shelf Shelf { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.