我试图为一些具有一对多关系的数据库表种子。使用代码第一种方法与实体框架。不知何故,外键在数据库表活动中保持为空,列为UserId。
为什么这不起作用?
context.Users.AddOrUpdate(
u => u.FirstName,
new User { Id = 1, FirstName = "Trees", Preposition = "de", LastName = "Vries", EmailAddress = "trees@test.com", PhoneNumber = 0684637485, Password = "test123", RoleId = 1 }
);
context.SaveChanges();
context.Activities.AddOrUpdate(
a => a.Name,
new Activity
{
Name = "Metselen van de binnenplaats",
Comment = "Voorbeeld commentaar regel 1.",
Start = DateTime.Now,
End = DateTime.Now.AddHours(5),
},//etc.
);
context.SaveChanges();
var randomActivities = (from s in context.Activities select s).OrderBy(x => Guid.NewGuid()).Take(6).ToList();
context.Users.AddOrUpdate(
u => u.Id,
new User { Id = 1, Activities = randomActivities }
);
context.SaveChanges();
编辑:解决方案
经过几个小时的代码摔跤后,我终于设法让它全部正常工作(无需在模型类中添加外键)。
我将分享代码和一些发现,以便其他人可以从中受益。
要记住一些事情。
直接编码到上下文!
这不起作用:
List<Activity> randomActivities = (from s in context.Activities select s).OrderBy(x => Guid.NewGuid()).Take(2).ToList(); //Does not work
使用:
List<Activity> randomActivities = context.Activities.OrderBy(x => Guid.NewGuid()).Take(2).ToList();
当您需要链接已经需要在表中的对象时,只调用SaveChanges()。
context.SaveChanges();
最后,你可以创建与其他表有关系的数据,只需在其中添加一个新的List(详见下面的代码)。
种子方法现在看起来像这样:
var users = new List<User>
{
new User
{
FirstName = "Trees",
Preposition = "de",
LastName = "Vries",
EmailAddress = "trees@test.com",
PhoneNumber = 0684637485,
Password = "test123",
Activities =
new List<Activity> { // <-- Like this!
new Activity { Name = "Boren muren in de grote hal 1", Start = DateTime.Now, End = DateTime.Now.AddHours(5) },
new Activity { Name = "Vloer schuren 1", Start = DateTime.Now, End = DateTime.Now.AddHours(1) },
new Activity { Name = "Boren muren in de grote hal 2", Start = DateTime.Now, End = DateTime.Now.AddHours(5) },
new Activity { Name = "Vloer schuren 2", Start = DateTime.Now, End = DateTime.Now.AddHours(1) }
},
Messages =
new List<Message>
{
new Message { Subject = "Test onderwerp van bericht 1", Body = "Tekst van het bericht 1", AuthorId = 3 },
new Message { Subject = "Test onderwerp van bericht 2", Body = "Tekst van het bericht 2", AuthorId = 3 },
new Message { Subject = "Test onderwerp van bericht 3", Body = "Tekst van het bericht 3", AuthorId = 3 }
}
},
new User { FirstName = "Jaap", LastName = "Winter", EmailAddress = "jaap@test.com", PhoneNumber = 0628305643, Password = "test123" },
new User { FirstName = "Klaas", LastName = "Janssen", EmailAddress = "klaas@test.com", PhoneNumber = 0639453321, Password = "test123" }
};
users.ForEach(s => context.Users.AddOrUpdate(r => r.FirstName, s));
context.SaveChanges();
List<Activity> randomActivities = context.Activities.OrderBy(x => Guid.NewGuid()).Take(2).ToList();
var projects = new List<Project>
{
new Project { Name = "Test project", Description = "Omschrijving van test project 1.", Start = DateTime.Now, End = DateTime.Now.AddDays(10), User = context.Users.Single(s => s.FirstName == "Trees"), Activities = randomActivities }
};
projects.ForEach(s => context.Projects.AddOrUpdate(r => r.Name, s));
活动模型类:
public class Activity
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Start { get; set; }
public DateTime End { get; set; }
public Skill Skill { get; set; }
public string Comment { get; set; }
}
用户模型类:
public int Id { get; set; }
public string FirstName { get; set; }
public string Preposition { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public int PhoneNumber { get; set; }
public string Password { get; set; }
public bool Active { get; set; }
public Role Role { get; set; }
public ICollection<Skill> Skills { get; set; }
public ICollection<Message> Messages { get; set; }
public ICollection<Activity> Activities { get; set; }
}