使用流畅的模型构建器进行多对多关系

时间:2019-06-26 12:29:50

标签: entity-framework fluent

如何首先使用代码为这些实体建立多对多关系?实体是Book,Customer和Borrow,其中Borrow是多对多结。

public class Borrow
{
    public int Id { get; set; }
    public int BookId { get; set; }
    public int CustomerId { get; set; }
    public DateTime BorrowDate { get; set; }
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime ExpectedReturnDate { get; set; }
    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    public DateTime ActualReturnDate { get; set; }
    public virtual Book Book { get; set; }
    public virtual Customer Customer { get; set; }
}

客户:

public class Customer
{
    public int Id { get; set; }
    public string Name { get; set; }
    public virtual List<Borrow> Borrows { get; set; }
}

图书:

public class Book
{
    public int Id { get; set; }
    public string Title { get; set; }
    public Guid SerialId { get; set; }
    public virtual List<Borrow> Borrows { get; set; }
}

模型制作器:

public class LibraryDatabase: DbContext
{
    public DbSet<Book> Books { get; set; }
    public DbSet<Borrow> Borrows { get; set; }
    public DbSet<Customer> Customers { get; set; }
    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {

    }

}

0 个答案:

没有答案