I have an EF code-first model with a table having several one-to-many relationships with other tables:
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...
}
As you can see, both Foo
and Bar
have their own list of Notes
, but a Note
belongs to either a Foo
or a Bar
.
When scaffolding the migration, EF creates a foreign key for Foo
and Bar
in the Notes
table, which I think is not correct. I would like, instead, that a link table is created between Foo
and Notes
and another one between Bar
and Notes
.
Is there a way to automatically do this? Or do I have to manually create these classes in my code-first implementation?