C# 如何正确地为所有数据设定种子代码优先配置
我读了这个问题,它有点像我的问题,但我似乎无法让它为我工作 我正在尝试播种一些测试数据,在本例中,它是一些用户。不过,我也很想知道他们的地址,但我不能 这是我的POCO的一个片段C# 如何正确地为所有数据设定种子代码优先配置,c#,asp.net,asp.net-web-api,ef-code-first,C#,Asp.net,Asp.net Web Api,Ef Code First,我读了这个问题,它有点像我的问题,但我似乎无法让它为我工作 我正在尝试播种一些测试数据,在本例中,它是一些用户。不过,我也很想知道他们的地址,但我不能 这是我的POCO的一个片段 public class User { public int UserId { get; set; } public string Username { get; set; } public string FirstName { get; set; } public string La
public class User
{
public int UserId { get; set; }
public string Username { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Password { get; set; }
public string Headline { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
public virtual ICollection<Phonenumber> Phonenumbers { get; set; }
public virtual ICollection<Email> Emails { get; set; }
public virtual ICollection<Position> Positions { get; set; }
}
public class Address
{
public string Id { get; set; }
public string StreetName { get; set; }
public string Country { get; set; }
public string ZipCode { get; set; }
public int Cycle { get; set; }
public int UserId { get; set; }
public User User { get; set; }
}
公共类用户
{
public int UserId{get;set;}
公共字符串用户名{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串密码{get;set;}
公共字符串标题{get;set;}
publicvirtualicollection指出AddOrUpdate方法是无效的,因此我不能像上面的例子那样返回任何地址
这是我的解决方案:
context.Users.AddOrUpdate(u => u.UserId,
new User
{
Username = "Bob",
FirstName = "John",
LastName = "Doe",
Password = "123",
Headline = "Elite",
Addresses = new List<Address>{
new Address
{
Id = "1",
StreetName = "John's Street",
ZipCode = "1234",
Country = "AB",
Cycle = 2
},
new Address
{
Id = "2",
StreetName = "John's Work",
ZipCode = "1234",
Country = "AB",
Cycle = 3
},
new Address
{
Id = "3",
StreetName = "John's Super Secret Street",
ZipCode = "0000",
Country = "AB",
Cycle = 44
}}
}
);
context.Users.AddOrUpdate(u=>u.UserId,
新用户
{
Username=“Bob”,
FirstName=“约翰”,
LastName=“Doe”,
Password=“123”,
Headline=“精英”,
地址=新列表{
新地址
{
Id=“1”,
StreetName=“约翰街”,
ZipCode=“1234”,
Country=“AB”,
周期=2
},
新地址
{
Id=“2”,
StreetName=“约翰的作品”,
ZipCode=“1234”,
Country=“AB”,
周期=3
},
新地址
{
Id=“3”,
StreetName=“约翰的超级秘密街”,
ZipCode=“0000”,
Country=“AB”,
周期=44
}}
}
);
AddOrUpdate方法无效,因此不会返回任何地址
我建议添加没有上下文的地址。添加或更新如下:
...
Addresses = new List<Address> {
new Address
{
Id = 1, //depends on if you have an address defined and how your schema is created
StreetName = "asd",
ZipCode = "123",
Country = "abc",
Cycle = 2
}},
...
。。。
Addresses=新列表问题,它可能会有帮助您似乎没有调用上下文。SaveChanges()
。@anaximander是否有必要,您能否进一步解释它的作用?:)@tweeps SaveChanges()实际保存更改…如果没有它,数据将不会保存到DB@Thewads我无法理解这一点,因为当我从代码中删除地址部分时,我可以在没有任何问题的情况下为用户播种,而对于该部分,我没有使用context.SaveChanges()
:/@anaximander在植入这样的数据时,您不需要调用savechanges。它的效果非常好,可惜我自己没有意识到该方法是无效的:干杯!
...
Addresses = new List<Address> {
new Address
{
Id = 1, //depends on if you have an address defined and how your schema is created
StreetName = "asd",
ZipCode = "123",
Country = "abc",
Cycle = 2
}},
...