0

我有一些多对多的关系,我试图在实体框架中使用代码优先方法来播种。只有最顶层实际上是正确地播种数据。用户类正在播种,但对象的底层列表不是。我的Poco如下:

public class User:Database
    {
        public String FirstName { get; set; }
        public String LastName { get; set; }
        public String UserName { get; set; }
        public String Password { get; set; }
        public String Email { get; set; }

        public virtual List<Address> Addresses { get; set; }
        public virtual List<UserRole> UserRoles { get; set; }

    }
public class Address:Database
    {
        public String City { get; set; }
        public String State { get; set; }
        public int Zip { get; set; }
        public String StreetAddress1 { get; set; }
        public String StreetAddress2 { get; set; }

        public int CountryID { get; set; }
        public virtual Country Country { get; set; }

        public int AddressTypeID { get; set; }
        public virtual AddressType Type { get; set; }


    }

public class UserRole:Database
    {
        public int UserID { get; set; }
        public virtual User User { get; set; }

        public int RoleID { get; set; }
        public virtual Role Role { get; set; }
    }

public class Role:Database
    {
        public String Name { get; set; }
    }

public abstract class Database
    {
        public int ID { get; set; }
        public DateTime CreatedAt { get; set; }
        public DateTime UpdatedAt { get; set; }
        public Boolean IsActive { get; set; }

    }

我构建的种子方法如下:

protected override void Seed(CodeFirstODS.DAL.DBContext.Context 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.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //

            context.Users.AddOrUpdate(
                u => u.ID,
                new Models.User { 
                    ID=1, 
                    FirstName="James",
                    LastName = "Rainey",                    
                    UserName = "BloodyRu1n",
                    Password = "bobcat*",
                    Email = "james_1999@blue.com",
                    Addresses = new List<Models.Address>(){
                        new Models.Address(){
                            ID = 1,
                            StreetAddress1 = "1260 Monkey Knoll",
                            City = "Marietta",
                            State = "Geaorgia",
                            Country = new Models.Country(){
                                ID = 1,
                                Name = "USA",
                                CreatedAt = DateTime.Now,
                                UpdatedAt = DateTime.Now,
                                IsActive = true
                            },
                            Type = new Models.AddressType(){
                                ID = 1,
                                Name = "Home",
                                CreatedAt = DateTime.Now,
                                UpdatedAt = DateTime.Now,
                                IsActive = true
                            },
                            CreatedAt = DateTime.Now,
                            UpdatedAt = DateTime.Now,
                            IsActive = true
                        }
                    },
                    UserRoles = new List<Models.UserRole>(){
                        new Models.UserRole(){
                            Role = new Models.Role(){
                                ID = 1,
                                Name = "Admin",
                                CreatedAt = DateTime.Now,
                                UpdatedAt = DateTime.Now,
                                IsActive = true
                            }                            
                        },
                        new Models.UserRole(){
                            Role = new Models.Role(){
                                ID = 1,
                                Name = "Admin",
                                CreatedAt = DateTime.Now,
                                UpdatedAt = DateTime.Now,
                                IsActive = true
                            }
                        },
                    },
                    CreatedAt = DateTime.Now,
                    UpdatedAt = DateTime.Now,
                    IsActive = true
                }
            );//End User 1 Seed
            context.SaveChanges();
        }

用户数据再次在数据库中正确传播,但是,所有底层对象列表根本没有在数据库中传播。所有表格都按预期通过。上下文类如下。

public class Context:DbContext
    {

        public DbSet<User> Users { get; set; }
        public DbSet<UserRole> UserRoles { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<Address> Addresses { get; set; }
        public DbSet<AddressType> AddressTypes { get; set; }
        public DbSet<Country> Countries { get; set; }

    }

数据库现在看起来像这样。

ID  FirstName   LastName    UserName    Password    Email   CreatedAt   UpdatedAt   IsActive
1   James   Rainey  BloodyRu1n  bobcat* james_1999@blue.com 2013-07-04 19:34:46.767 2013-07-04 19:34:46.767 1

地址和其他对象表都没有插入任何值。

任何帮助是极大的赞赏!!再次感谢各位。

,

Ĵ

4

1 回答 1

2

我建议分段处理:

添加您的用户

context.Users.AddOrUpdate(
        u => u.ID,
        new Models.User { 
          ID=1, 
          FirstName="James",
          LastName = "Rainey",                    
          UserName = "BloodyRu1n",
          Password = "bobcat*",
          Email = "james_1999@blue.com",                    
          CreatedAt = DateTime.Now,
          UpdatedAt = DateTime.Now,
          IsActive = true
        }
);//End User 1 Seed

然后添加地址、角色、用户角色等相关数据,让 EF 在表之间建立连接:

context.UserRoles.AddOrUpdate(
        r => r.ID,        
        new Models.UserRole{
          UserID = 1,
          //etc
        },
        new Models.UserRole{
          UserID = 2,
          //etc
        },
}; //End roles seed

(注意:为此,您需要模型中的 UserID 和 User 属性等导航属性,以便 EF 知道与哪个用户相关。就像上面的 UserRole 模型/示例一样)

于 2013-07-05T15:42:20.253 回答