I have 2 tables in database: ReceivedGoods
and ReceivedGoodsProperties
ReceivedGoods
contains ReceivingId
as PK and must have its extending data in ReceivedGoodsProperties
which contains ReceivingId
as FK referencing to the ReceivedGoods
's ReceivingId
. Current ReceivedGoodsProperties
, however, has its own PK Id
and is therefore distinct from FK. So I have following:
public class ReceivedGoods
{
...
public int ReceivingId { get; set; }
...
public virtual ReceivedGoodsProperties properties { get; set; }
}
public class ReceivedGoodsProperties
{
...
public int Id { get; set; } // This is PK
public int ReceivingId { get; set; } // This is FK
...
public virtual ReceivedGoods goods { get; set; }
}
I would like to get ReceivedGoods
object and have properties automatically loaded as well but I am not able to figure out, how to set up this within EF.
I've tried something like this (from the ReceivedGoodsProperties
side mapping):
this.HasRequired(p => p.goods)
.WithRequiredDependent(d => d.properties)
.Map(m => m.MapKey("ReceivingId"));
but I am ending up with following error:
ReceivingId: Name: Each property name in a type must be unique. Property
name 'ReceivingId' is already defined.
When commenting out ReceivingId
in ReceivedGoodsProperties
, upper exception is not thrown, ReceivedGoods
is loaded correctly except the properties
property.
Can somebody explain me, how to do one-to-one mapping in situation like this?