Entity framework 实体框架:在运行Seed方法时,是否可以将新创建的记录的id插入到另一个记录中
尝试将一条记录上创建的id用于多条记录时出现问题 我的模型是这样的:Entity framework 实体框架:在运行Seed方法时,是否可以将新创建的记录的id插入到另一个记录中,entity-framework,entity-framework-6,linq-to-entities,Entity Framework,Entity Framework 6,Linq To Entities,尝试将一条记录上创建的id用于多条记录时出现问题 我的模型是这样的: public class Student { [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public int StudentId { get; set; } public int PersonId { get; set; } public string SID { get; set; } public int
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)?我想对多个人的地址Id使用相同的Id。如果要对多个人使用相同的地址实体,为什么不先创建地址,然后根据需要多次使用它
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
}
}
);
太棒了!这实际上给了我比内联方法更大的灵活性。好建议,谢谢!!