I am in the middle of migrating from Linq2Sql to Entity Framework and I got really stuck on one thing.
I have one table MappingSet. Which has a PK, MappingSetId. Then I also have a table XpathMappingSet which has the same PK, MappingSetId. It is both an FK and a PK.
In then I have a property on MappingSet which is called XpathMappingSet. This is null of there is no corresponding row in the XpathMappingSet table.
How can I configure this? I tried the following, but that didn’t really work:
HasOptional(m => m.XpathMappingSet).WithRequired(m => m.MappingSet).Map(m => m.MapKey("MappingSetId"));
The exception I got was:
(66,6) : error 0019: Each property name in a type must be unique. Property name ‘MappingSetId’ was already defined.
This all worked well in L2S but I cannot get it to work using Entity Framework.
MappingSet
public partial class MappingSet : BaseEntity
{
public int MappingSetId { get; set; }
public virtual XpathMappingSet XpathMappingSet { get; set; }
}
XpathMappingSet
public partial class XpathMappingSet
{
public int MappingSetId { get; set; }
public virtual MappingSet MappingSet { get; set; }
}
Mapping of XpathMappingSet
public partial class XpathMappingSetMap : EntityTypeConfiguration<XpathMappingSet>
{
public XpathMappingSetMap()
{
ToTable("XpathMappingSets");
HasKey(m => m.MappingSetId);
}
}
Mapping of MappingSet
public partial class MappingSetMap : EntityTypeConfiguration<MappingSet>
{
public MappingSetMap()
{
ToTable("MappingSets");
HasKey(m => m.MappingSetId);
HasOptional(m => m.XpathMappingSet).WithRequired(m => m.MappingSet).Map(m=>m.MapKey("MappingSetId"));
}
}
Okay, I solved it.
I changed the mapping for xpathmappingset to:
it didn’t like the fact that it had the same name