实体框架4.1 InverseProperty属性 [英] Entity Framework 4.1 InverseProperty Attribute

查看:125
本文介绍了实体框架4.1 InverseProperty属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

只是想了解更多关于RelatedTo属性的信息,我发现它已被EF 4.1 RC中的ForeignKeyInverseProperty属性取代.

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.

有人知道有关此属性有用的场景的任何有用资源吗?

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;}
}

推荐答案

我为InversePropertyAttribute添加了一个示例.它不仅可以用于自引用实体中的关系(如Ladislav的答案中链接的示例),而且还可以用于不同实体之间关系的正常"情况:

I add an example for the 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:

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;}
}

这将描述与此Fluent代码相同的关系:

This would describe the same relationship as this Fluent Code:

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

...或...

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

现在,在上面的示例中添加InverseProperty属性是多余的:映射约定将始终创建相同的单一关系.

Now, adding the InverseProperty attribute in the example above is redundant: The mapping conventions would create the same single relationship anyway.

但是,请考虑以下示例(一个仅包含由两位作者共同撰写的书籍的图书库):

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;}
}

映射约定不会检测到这些关系的哪些末端在一起,而是实际创建四个关系(在Books表中具有四个外键).在这种情况下,使用InverseProperty将有助于定义我们希望在模型中建立的正确关系:

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). In this situation using the InverseProperty would help to define the correct relationships we want in our model:

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;}
}

在这里,我们只会得到两个关系. (注意:InverseProperty属性仅在关系的一端是必需的,我们可以在另一端省略该属性.)

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.)

这篇关于实体框架4.1 InverseProperty属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆