在集合中获取 Class 个实例 - EF Code First Fluent API
Getting Class instance in Collection - EF Code First Fluent API
我有以下三个类
Om_MembershipCharges Class
public class Om_MembershipCharges
{
[Key]
public Int32 MembershipChargesID { get; set; }
public Decimal Amount { get; set; }
public Int16 PerMonth { get; set; }
public Int16? CountryID { get; set; }
public Int16 MemebershipTypeID { get; set; }
public virtual Om_MembershipType MemebershipType { get; set; }
public virtual Om_Country Country { get; set; }
}
Om_MembershipType Class
public class Om_MembershipType
{
[Key]
public Int16 MemebershipTypeID { get; set; }
public String MemebershipType { get; set; }
public Boolean IsDefaultMembership { get; set; }
public virtual ICollection<Om_MembershipCharges> MembershipCharges { get; set; }
}
Om_Country Class
public class Om_Country
{
[Key]
public Int16 CountryID { get; set; }
public String CountryName { get; set; }
public Boolean IsActive { get; set; }
public Int16? CurrencyID { get; set; }
public Int32? MembershipChargesID { get; set; }
public virtual Om_Currency Currency { get; set; }
public Om_MembershipCharges MembershipCharges { get; set; }
}
以下是我使用 Om_MembershipType Class
中的 MembershipCharges Property
获取所有会员费用的方法。我正在使用 Include
来获取集合。
public async Task<KeyValuePair<String, Om_MembershipType>> ListByMType(String mType)
{
try
{
using (var membershipChargesContext = new DatabaseTables())
{
using (var transaction = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted },
TransactionScopeAsyncFlowOption.Enabled))
{
membershipChargesContext.Configuration.ProxyCreationEnabled = false;
var data = await membershipChargesContext
.tblMembershipType
.Where(i => i.MemebershipType == membershipType)
.Include(i => i.MembershipCharges)
.FirstOrDefaultAsync();
transaction.Complete();
return new KeyValuePair<String, Om_MembershipType>("", data);
}
}
}
catch (Exception ex)
{
var exception = ex.Message;
if (ex.InnerException != null)
exception = ex.InnerException.ToString();
return new KeyValuePair<String, Om_MembershipType>(exception, null);
}
}
有什么方法可以在 Membership Charges collection
中获取国家实例吗?
Is there any way to get the Country Instance in the Membership Charges collection?
是:
var data = await membershipChargesContext
.tblMembershipType
.Where(i => i.MemebershipType == membershipType)
.Include(i => i.MembershipCharges.Select(m => m.Country))
.FirstOrDefaultAsync();
您可以通过后续 Select
语句扩展 Include
语句中的表达式以包含嵌套的导航属性。
我有以下三个类
Om_MembershipCharges Class
public class Om_MembershipCharges
{
[Key]
public Int32 MembershipChargesID { get; set; }
public Decimal Amount { get; set; }
public Int16 PerMonth { get; set; }
public Int16? CountryID { get; set; }
public Int16 MemebershipTypeID { get; set; }
public virtual Om_MembershipType MemebershipType { get; set; }
public virtual Om_Country Country { get; set; }
}
Om_MembershipType Class
public class Om_MembershipType
{
[Key]
public Int16 MemebershipTypeID { get; set; }
public String MemebershipType { get; set; }
public Boolean IsDefaultMembership { get; set; }
public virtual ICollection<Om_MembershipCharges> MembershipCharges { get; set; }
}
Om_Country Class
public class Om_Country
{
[Key]
public Int16 CountryID { get; set; }
public String CountryName { get; set; }
public Boolean IsActive { get; set; }
public Int16? CurrencyID { get; set; }
public Int32? MembershipChargesID { get; set; }
public virtual Om_Currency Currency { get; set; }
public Om_MembershipCharges MembershipCharges { get; set; }
}
以下是我使用 Om_MembershipType Class
中的 MembershipCharges Property
获取所有会员费用的方法。我正在使用 Include
来获取集合。
public async Task<KeyValuePair<String, Om_MembershipType>> ListByMType(String mType)
{
try
{
using (var membershipChargesContext = new DatabaseTables())
{
using (var transaction = new TransactionScope(
TransactionScopeOption.Required,
new TransactionOptions { IsolationLevel = IsolationLevel.ReadUncommitted },
TransactionScopeAsyncFlowOption.Enabled))
{
membershipChargesContext.Configuration.ProxyCreationEnabled = false;
var data = await membershipChargesContext
.tblMembershipType
.Where(i => i.MemebershipType == membershipType)
.Include(i => i.MembershipCharges)
.FirstOrDefaultAsync();
transaction.Complete();
return new KeyValuePair<String, Om_MembershipType>("", data);
}
}
}
catch (Exception ex)
{
var exception = ex.Message;
if (ex.InnerException != null)
exception = ex.InnerException.ToString();
return new KeyValuePair<String, Om_MembershipType>(exception, null);
}
}
有什么方法可以在 Membership Charges collection
中获取国家实例吗?
Is there any way to get the Country Instance in the Membership Charges collection?
是:
var data = await membershipChargesContext
.tblMembershipType
.Where(i => i.MemebershipType == membershipType)
.Include(i => i.MembershipCharges.Select(m => m.Country))
.FirstOrDefaultAsync();
您可以通过后续 Select
语句扩展 Include
语句中的表达式以包含嵌套的导航属性。