Entity framework 4 EF代码优先和Linq到实体,如何一次性填充嵌套对象?
我知道也有类似的问题,这一个几乎是一样的,但它不能解决我的问题 Im首先使用EF代码,然后使用LINQ创建实体。这些是我的一些实体Entity framework 4 EF代码优先和Linq到实体,如何一次性填充嵌套对象?,entity-framework-4,code-first,Entity Framework 4,Code First,我知道也有类似的问题,这一个几乎是一样的,但它不能解决我的问题 Im首先使用EF代码,然后使用LINQ创建实体。这些是我的一些实体 public class Application { public int ApplicationID { get; set; } public int ApplicationTypeID { get; set; } public int MembershipTypeID { get; set; } public string Ma
public class Application
{
public int ApplicationID { get; set; }
public int ApplicationTypeID { get; set; }
public int MembershipTypeID { get; set; }
public string MailTo { get; set; }
public DateTime ApplicationDate { get; set; }
public int PersonID { get; set; }
.......
public virtual Person Person { get; set; }
.......
public virtual PaymentType PaymentType { get; set; }
}
public class Person
{
public int PersonID { get; set; }
public int? OrganisationID { get; set; }
public int? HomeAddressID { get; set; }
public int? WorkAddressID { get; set; }
public string Title { get; set; }
public string Initials { get; set; }
public string Forename { get; set; }
public string Surname { get; set; }
public string JobTitle { get; set; }
public string Department { get; set; }
public string Phone { get; set; }
public string Fax { get; set; }
public string Email { get; set; }
public bool NoInhouseMail { get; set; }
public bool NoThirdPartyMail { get; set; }
public DateTime Created { get; set; }
public DateTime? Updated { get; set; }
public virtual Address HomeAddress { get; set; }
public virtual Address WorkAddress { get; set; }
public virtual Organisation Organisation { get; set; }
public virtual ICollection<PersonAttribute> PersonAttributes { get; set; }
public virtual ICollection<Email> Emails { get; set; }
}
{
public class Address
{
public int AddressID { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Town { get; set; }
public string Region { get; set; }
public string Postcode { get; set; }
public string CountryCode { get; set; }
public DateTime Created { get; set; }
public DateTime? Updated { get; set; }
public virtual Country Country { get; set; }
}
这是我尝试一次性填充OrganizationEmailMessageData类型对象的方式
using (var db = _databaseFactory.GetDatabase())
{
var messageData = (from a in db.Applications
where a.ApplicationID == applicationId
select new OrganisationEmailMessageData
{
ApplicationType = a.ApplicationType.EmailMessage,
ApplicationId = a.ApplicationID,
PaymentType = a.PaymentType.Type,
MembershipType = a.MembershipType.Type,
Price = a.MembershipType.Price,
FullName = a.Person.Title + " " + a.Person.Forename + " " + a.Person.Surname,
JobTitle = a.Person.JobTitle,
Department = a.Person.Department ?? string.Empty,
Organisation = a.Person.Organisation != null
? a.Person.Organisation.Name
: string.Empty,
HomeAddress = a.Person.HomeAddressID.HasValue
? ( from add in db.Addresses
where add.AddressID == a.Person.HomeAddressID.Value
select new AddressEmailData
{
Address1 = add.Address1,
Address2 = add.Address2,
Address3 = add.Address3,
Address4 = add.Address4,
Region = add.Region,
Town = add.Town,
PostalCode = add.Postcode,
Country = add.Country.Name
}).Single()
: null,
WorkAddress = a.Person.WorkAddressID.HasValue
? (from add in db.Addresses
where add.AddressID == a.Person.WorkAddressID.Value
select new AddressEmailData
{
Address1 = add.Address1,
Address2 = add.Address2,
Address3 = add.Address3,
Address4 = add.Address4,
Region = add.Region,
Town = add.Town,
PostalCode = add.Postcode,
Country = add.Country.Name
}).Single()
: null,
InvoiceAddress = (from add in db.Addresses
where add.AddressID == a.InvoiceAddressID
select new AddressEmailData
{
Address1 = add.Address1,
Address2 = add.Address2,
Address3 = add.Address3,
Address4 = add.Address4,
Region = add.Region,
Town = add.Town,
PostalCode = add.Postcode,
Country = add.Country.Name
}).Single(),
PublicArea = a.Person.PersonAttributes.Select(att => att.Attribute.Parent.Value).FirstOrDefault(),
AreasOfInterest = a.Person.PersonAttributes.Select(att => att.Attribute.Value).ToArray()
}
).SingleOrDefault();
我以前在其他应用程序中使用LINQtoSQL时也这样做过,但在运行应用程序时使用LINQtoEntities时,我会遇到此错误
Unable to create a constant value of type 'Namespace.Model.Entities.Address'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
这个模型有什么问题吗?因为这是在抱怨地址实体。我在顶部发布的问题的解决方案是删除嵌套集合的ToList()方法调用,但这不适用于单个()方法调用。
任何帮助都将不胜感激 问题在于,在同一LINQ查询中,您将LINQ to实体与LINQ to对象混合使用。我看到的最简单的解决方法是,首先使用一个LINQ查询从数据库获取结果,然后在第二个LINQ查询中将结果投影到所需的对象中
//LINQ TO Entities - DbQuery
var dbResults = (from a in db.Applications
where a.ApplicationID == applicationId
select a).ToList();
//LINQ To Objects-- project results from Db into required object
var messageData = (from a in dbResults
select new OrganisationEmailMessageData
{
ApplicationType = a.ApplicationType.EmailMessage,
ApplicationId = a.ApplicationID,
PaymentType = a.PaymentType.Type,
MembershipType = a.MembershipType.Type,
Price = a.MembershipType.Price,
FullName = a.Person.Title + " " + a.Person.Forename + " " + a.Person.Surname,
JobTitle = a.Person.JobTitle,
Department = a.Person.Department ?? string.Empty,
Organisation = a.Person.Organisation != null
? a.Person.Organisation.Name
: string.Empty,
HomeAddress = a.Person.HomeAddressID.HasValue
? ( from add in db.Addresses
where add.AddressID == a.Person.HomeAddressID.Value
select new AddressEmailData
{
Address1 = add.Address1,
Address2 = add.Address2,
Address3 = add.Address3,
Address4 = add.Address4,
Region = add.Region,
Town = add.Town,
PostalCode = add.Postcode,
Country = add.Country.Name
}).Single()
: null,
WorkAddress = a.Person.WorkAddressID.HasValue
? (from add in db.Addresses
where add.AddressID == a.Person.WorkAddressID.Value
select new AddressEmailData
{
Address1 = add.Address1,
Address2 = add.Address2,
Address3 = add.Address3,
Address4 = add.Address4,
Region = add.Region,
Town = add.Town,
PostalCode = add.Postcode,
Country = add.Country.Name
}).Single()
: null,
InvoiceAddress = (from add in db.Addresses
where add.AddressID == a.InvoiceAddressID
select new AddressEmailData
{
Address1 = add.Address1,
Address2 = add.Address2,
Address3 = add.Address3,
Address4 = add.Address4,
Region = add.Region,
Town = add.Town,
PostalCode = add.Postcode,
Country = add.Country.Name
}).Single(),
PublicArea = a.Person.PersonAttributes.Select(att => att.Attribute.Parent.Value).FirstOrDefault(),
AreasOfInterest = a.Person.PersonAttributes.Select(att => att.Attribute.Value).ToArray()
}
).SingleOrDefault();
谢谢我用它作为解决方案,唯一一个我仍然不明白的问题是为什么它在LINQtoSQL上有效,而在EF和LINQtoEntities上无效。
Unable to create a constant value of type 'Namespace.Model.Entities.Address'. Only primitive types ('such as Int32, String, and Guid') are supported in this context.
//LINQ TO Entities - DbQuery
var dbResults = (from a in db.Applications
where a.ApplicationID == applicationId
select a).ToList();
//LINQ To Objects-- project results from Db into required object
var messageData = (from a in dbResults
select new OrganisationEmailMessageData
{
ApplicationType = a.ApplicationType.EmailMessage,
ApplicationId = a.ApplicationID,
PaymentType = a.PaymentType.Type,
MembershipType = a.MembershipType.Type,
Price = a.MembershipType.Price,
FullName = a.Person.Title + " " + a.Person.Forename + " " + a.Person.Surname,
JobTitle = a.Person.JobTitle,
Department = a.Person.Department ?? string.Empty,
Organisation = a.Person.Organisation != null
? a.Person.Organisation.Name
: string.Empty,
HomeAddress = a.Person.HomeAddressID.HasValue
? ( from add in db.Addresses
where add.AddressID == a.Person.HomeAddressID.Value
select new AddressEmailData
{
Address1 = add.Address1,
Address2 = add.Address2,
Address3 = add.Address3,
Address4 = add.Address4,
Region = add.Region,
Town = add.Town,
PostalCode = add.Postcode,
Country = add.Country.Name
}).Single()
: null,
WorkAddress = a.Person.WorkAddressID.HasValue
? (from add in db.Addresses
where add.AddressID == a.Person.WorkAddressID.Value
select new AddressEmailData
{
Address1 = add.Address1,
Address2 = add.Address2,
Address3 = add.Address3,
Address4 = add.Address4,
Region = add.Region,
Town = add.Town,
PostalCode = add.Postcode,
Country = add.Country.Name
}).Single()
: null,
InvoiceAddress = (from add in db.Addresses
where add.AddressID == a.InvoiceAddressID
select new AddressEmailData
{
Address1 = add.Address1,
Address2 = add.Address2,
Address3 = add.Address3,
Address4 = add.Address4,
Region = add.Region,
Town = add.Town,
PostalCode = add.Postcode,
Country = add.Country.Name
}).Single(),
PublicArea = a.Person.PersonAttributes.Select(att => att.Attribute.Parent.Value).FirstOrDefault(),
AreasOfInterest = a.Person.PersonAttributes.Select(att => att.Attribute.Value).ToArray()
}
).SingleOrDefault();