【发布时间】:2012-12-20 11:56:28
【问题描述】:
我有一个 EF 代码优先模型,其中一个表与其他表具有多个一对多关系:
public class Note
{
[Key]
public Guid Id { get; set; }
public string NoteText { get; set; }
public string Author { get; set; }
public DateTime? CreationDate { get; set; }
}
public class Foo
{
public Foo()
{
Notes = new HashSet<Note>();
}
[Key]
public Guid Id { get; set; }
public virtual ICollection<Note> Notes { get; set; }
// other properties ommited...
}
public class Bar
{
public Bar()
{
Notes = new HashSet<Note>();
}
[Key]
public Guid Id { get; set; }
public virtual ICollection<Note> Notes { get; set; }
// other properties ommited...
}
如您所见,Foo 和Bar 都有自己的Notes 列表,但Note 属于Foo 或 Bar。
在为迁移搭建脚手架时,EF 在Notes 表中为Foo 和Bar 创建了一个外键,我认为这是不正确的。相反,我希望在Foo 和Notes 之间创建一个链接表,并在Bar 和Notes 之间创建另一个链接表。
有没有办法自动做到这一点?还是我必须在我的代码优先实现中手动创建这些类?
【问题讨论】:
标签: entity-framework ef-code-first entity-framework-migrations