【发布时间】:2014-04-29 14:26:09
【问题描述】:
我一直试图弄清楚是否可以在不使用所有键的情况下在 EF 中创建关联。
以下是作者中有组合键的示例,但我在书中只有该键的 1 部分。我的问题是如何在没有所有键的情况下创建导航属性?
[Table("Book")]
public class Book {
[Key]
public int ID { get; set; }
public string AuthorLastName { get; set; }
public virtual Author Author { get; set; }
}
[Table("Author")]
public class Author {
[Key, Column(Order=0)]
public string AuthorFirstName { get; set; }
[Key, Column(Order=1)]
public string AuthorLastName { get; set; }
public virtual ICollection<Book> Books { get; set; }
}
internal class BookConfig : EntityTypeConfiguration<Book> {
public BookConfig()
{
HasRequired(hr => hr.Author)
.WithMany(wm => wm.Books)
.HasForeignKey(fk => new {
fk.AuthorLastName
});
}
}
这显然行不通,因为我没有书中的所有完整组合键以这种方式将其与作者相关联。
【问题讨论】:
标签: c# entity-framework orm navigation-properties