实体框架4.1 InverseProperty属性

Kam*_*yar 37 c# data-annotations entity-framework-4.1

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

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

我应该在导航属性上使用此属性吗?例:

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;}
}
Run Code Online (Sandbox Code Playgroud)

Sla*_*uma 80

我为此添加了一个示例InversePropertyAttribute.它不仅可以用于自引用实体中的关系(如在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;}
}
Run Code Online (Sandbox Code Playgroud)

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

modelBuilder.Entity<Book>()
            .HasOptional(b => b.Author)
            .WithMany(a => a.Books);
Run Code Online (Sandbox Code Playgroud)

... 要么 ...

modelBuilder.Entity<Author>()
            .HasMany(a => a.Books)
            .WithOptional(b => b.Author);
Run Code Online (Sandbox Code Playgroud)

现在,InverseProperty在上面的示例中添加属性是多余的:映射约定无论如何都会创建相同的单个关系.

但请考虑这个例子(一个只包含两位作者共同撰写的书籍的图书馆):

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;}
}
Run Code Online (Sandbox Code Playgroud)

映射约定不会检测这些关系的哪些端点属于一起并实际创建四个关系(Books表中有四个外键).在这种情况下使用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;}
}
Run Code Online (Sandbox Code Playgroud)

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

  • 大声笑!但不知何故,这个属性是在约定和Fluent映射之间的无人区域:对于简单的关系,它不是必需的,对于复杂的关系,每个人都将使用Fluent代码.可能之所以它如此罕见的原因和唯一的解释我能想到甚至为什么*你*不知道:) (7认同)
  • 我一直在谷歌上搜索一个星期.你是男人中的神. (3认同)

Lad*_*nka 28

ForeignKey属性对具有导航属性的FK属性.它可以放在FK属性或导航属性上.它相当于HasForeignKey流畅的映射.

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

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

    public virtual OtherEntity Navigation { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

InverseProperty用于定义两端的自引用关系和配对导航属性.检查此问题以获取样本.

  • 哈!又是你了!谢谢EF大师! (2认同)