I like to store a collection in database, which contains a set of available "Wheels". A car class reference to these wheels.
public class Car
{
private ICollection<Wheel> _wheels;
public ICollection<Wheel> Wheels
{
get { return _wheels; }
set { _wheels = value; }
}
}
public class Wheel
{
public enum position
{
FrontRight,
FrontLeft,
BackRight,
BackLeft,
}
[Key]
public int ID { get; set; }
}
If I use the entity framework the foreignkey will be stored in the Wheels-DataBase Table. Which is not the wished result, because I have multiple Cars and foreach car a new entry in Wheels will be created with the same content. (Waste of storage space)
Wheels
-------
<PK>ID:Integer
<FK>Car_ID:Integer
Cars
-----
<PK>ID:Integer
So I tried another solution by forcing the creation of a third table for the Car-Wheel relationship. So the ID's will be stored in the corresponding Car_Wheel table.
I added a property to the Wheel-class for focusing a many-many-relationship.
public virtual ICollection<MonitoringTask> RelatedCars { get; set; }
Wish creates the table scheme:
Wheels
-------
<PK>ID:Integer
Cars
-----
<PK>ID:Integer
Car_Wheel
-------
<PK><FK> ID_Car:Integer
<PK><FK> ID_Wheel:Integer
???So this looks good, but I'm searching for a solution which does not require the change of the Wheel-class.???