I have a join table with an additional field relevant to the join:
VirtualDatastreamId INT NOT NULL
DatastreamId INT NOT NULL
FormulaElement INT NOT NULL
All columns are the primary key.
The POCO this is mapped to is:
public class VirtualDatastreamMap
{
public virtual Datastream Datastream { get; set; }
public virtual VirtualDatastream VirtualDatastream { get; set; }
public virtual string FormulaElement { get; set; }
}
I do not have foreign key properties in the model as, to me, the concept of a foreign key is an implementation detail and should be encapsulated by the relationships specified by the class. I'd prefer not to add these if possible.
How do I map this with Fluent API? I was hoping for something like the below (which unfortunately doesn't work):
modelBuilder.Entity<VirtualDatastreamMap>()
.HasKey(vdm =>
new
{
DatastreamId = vdm.Datastream.Id,
VirtualDatastreamId = vdm.VirtualDatastream.Id,
FormulaElement = vdm.FormulaElement
});
The field names of the anonymous type have to be named as an object cannot have two fields with the same name.
I see from this question that this issue was raised in the answer but there doesn't seem to be anything further.
If it's not possible, which is a better option?
- Add the foreign key properties to the model
- Add an
Id
field to the database and the model
Thanks