Is it possible to create a One-to-Many relationship with Code First that uses a link/join table between them?
public class Foo {
public int FooId { get; set; }
// ...
public int? BarId { get; set; }
public virtual Bar Bar { get; set; }
}
public class Bar {
public int BarId { get; set; }
// ...
public virtual ICollection<Foo> Foos { get; set; }
}
I want this to map as follows:
TABLE Foo
FooId INT PRIMARY KEY
...
TABLE Bar
BarId INT PRIMARY KEY
TABLE FooBar
FooId INT PRIMARY KEY / FOREIGN KEY
BarId INT FOREIGN KEY
With this, I would have the ability to make sure Foo only has one Bar, but that Bar could be re-used by many different Foos.
Is this possible with Entity Framework? I would prefer to not have to put the key in Foo itself because I don't want a nullable foreign key. If it is possible please provide an example using the Fluent API and not the data annotations.