0
複数のレコードに対して1つのレコードで作成されたIDを使用しようとすると問題が発生します。Entity Framework:シードメソッドを実行中に新しく作成したレコードのIDを別のレコードに挿入できますか
私のモデルは次のようになります。
context.Students.AddOrUpdate(x => x.SID,
new WestCobb.Data.Student
{
SID = "99-B50-404324175",
EnrollmentDate = Convert.ToDateTime("01-30-2017"),
Active = true,
Person = new WestCobb.Data.Person
{
FirstName = "Brian",
LastName = "Folks",
AddressId = 4,
SSN = 404324175,
Addresss = new WestCobb.Data.Address // This address!
{
StreetAddress = "5076 Lake Charles Court",
City = "Chicago",
State = "Illinois",
ZipCode = 60208
}
},
Account = new WestCobb.Data.Account
{
AccountNumber = "M00000000000088",
AccountTypeId = 1,
Priority = 1,
Rate = 225.00m,
BillingCycleId = 1,
CreateDate = Convert.ToDateTime("01-30-2017"),
Active = true
}
},
// Manually set AddressId of this Person to AddressId of previous Person added
new WestCobb.Data.Student
{
SID = "99-A50-404527896",
EnrollmentDate = Convert.ToDateTime("01-30-2017"),
Active = true,
Person = new WestCobb.Data.Person
{
FirstName = "Arthur",
LastName = "Clue",
SSN = 404527896,
AddressId = context.Addresses.Last().AddressId // This seems not to work
},
Account = new WestCobb.Data.Account
{
AccountNumber = "W89322198433588",
AccountTypeId = 1,
Priority = 2,
Rate = 225.00m,
BillingCycleId = 1,
CreateDate = Convert.ToDateTime("01-30-2017"),
Active = true
}
}
);
と、次のエラーを取得:データベースをシードするために、以下のものを使用して
public class Student
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int StudentId { get; set; }
public int PersonId { get; set; }
public string SID { get; set; }
public int AccountId { get; set; }
public bool Active { get; set; }
[ForeignKey("PersonId")]
public virtual Person Person { get; set; }
[ForeignKey("AccountId")]
public virtual Account Account { get; set; }
}
public class Person
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MI { get; set; }
public int SSN { get; set; }
public int AddressId { get; set; }
public int RaceId { get; set; }
[ForeignKey("AddressId")]
public Address Addresss { get; set; }
}
public class Address
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int AddressId { get; set; }
public string StreetAddress { get; set; }
public string City { get; set; }
public string State { get; set; }
public int ZipCode { get; set; }
}
LINQ to Entities does not recognize the method 'WestCobb.Data.Address Last[Address](System.Linq.IQueryable`1[WestCobb.Data.Address])' method, and this method cannot be translated into a store expression.
を使用することが可能です2番目のPのアドレスに最初のPersonのアドレスが挿入されたときに作成された同じ「id」(AddressId) erson?私は複数のPersonのAddressIdに同じIdを使用したい。
恐ろしいソース!これは実際に私にインラインメソッドよりもはるかに柔軟性を与えます。良い提案ありがとう! – GrassFed