如何正确地为我的所有数据播种代码优先配置

本文关键字:代码 配置 数据 正确地 我的 | 更新日期: 2023-09-27 18:19:58

我在这里读到了这个问题,它和我的有点相似,但我似乎无法让它为我工作。

我正在尝试播种一些测试数据,在这种情况下是一些用户。然而,我也想为他们的地址播种,但我做不到。

这是我的POCO 的一个片段

 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; }
}

在配置文件中,如下所示:

internal sealed class Configuration : DbMigrationsConfiguration<CWS.Models.ConnectoDbContext>
{
    public Configuration()
    {
        AutomaticMigrationsEnabled = false;
    }
    protected override void Seed(CWS.Models.ConnectoDbContext context)
    {
        //  This method will be called after migrating to the latest version.
        //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
        //  to avoid creating duplicate seed data. E.g.
        context.Users.AddOrUpdate(u => u.UserId,
                new User
                {
                    Username = "Blackstallion",
                    FirstName = "Gilbert",
                    LastName = "Johnson",
                    Password = "3fZ45SDF",
                    Headline = "Novice",
                    Addresses = new List<Address> { context.Addresses.AddOrUpdate(a => a.Id,
                        new Address
                            {
                                StreetName = "asd",
                                ZipCode = "123",
                                Country = "abc",
                                Cycle = 2
                            });
                }},
                new User
                {
                    Username = "Qaus",
                    FirstName = "Jon",
                    LastName = "Orton",
                    Password = "1564as5F",
                    Headline = "Novice"
                }
                );
    }
}

}

我尝试了几种不同的方法,但在阅读了那篇文章后,这样做是有意义的,但我似乎无法做到

更新

正如用户Igarioshka所指出的,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
                            }}
                    }
                    );

如何正确地为我的所有数据播种代码优先配置

AddOrUpdate方法是void,因此它不返回任何地址。

我建议在没有上下文的情况下添加地址。像这样添加或更新:

...
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
                        }},
...

试试这个问题,它可能有助于