简体   繁体   English

一对一的实体框架具有其他属性

[英]One to many with additional properties in Entity Framework

I am new to EF and have got a very basic question. 我是EF的新手,有一个非常基本的问题。 Can someone please advise how to implement below scenario using Code First approach ? 有人可以建议使用Code First方法实施以下方案吗?

    public class Book
    {
       public int Id {get; set;}
       public string title {get; set;}
       public string author {get; set;}
       public DateTime DateBorrowed {get; set;}
       public DateTime DateReturned {get; set;}

       public virtual User Borrower {get;set;}
    }
    public class User
    {
       public int Id {get;set;}
       public string UserName {get;set;}
       public string Email {get; set;}

       public virtual ICollection<Book> Books {get; set;}
    }

I would like to maintain both the Book and User classes/underlying tables maintain a unique collection of records every time. 我想同时维护Book和User类/底层表,每次都维护唯一的记录集合。 In other words, how do we separate DateBorrowed and DateReturned properties in a separate class so that book borrowing and returning transactions would be maintained in a separate SQL table. 换句话说,我们如何在单独的类中分离DateBorrowed和DateReturned属性,以便将借书和退还交易记录保存在单独的SQL表中。

Current setup will only allow each book to have one single record of when it was borrowed and when it was returned. 当前的设置将仅允许每本书具有何时借用和何时退还的单个记录。 Every time its borrowed or returned, it will overwrite the last one. 每次借用或归还时,它将覆盖最后一个。 What you're probably looking to do is more along the lines to creating a class that holds the two dates, and then reference that class as a foreign key in your Book table. 您可能要做的更多是创建一个包含两个日期的类,然后将该类作为Book表中的外键引用。

Your current table may change like so (code not tested): 您当前的表可能会这样更改(未测试代码):

public class Book
{
   public int BookId {get; set;}
   public string title {get; set;}
   public string author {get; set;}

   public virtual ICollection<BorrowingRecord> BorrowingRecords { get; set; }

   public int UserId { get; set; }
   public virtual User Borrower {get;set;}
}

public class BorrwingRecord
{
    public int BorrowingRecordId {get; set; }
    public DateTime DateBorrowed {get; set;}
    public DateTime DateReturned {get; set;}

    public int BookId { get; set; }
    public virtual Book Book { get; set; }
}

public class User
{
   public int UserId {get;set;}
   public string UserName {get;set;}
   public string Email {get; set;}

   public virtual ICollection<Book> Books {get; set;}
}

You already have the right idea with your User table. 您已经对User表有了正确的想法。 Just set up the connections and you're good to go. 只需建立连接,您就可以开始了。

What you're looking for is a One-to-Many relationship. 您正在寻找的是一对多关系。 Consult this page for more detail on it: https://msdn.microsoft.com/en-us/data/jj713564.aspx 请查阅此页面以获取更多详细信息: https : //msdn.microsoft.com/zh-cn/data/jj713564.aspx

Here's an example of how you'd get books borrowed by a given user and the borrowing record of those books (code not tested and assumes you've set up your context): 这是一个示例,说明如何获取给定用户借的书以及这些书的借书记录(未经测试的代码,并假定您已设置上下文):

var booksForUser = context.Books.Where(b => b.UserId == GivenUserID);

foreach(var book in booksForUser)
{
    var borrowingRecordForBook = context.BorrowingRecords.Where(br => br.BookId == book.BookId);

    // do what you need with this data.
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM