简体   繁体   English

实体框架4.1 InverseProperty属性

[英]Entity Framework 4.1 InverseProperty Attribute

Just wanted to know more about RelatedTo attribute and I found out it has been replaced by ForeignKey and InverseProperty attributes in EF 4.1 RC. 只是想了解更多有关RelatedTo属性的信息,我发现它已被EF 4.1 RC中的ForeignKeyInverseProperty属性所取代。

Does anyone know any useful resources about the scenarios that this attribute becomes useful? 有没有人知道有关此属性变得有用的场景的任何有用资源?

Should I use this attribute on navigation properties? 我应该在导航属性上使用此属性吗? example: 例:

public class Book
{
  public int ID {get; set;}
  public string Title {get; set;}

  [ForeignKey("FK_AuthorID")]
  public Author Author {get; set;}
}  

public class Author
{
  public int ID {get; set;}
  public string Name {get; set;}
  // Should I use InverseProperty on the following property?
  public virtual ICollection<Book> Books {get; set;}
}

I add an example for the InversePropertyAttribute . 我为InversePropertyAttribute添加了一个示例。 It cannot only be used for relationships in self referencing entities (as in the example linked in Ladislav's answer) but also in the "normal" case of relationships between different entities: 它不仅可以用于自引用实体中的关系(如在Ladislav的答案中链接的示例中),还可以用于不同实体之间关系的“正常”情况:

public class Book
{
    public int ID {get; set;}
    public string Title {get; set;}

    [InverseProperty("Books")]
    public Author Author {get; set;}
}

public class Author
{
    public int ID {get; set;}
    public string Name {get; set;}

    [InverseProperty("Author")]
    public virtual ICollection<Book> Books {get; set;}
}

This would describe the same relationship as this Fluent Code: 这将描述与此Fluent代码相同的关系:

modelBuilder.Entity<Book>()
            .HasOptional(b => b.Author)
            .WithMany(a => a.Books);

... or ... ... 要么 ...

modelBuilder.Entity<Author>()
            .HasMany(a => a.Books)
            .WithOptional(b => b.Author);

Now, adding the InverseProperty attribute in the example above is redundant: The mapping conventions would create the same single relationship anyway. 现在,在上面的示例中添加InverseProperty属性是多余的:映射约定无论如何都会创建相同的单个关系

But consider this example (of a book library which only contains books written together by two authors): 但请考虑这个例子(一个只包含两位作者共同撰写的书籍的图书馆):

public class Book
{
    public int ID {get; set;}
    public string Title {get; set;}

    public Author FirstAuthor {get; set;}
    public Author SecondAuthor {get; set;}
}

public class Author
{
    public int ID {get; set;}
    public string Name {get; set;}

    public virtual ICollection<Book> BooksAsFirstAuthor {get; set;}
    public virtual ICollection<Book> BooksAsSecondAuthor {get; set;}
}

The mapping conventions would not detect which ends of these relationships belong together and actually create four relationships (with four foreign keys in the Books table). 映射约定不会检测这些关系的哪些端点属于一起并实际创建四个关系 (Books表中有四个外键)。 In this situation using the InverseProperty would help to define the correct relationships we want in our model: 在这种情况下,使用InverseProperty将有助于在我们的模型中定义我们想要的正确关系:

public class Book
{
    public int ID {get; set;}
    public string Title {get; set;}

    [InverseProperty("BooksAsFirstAuthor")]
    public Author FirstAuthor {get; set;}
    [InverseProperty("BooksAsSecondAuthor")]
    public Author SecondAuthor {get; set;}
}

public class Author
{
    public int ID {get; set;}
    public string Name {get; set;}

    [InverseProperty("FirstAuthor")]
    public virtual ICollection<Book> BooksAsFirstAuthor {get; set;}
    [InverseProperty("SecondAuthor")]
    public virtual ICollection<Book> BooksAsSecondAuthor {get; set;}
}

Here we would only get two relationships . 在这里,我们只会得到两个关系 (Note: The InverseProperty attribute is only necessary on one end of the relationship, we can omit the attribute on the other end.) (注意: InverseProperty属性仅在关系的一端是必需的,我们可以省略另一端的属性。)

ForeignKey attribute pairs FK property with navigation property. ForeignKey属性将FK属性与navigation属性配对。 It can be placed either on FK property or navigation property. 它可以放在FK属性或导航属性上。 It is equivalent of HasForeignKey fluent mapping. 它相当于HasForeignKey流畅的映射。

public class MyEntity
{
    public int Id { get; set; }

    [ForeignKey("Navigation")]
    public virtual int NavigationFK { get; set; }

    public virtual OtherEntity Navigation { get; set; }
}

InverseProperty is used for defining self referencing relation and pairing navigation properties on both ends. InverseProperty用于定义两端的自引用关系和配对导航属性。 Check this question for sample . 检查此问题以获取样本

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

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