私はそこに同様の問題があることを知っています.これはネストされたViewModelをうまく作成して設定する方法とほとんど同じですが、私の問題は解決しません.
EF Code First と LINQ to Entities を使用しています。これらは私のエンティティの一部です
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; }
}
そして、これはそれらがどのようにマッピングされているかです
public ApplicationEntityTypeConfiguration()
{
//Mapping
this.HasKey(ap => ap.ApplicationID);
this.Property(ap => ap.ApplicationID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(ap => ap.WorldPayID)
.HasColumnType("bigint");
this.Property(ap => ap.Processed)
.HasColumnType("smalldatetime");
this.Property(ap => ap.Exported)
.HasColumnType("smalldatetime");
//Relationships
this.HasRequired(ap => ap.MembershipType)
.WithMany()
.HasForeignKey(ap => ap.MembershipTypeID);
this.HasRequired(ap => ap.ApplicationType)
.WithMany()
.HasForeignKey(ap => ap.ApplicationTypeID);
this.HasOptional(ap => ap.WorldPayStatus)
.WithMany()
.HasForeignKey(ap => ap.WorldPayStatusCode);
this.HasRequired(ap => ap.PaymentType)
.WithMany()
.HasForeignKey(ap => ap.PaymentTypeID);
this.HasRequired(ap => ap.Person)
.WithMany()
.HasForeignKey(ap => ap.PersonID);
this.HasRequired(ap => ap.InvoiceAddress)
.WithMany()
.HasForeignKey(ap => ap.InvoiceAddressID);
}
}
public class PersonEntityTypeConfiguration : EntityTypeConfiguration<Person>
{
public PersonEntityTypeConfiguration()
{
this.HasKey(p => p.PersonID);
this.Property(p => p.PersonID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(p => p.Created)
.HasColumnType("smalldatetime");
this.Property(p => p.Updated)
.HasColumnType("smalldatetime");
//Relationships
this.HasRequired(p => p.Organisation)
.WithMany()
.HasForeignKey(p => p.OrganisationID);
this.HasRequired(p => p.HomeAddress)
.WithMany()
.HasForeignKey(p => p.HomeAddressID);
this.HasRequired(p => p.WorkAddress)
.WithMany()
.HasForeignKey(p => p.WorkAddressID);
}
}
public class AddressEntityTypeConfiguration : EntityTypeConfiguration<Address>
{
public AddressEntityTypeConfiguration()
{
this.HasKey(a => a.AddressID);
this.Property(a => a.AddressID)
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(a => a.Created)
.HasColumnType("smalldatetime");
this.Property(a => a.Updated)
.HasColumnType("smalldatetime");
//Relationships))))
this.HasRequired(a => a.Country)
.WithMany()
.HasForeignKey(a => a.CountryCode)
.WillCascadeOnDelete(false);
}
}
これは、私が設定しようとしているクラスの構造です
public class OrganisationEmailMessageData
{
public string ApplicationType { get; set; }
public int ApplicationId { get; set; }
public string PaymentType { get; set; }
public string MembershipType { get; set; }
public double Price { get; set; }
public string FullName { get; set; }
public string JobTitle { get; set; }
public string Department { get; set; }
public string Organisation { get; set; }
public AddressEmailData HomeAddress { get; set; }
public AddressEmailData WorkAddress { get; set; }
public AddressEmailData InvoiceAddress { get; set; }
public string PublicArea { get; set; }
public string[] AreasOfInterest { get; set; }
}
public class AddressEmailData
{
public bool Selected { get; set; }
public string Address1 { get; set; }
public string Address2 { get; set; }
public string Address3 { get; set; }
public string Address4 { get; set; }
public string Town { get; set; }
public string Region { get; set; }
public string PostalCode { get; set; }
public string Country { get; set; }
}
これは、タイプ OrganisationEmailMessageData のオブジェクトを一度に入力しようとしている方法です。
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();
LINQ to SQL を使用していたときに他のアプリケーションでこれを行ったことがありますが、アプリケーションを実行するときに LINQ to Entities を使用すると、このエラーが発生します
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.
モデルに何か問題がありますか?これは Address エンティティについて不平を言っているためです。ネストされた ViewModel を適切に作成および設定する方法
の上部に投稿した問題の解決策は、ネストされたコレクションの ToList() メソッド呼び出しを削除することでしたが、これは Single() メソッド呼び出しでは機能しません。
どんな助けでも大歓迎です。