实体框架中基于多键(条件)的一对多关系

时间:2021-12-22 20:24:27

So I have

所以我有

class User
{
   ...
   public virtual ICollection<Book> AlreadyRead {get; set;} 
}

class Book
{
   ...
   public virtual User Owner {get; set;}
   public bool AlreadyRead {get; set;}
}

So what I need is that User.AlreadyRead return me Books by this User, where AlreadyRead == true

所以我需要的是User.AlreadyRead返回给我这个用​​户的书籍,其中AlreadyRead == true

I tryed this bindings

我试过这个绑定

modelBuilder.Entity<Book>()
            .HasRequired(b => b.Owner)
            .WithMany(u => u.AlreadyRead)

But in that way User.AlreadyRead return all books, but I need only books with AlreadyRead == true;

但就这样,User.AlreadyRead会返回所有书籍,但我只需要AlreadyRead == true的书籍;

Is it possible to solve this problem using mapping, but not additional property with Where logic?

是否可以使用映射解决此问题,但不能使用Where逻辑解决此问题?

I just need to be able to add condition to the mapping which will check AlreadyRead == true

我只需要能够在映射中添加条件,这将检查AlreadyRead == true

1 个解决方案

#1


2  

Sorry, but I'm afraid is not possible include a condition in a relationship configuration. But, as a partial solution, I suggest you add a NotMapped property to your User class. Your model would be like this:

对不起,但我担心不可能在关系配置中包含条件。但是,作为部分解决方案,我建议您在User类中添加NotMapped属性。你的模型是这样的:

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

  [NotMapped] 
  public IEnumerable<Book> AlreadyReadedBooks 
  { 
     get 
     { 
        return Books.Where(b=>b.AlreadyRead); 
     } 
  }
}

public class Book
{
  ...
  public virtual User Owner {get; set;}
  public bool AlreadyRead {get; set;}
}

And your relationship configuration would be like this:

你的关系配置是这样的:

modelBuilder.Entity<Book>()
        .HasRequired(b => b.Owner)
        .WithMany(u => u.Books);

Update:

The navegation properties are used to represent relationships between tables and to provide a way to navigate an association between two entity types. In your case, you are stablishing an one-to-many relationship between Books and Users, and it works because a foreign key is defined on the table that represents the many end of the relationship (Books). When you stablish a relationship between two tables in a relational database, you can't add a restriction as you want in the relationship, the same happens in Entity Framework when you declare navegation properties. That's way, if you need the readed books of an specific user, you need to do a query.

navegation属性用于表示表之间的关系,并提供导航两个实体类型之间的关联的方法。在您的情况下,您正在建立书籍和用户之间的一对多关系,并且它的工作原理是因为在表格上定义了一个外键,代表了关系的多端(书籍)。在关系数据库中建立两个表之间的关系时,无法在关系中添加限制,在声明navegation属性时,实体框架中也会出现相同的情况。这样,如果您需要特定用户的书籍,您需要进行查询。

#1


2  

Sorry, but I'm afraid is not possible include a condition in a relationship configuration. But, as a partial solution, I suggest you add a NotMapped property to your User class. Your model would be like this:

对不起,但我担心不可能在关系配置中包含条件。但是,作为部分解决方案,我建议您在User类中添加NotMapped属性。你的模型是这样的:

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

  [NotMapped] 
  public IEnumerable<Book> AlreadyReadedBooks 
  { 
     get 
     { 
        return Books.Where(b=>b.AlreadyRead); 
     } 
  }
}

public class Book
{
  ...
  public virtual User Owner {get; set;}
  public bool AlreadyRead {get; set;}
}

And your relationship configuration would be like this:

你的关系配置是这样的:

modelBuilder.Entity<Book>()
        .HasRequired(b => b.Owner)
        .WithMany(u => u.Books);

Update:

The navegation properties are used to represent relationships between tables and to provide a way to navigate an association between two entity types. In your case, you are stablishing an one-to-many relationship between Books and Users, and it works because a foreign key is defined on the table that represents the many end of the relationship (Books). When you stablish a relationship between two tables in a relational database, you can't add a restriction as you want in the relationship, the same happens in Entity Framework when you declare navegation properties. That's way, if you need the readed books of an specific user, you need to do a query.

navegation属性用于表示表之间的关系,并提供导航两个实体类型之间的关联的方法。在您的情况下,您正在建立书籍和用户之间的一对多关系,并且它的工作原理是因为在表格上定义了一个外键,代表了关系的多端(书籍)。在关系数据库中建立两个表之间的关系时,无法在关系中添加限制,在声明navegation属性时,实体框架中也会出现相同的情况。这样,如果您需要特定用户的书籍,您需要进行查询。