尝试将一条记录上创建的ID用于多条记录时,我遇到了问题。
我的模特看起来像这样:
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; }
}
使用以下方法为数据库设定种子:
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
}
}
);
并收到以下错误:
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.
是否可以使用通过在第二个人的地址中插入第一个人的地址而创建的相同“id”(AddressId)?我想对多个人的AddressId使用相同的Id。
答案 0 :(得分:0)
如果您想对多个人使用相同的地址实体,为什么不首先创建地址,然后根据需要多次使用?
var myAwesomeAddress = new WestCobb.Data.Address()
{
StreetAddress = "5076 Lake Charles Court",
City = "Chicago",
State = "Illinois",
ZipCode = 60208
};
// You should have a DbSet of your Addresses right ?
context.Addresses.AddOrUpdate(x => x.Id, // Use any discriminator
myAwesomeAddress
);
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 = myAwesomeAddress
}
},
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,
Address = myAwesomeAddress // here
}
}
);