简体   繁体   中英

Complex Relationships Entity Framework Seed Method

I have some many to many relationships in which I am trying to Seed using the code first approach in the Entity Framework. Only the top most layer is actually seeding data correctly. The user class is seeding but the underlying lists of objects are not. My Poco's are as follows:

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

    }

The seed method I have constructed is as follows:

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

Again the user data is propagating in the database correctly, however, all underlying lists of objects are not propagating in the database at all. All of the tables are coming through as expected. The Context class is as follows.

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

    }

The database as it stands looks like this.

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

Addresses and other objects tables all have no values inserted.

Any help is greatly appreciated!! Thanks again guys.

,

J

I would suggest approaching this in pieces:

Add your user

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

Then add the related data like Addresses, Roles, UserRoles etc. letting EF make the connections between tables:

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

(Note: To do this, you need navigation properties like UserID and a User property in the model so EF knows which User to relate to. Like in the UserRole model/example above)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM