我正在使用实体框架4.1和代码优先映射。
public class Account
{
public Int AccountId {get;set};
public string Name {get;set};
public int? ProfileId {get;set;}
public virtual Profile {get;set;}
}
public class Profile
{
public int ProfileId {get;set;}
public DateTime Created {get;set;}
public virtual Account {get;set;} // navigation back to account
}
public AccountMapper()
{
ToTable("..")
HasKey(x => x.AccountId);
HasOptional(x => x.Profile).WithRequired(x => x.Account) // invalid column exception
// Try HasOptional(x => x.Profile).WithRequired(x => x.Account).Map(x => x.MapKey("ProfileId")) // rror 0019: Each property name in a type must be unique. Property name 'ProfileId' was already defined.
}
public ProfileMappeR()
{
ToTable("..")
HasKey(x => x.ProfileId);
}
嗯,问题很简单:你在哪里做错了?
谢谢,马丁。
一对一的关系有点特殊,首先是代码。这是一篇很好的博客文章:
干杯