通过 fluent API 公开外键
Expose Foreign key through fluent API
好的,所以我有很多对象,但是举个例子,它只有 2 个对象,Company 和 People,为了简单的例子,我已经删除了大部分道具:
public class Company {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public List<People> Peoples { get; set; }
[StringLength(100)]
[Required]
public string Bname { get; set; }
}
和
public class People {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string FName { get; set; }
[StringLength(50)]
public string LName { get; set; }
[StringLength(50)]
}
我不喜欢使用数据注释,所以我可以将我的地图分开放置。
我的简化映射是:
class CompanyMap : EntityTypeConfiguration<Company> {
public CompanyMap() {
HasMany(p => p.Peoples)
.WithRequired();
}}
它似乎工作得很好,人们在 db 中得到了一个 fk 到公司,EF 可以插入和拉回数据,如果我用 lambda 做的话我可以查询它:
var tmp = db.Companies.Include("Peoples")
但如果我尝试用 linq 查询它,并将它们连接起来:
var tmp2 = from c in db.Companies
join p in db.Person
on c.Id equals p.
这就是我的问题,People 对象没有从 db 公开它的外键,所以我不能像这样连接它们。
所以我的问题是,我可以将流利的 api 创建的 fk 公开给我的对象模型,以便我可以对其进行 linq 吗?
或者我是否应该使用 lambda,并以某种方式将其映射到我的视图模型,这样它就不会为该视图生成不需要的列?
首先,添加 FK 属性 (CompanyId
) 和 导航 属性 (Company
) 您的 People
实体:
public class People {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
//...
public int CompanyId {get;set}
public virtual Company Company{get;set;}
}
然后,将 Fluent Api 关系配置移动到 PeopleMap
class 并以这种方式修改该配置(也将 FK 属性 映射为与在 People
table):
中有 FK 列
public class PeopleMap : EntityTypeConfiguration<People> {
public CompanyMap() {
Property(p => p.CompanyId)
.HasColumnName("CompanyId");//remember change the name for the real FK column
HasRequired(p=>p.Company)
.WithMany(c=>c.Peoples)
.HasForeignKey(p=>p.CompanyId);
}
}
之后,您应该可以在查询中使用 CompanyId
FK 属性:
var tmp2 = from c in db.Companies
join p in db.Person on c.Id equals p.CompanyId
//...
好的,所以我有很多对象,但是举个例子,它只有 2 个对象,Company 和 People,为了简单的例子,我已经删除了大部分道具:
public class Company {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public List<People> Peoples { get; set; }
[StringLength(100)]
[Required]
public string Bname { get; set; }
}
和
public class People {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public string FName { get; set; }
[StringLength(50)]
public string LName { get; set; }
[StringLength(50)]
}
我不喜欢使用数据注释,所以我可以将我的地图分开放置。 我的简化映射是:
class CompanyMap : EntityTypeConfiguration<Company> {
public CompanyMap() {
HasMany(p => p.Peoples)
.WithRequired();
}}
它似乎工作得很好,人们在 db 中得到了一个 fk 到公司,EF 可以插入和拉回数据,如果我用 lambda 做的话我可以查询它:
var tmp = db.Companies.Include("Peoples")
但如果我尝试用 linq 查询它,并将它们连接起来:
var tmp2 = from c in db.Companies
join p in db.Person
on c.Id equals p.
这就是我的问题,People 对象没有从 db 公开它的外键,所以我不能像这样连接它们。
所以我的问题是,我可以将流利的 api 创建的 fk 公开给我的对象模型,以便我可以对其进行 linq 吗?
或者我是否应该使用 lambda,并以某种方式将其映射到我的视图模型,这样它就不会为该视图生成不需要的列?
首先,添加 FK 属性 (CompanyId
) 和 导航 属性 (Company
) 您的 People
实体:
public class People {
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
//...
public int CompanyId {get;set}
public virtual Company Company{get;set;}
}
然后,将 Fluent Api 关系配置移动到 PeopleMap
class 并以这种方式修改该配置(也将 FK 属性 映射为与在 People
table):
public class PeopleMap : EntityTypeConfiguration<People> {
public CompanyMap() {
Property(p => p.CompanyId)
.HasColumnName("CompanyId");//remember change the name for the real FK column
HasRequired(p=>p.Company)
.WithMany(c=>c.Peoples)
.HasForeignKey(p=>p.CompanyId);
}
}
之后,您应该可以在查询中使用 CompanyId
FK 属性:
var tmp2 = from c in db.Companies
join p in db.Person on c.Id equals p.CompanyId
//...