This should be a simple question for the well versed EF user.
I have the following schema (in my head) of how the relationships between the tables should look.
[FooBar] [Foo] [Bar]
FooId PK,FK Id PK Id PK
BarId PK,FK BarId FK Name
IsRead Name Description
Description
Though, when I try to generate the schema using EF code-first it fails to interpret the relationships between the entities as I've interpreted them (adds foreign key FooId
to the [bar]
table) and fails to fully create the [FooBar]
bridge table.
If someone could guide me on how to achieve the above schema using EF4 code-first I'd appreciate it. Whether the solution involves attributes on my POCO models, fluent configurations or a hybrid of both doesn't matter much - as long as the desired database schema is created.
POCO Models:
public class Foo
{
public int Id { get; set; }
public string Text { get; set; }
public string Description { get; set; }
public int BarId { get; set; }
public Bar Bar { get; set; } /* bar entity */
public virtual ICollection<Bar> BridgedBars { get; set; }
public Foo()
{
Bars = new List<Bar>();
}
}
public class Bar
{
public int Id { get; set; }
public string Text { get; set; }
public string Description { get; set; }
public virtual ICollection<Foo> Foos { get; set; }
public virtual ICollection<Foo> BridgedFoos { get; set; }
public Bar()
{
Foos = new List<Foo>();
BridgedFoos = new List<Foo>();
}
}
public class FooBar
{
public int FooId { get; set; }
public int BarId { get; set; }
public virtual Foo Foo { get; set; }
public virtual Bar Bar { get; set; }
public bool IsRead { get; set; }
}