【问题标题】:Entity framework 4.1 code-first mapping issue实体框架 4.1 代码优先映射问题
【发布时间】:2012-03-12 00:42:55
【问题描述】:

我正在使用实体框架 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);
}

嗯,问题很简单:你在哪里做错了?

谢谢,马丁。

【问题讨论】:

    标签: entity-framework database-design entity-framework-4.1 mapping


    【解决方案1】:

    一对一的关系在代码优先时有点特别。这是一篇关于它的好博客文章:

    http://weblogs.asp.net/manavi/archive/2011/05/01/associations-in-ef-4-1-code-first-part-5-one-to-one-foreign-key-associations.aspx

    干杯

    【讨论】:

      猜你喜欢
      • 2011-12-10
      • 1970-01-01
      • 1970-01-01
      • 2011-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多