我有两个表想用 EntityFramework 查询,使用一个主表,这是第二个用于存储辅助数据的表,我希望我可以做一个左连接,但我做不到,以下是元素:
public class TableA
{
public int Id {get; set;}
public string Value {get;} set
}
public class OptionalDataTableB
{
public int OptionalDataTableBId {get; set;}
public string OtherValue {get; set;}
}
这里是映射类
public class TableAMap: EntityTypeConfiguration<TableA>
{
public TableAMap ()
{
this.HasKey (e => e.Id);
this.Property (e => e.Id)
. IsRequired ()
.HasDatabaseGeneratedOption(System.ComponentModel.DataAnnotations.Schema.DatabaseGeneratedOption.Identity);
}
}
public class OptionalDataTableBMap: EntityTypeConfiguration<OptionalDataTableB>
{
public OptionalDataTableB ()
{
this.HasKey (e => e.OptionalDataTableBId);
this.Property (e => e.OptionalDataTableBId)
. IsRequired ()
}
}
我想与 DbContext 建立关系
public class MyDbContext: DbContext
{
protected override void OnModelCreating (DbModelBuilder ModelBuilder)
{
modelBuilder.Configurations.Add (new Mapping.TableAMap());
modelBuilder.Configurations.Add (new Mapping.OptionalDataTableBMap());
}
}
如何配置映射才能生成这种请求?
select *
from TableA
left join OptionalDataTableB On TableA.Id = OptionalDataTableB.OptionalDataTableBId
MSDN 的以下内容应该可以回答您的问题:http://msdn.microsoft.com/en-us/data/hh134698.aspx