我正在使用Entity Framework 6。
我正在上2节课:
public partial class StateProvince
{
public StateProvince()
{
Addresses = new HashSet<Address>();
}
public int StateProvinceID { get; set; }
public string StateProvinceCode { get; set; }
public string CountryRegionCode { get; set; }
public bool IsOnlyStateProvinceFlag { get; set; }
public string Name { get; set; }
public int TerritoryID { get; set; }
public Guid rowguid { get; set; }
public DateTime ModifiedDate { get; set; }
public ICollection<Address> Addresses { get; set; }
public CountryRegion CountryRegion { get; set; }
}
public partial class CountryRegion
{
public CountryRegion()
{
StateProvinces = new HashSet<StateProvinceTlb>();
}
public string CountryRegionCode { get; set; }
public string Name { get; set; }
public DateTime ModifiedDate { get; set; }
public virtual ICollection<StateProvince> StateProvinces { get; set;}
}
我希望能够运行一个查询,该查询将返回StateProvince的列表,但还要包括CountryRegion中的名称。想法是,在编辑屏幕上,用户将选择或编辑CountryRegionCode,但是Name将显示在其旁边的不可编辑字段中,仅供参考。
我尝试将属性作为未映射的字段添加到StateProvince并在CountryRegion上引用该属性,如下所示:
[NotMapped]
public string CountryName
{
get{ return CountryRegion.Name;}
}
但这是一个问题,必须加载CountryRegion才能正常工作,我的目标是不必加载整个CountryRegion对象。
我也尝试过在查询中进行如下设置:
List<StateProvince> statP = context.StateProvinces.Select(s => new StateProvince() {s, CountryName = context.CountryRegions.Where(x => x.CountryRegionCode == s.CountryRegionCode).Select(x => x.Name).FirstOrDefault() }).ToList();
但这不起作用,因为返回的对象由StateProvince对象和单独的CountryName属性组成。
我什至尝试将每个字段分别设置为:
List<StateProvince> statP = context.StateProvinces.Select(s => new StateProvince() { Name = s.Name, TerritoryID = s.TerritoryID, rowguid = s.rowguid, ModifiedDate = s.ModifiedDate, Addresses=s.Addresses, CountryRegion=s.CountryRegion, CountryName = context.CountryRegions.Where(x => x.CountryRegionCode == s.CountryRegionCode).Select(x => x.Name).FirstOrDefault() }).ToList();
但这又导致整个CountryRegion对象加载,并且如果我忽略这些属性,则会引发异常。而且,对于较大的实体,这将很难维护。
TLDR;此版本的版本:实体框架中是否有一种方法可以将一个类映射到一个表,但是在该类上具有一个属性,而该属性引用了另一个表上的属性,而不必检索子表上的所有内容?
我已经搜索了很多,但实际上找不到涵盖这种特定情况的任何内容。我对Entity Framework不太熟悉,感觉好像缺少明显的东西。任何帮助将不胜感激!!
解
这就是我想出的解决方案。
首先,我将CountryRegion表分为两个单独的类
public partial class CountryRegionHeader
{
public CountryRegionHeader()
{
StateProvinces = new HashSet<StateProvinceTlb>();
}
public string CountryRegionCode { get; set; }
public string Name { get; set; }
}
public partial class CountryRegionDetail
{
public CountryRegionDetail()
{
StateProvinces = new HashSet<StateProvinceTlb>();
}
public string CountryRegionCode { get; set; }
public DateTime ModifiedDate { get; set; }
public virtual ICollection<StateProvince> StateProvinces { get; set;}
public virtual CountryRegion CountryRegion {get;set;}
}
然后,将新类的属性添加到StateProvince类中
[ForeignKey("CountryRegionCode)]
public CountryRegionHeader CountryRegionHeader {get;set;}
[ForeignKey("CountryRegionCode)]
public CountryRegionDetail CountryRegionDetail {get;set;}
然后,我将DBSet添加到我的CountryRegionHeader和CountryRegionDetail的模型上下文中,并使用OnModelCreating方法中的流畅API将它们绑定在一起
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<CountryRegionHeader>()
.HasRequired(e => e.CountryRegionDetail)
.WithRequiredPrincipal();
}
除此之外,我还创建了另一个名为CountryRegion的类,该类具有Header和Detail以及Header和Detail对象本身的所有属性。该属性实际上指向标题和详细信息。这确实不是必需的,但是它使代码更简洁,更易于使用。另外,将数据向下发送到Web客户端时,我可以仅序列化CountryRegion对象,而排除Header和Detail对象。所以基本上我的CountryRegion类如下所示:
public Class CountryRegion
{
public CountryRegionHeader Header;
public CountryRegionDetail Detail;
public CountryRegionCode
{
//need some special logic here for the key since both Header or
//Detail share the primary key and both may not be loaded
get
{
if (Header != null)
return Header.CountryRegionCode;
else
return Detail.CountryRegionCode;
}
set
{
if (Header != null) Header.CountryRegionCode = value;
if (Detail != null) Detail.CountryRegionCode = value;
}
}
public string Name
{
get
{
return Header.Name;
}
set
{
Header.Name = value;
}
}
public DateTime ModifiedDate
{
get
{
return Detail.ModifiedDate ;
}
set
{
Detail.ModifiedDate = value;
}
}
public virtual ICollection<StateProvince> StateProvinces
{
get
{
return Detail.StateProvinces ;
}
set
{
Detail.StateProvinces = value;
}
}
}
因此,现在当我查询时,我可以执行以下操作:
List<StateProvince> query = db.StateProvince.Include(o=>o.CountryRegionHeader).ToList();
而且我只检索所需的数据,而没有检索整个CountryRegion记录
另外,如果我仅使用CountryRegion,则可以这样查询:
List<CountryRegion> query = (from a in db.CountryRegionHeader join b in db.CountryRegionDetail on a.CountryRegionCode equals b.CountryRegionCode select new Employee(){Header = a, Detail = b}).ToList();
最佳答案
解
这就是我想出的解决方案。
首先,我将CountryRegion表分为两个单独的类
public partial class CountryRegionHeader
{
public CountryRegionHeader()
{
StateProvinces = new HashSet<StateProvinceTlb>();
}
public string CountryRegionCode { get; set; }
public string Name { get; set; }
}
public partial class CountryRegionDetail
{
public CountryRegionDetail()
{
StateProvinces = new HashSet<StateProvinceTlb>();
}
public string CountryRegionCode { get; set; }
public DateTime ModifiedDate { get; set; }
public virtual ICollection<StateProvince> StateProvinces { get; set;}
pubic virtual CountryRegion CountryRegion {get;set;}
}
然后,将新类的属性添加到StateProvince类中
[ForeignKey("CountryRegionCode)]
public CountryRegionHeader CountryRegionHeader {get;set;}
[ForeignKey("CountryRegionCode)]
public CountryRegionDetail CountryRegionDetail {get;set;}
然后,我将DBSet添加到我的CountryRegionHeader和CountryRegionDetail的模型上下文中,并使用OnModelCreating方法中的流畅API将它们绑定在一起
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<CountryRegionHeader>()
.HasRequired(e => e.CountryRegionDetail)
.WithRequiredPrincipal();
}
除此之外,我还创建了另一个名为CountryRegion的类,该类具有Header和Detail以及Header和Detail对象本身的所有属性。该属性实际上指向标题和详细信息。这确实不是必需的,但是它使代码更简洁,更易于使用。另外,将数据向下发送到Web客户端时,我可以仅序列化CountryRegion对象,而排除Header和Detail对象。所以基本上我的CountryRegion类如下所示:
public Class CountryRegion
{
public CountryRegionHeader Header;
public CountryRegionDetail Detail;
public CountryRegionCode
{
//need some special logic here for the key since both Header or
//Detail share the primary key and both may not be loaded
get
{
if (Header != null)
return Header.CountryRegionCode;
else
return Detail.CountryRegionCode;
}
set
{
if (Header != null) Header.CountryRegionCode = value;
if (Detail != null) Detail.CountryRegionCode = value;
}
}
public string Name
{
get
{
return Header.Name;
}
set
{
Header.Name = value;
}
}
public DateTime ModifiedDate
{
get
{
return Detail.ModifiedDate ;
}
set
{
Detail.ModifiedDate = value;
}
}
public virtual ICollection<StateProvince> StateProvinces
{
get
{
return Detail.StateProvinces ;
}
set
{
Detail.StateProvinces = value;
}
}
}
因此,现在当我查询时,我可以执行以下操作:
List<StateProvince> query = db.StateProvince.Include(o=>o.CountryRegionHeader).ToList();
而且我只检索所需的数据,而没有检索整个CountryRegion记录
另外,如果我仅使用CountryRegion,则可以这样查询:
List<CountryRegion> query = (from a in db.CountryRegionHeader join b in db.CountryRegionDetail on a.CountryRegionCode equals b.CountryRegionCode select new Employee(){Header = a, Detail = b}).ToList();
关于c# - Entity Framework 引用属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35984325/