【问题标题】:What's the Entity Framework equivalent of NHibernate's References(x => x.ResidenceCountry).Column("ResidingInCountryId")?NHibernate 的 References(x => x.ResidenceCountry).Column("ResidingInCountryId") 的实体框架等价物是什么?
【发布时间】:2011-05-11 09:27:33
【问题描述】:

我有这个代码:

class Country
{
    public int CountryId { get; set; }
    public string CountryName { get; set; }
}


class Employee
{
    public int EmployeeId { get; set; }
    public string EmployeeName { get; set; }
    public int ResidingInCountryId { get; set; }
    public virtual Country ResidenceCountry { get; set; }
}

我应该在 OnModelCreating 上添加什么?所以我可以从 Employee 导航 Country 的 CountryName

【问题讨论】:

  • 您是否使用 EF 4.1 代码优先?
  • 是的,我首先使用带有代码的 EF 4.1。我更新了我的问题。我认为virtual Country ResidenceCountry 需要手动映射。 ResidenceCountry 属性的实际后备数据库字段是 ResidingInCountryId
  • 我已经更新了我的答案,希望对您有所帮助。我自己还在学习这些东西。

标签: entity-framework mapping


【解决方案1】:

我直接从this blog posting 引用Ian Nelson,但我认为这是您需要的:


以下是在 Fluent NHibernate 中为单向一对多关系重命名外键的方法:

References(x => x.AudioFormat).Column("AudioFormat");

虽然在实体框架代码优先,但等效的是:

HasOptional(x => x.AudioFormat)  
    .WithMany()   
    .IsIndependent()   
    .Map(m => m.MapKey(a => a.Id, "AudioFormat"));  

【讨论】:

  • EF 4.1 上不再有 IsIndependent 方法。我可以使用这个stackoverflow.com/questions/5448979/… 来解决我的问题,但是 EF 的 POCO 不再简单了 :-) 我更喜欢第二种方法(外键在课堂上,而不是在原始类型上)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-11-04
  • 2015-04-07
  • 2010-11-27
  • 2020-10-05
相关资源
最近更新 更多