我正在从一个EDMX映射到EF 4.1 DbContext和Fluent映射,我想映射一个字符串外键和外国对象使用流利的API。我有一个雇员与一个可选的办公室。我想要Employee类中的OfficeId和Office对象(这是全部只读的,我不需要能够保存这些对象)。具有int键的对象工作正常,但我已经尝试了几个字符串键并获得相同的结果 - OfficeId字段填充,但Office对象返回为空。在SQL分析器中查找数据正在被查询,但Office对象没有被填充。实体框架4.1 Fluent映射外键和外键对象与字符串键
public partial class Employee
{
public int Id { get; set; }
// snip irrelevant properties
public Office Office { get; set; } // this is (incorrectly) always null
public string OfficeId { get; set; }
public WorkGroup WorkGroup { get; set; } // this one with the int key is fine
public int? WorkGroupId { get; set; }
// snip more properties
}
public partial class Office
{
public string Id { get; set; }
public string Description { get; set; }
}
public partial class WorkGroup
{
public int Id { get; set; }
public string Code { get; set; }
}
从下面拉吉斯拉夫反馈后,我映射像这样在OnModelCreating
modelBuilder.Entity<Employee>().HasKey(d => d.Id).ToTable("Employee", "ExpertQuery");
modelBuilder.Entity<Office>().HasKey(d => d.Id).ToTable("Office", "ExpertQuery");
modelBuilder.Entity<WorkGroup>().HasKey(d => d.Id).ToTable("WorkGroup", "ExpertQuery");
modelBuilder.Entity<Employee>()
.HasOptional(a => a.Office)
.WithMany()
.Map(x => x.MapKey("OfficeId")); // this one does not work
modelBuilder.Entity<Employee>()
.HasOptional(e => e.WorkGroup)
.WithMany()
.HasForeignKey(e => e.WorkGroupId); // this one works fine
我认为有一些微妙与我丢失的字符串键?我查询,如下所示:
var employees = expertEntities.Employees.Include("Office").Include("WorkGroup").Take(10).ToList();
如果我从员工省略OfficeId现场,并成立了这样的映射:
modelBuilder.Entity<Employee>()
.HasOptional(e => e.BusinessEntity)
.WithMany()
.Map(x => x.MapKey("OfficeId"));
然后Office对象被填充,但我需要的OfficeId Employee对象中的字段。
谢谢,这帮助我进一步细化 - int键工作正常,但字符串键似乎不起作用。我添加了几个检查,并且int键很好,并且字符串键总是产生一个空对象 – MarkGr