0

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

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

这是我的 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
                            }}

                    }
                    );
4

1 回答 1

1

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

我建议在没有 context.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
                        }},
...

试试这个问题,它可能会有所帮助

于 2013-06-25T10:12:51.503 回答