I am working on an ASP.NET MVC application with Entity Framework. My requirements are to insert the some default values into the tables using seed functionality.
I have successfully done this when there is no relation between the values. But I am not able to insert the foreign key reference using seed in Entity Framework.
This is the seed method:
protected override void Seed(VBlog.DbLayer.DbInitializer context)
{
//this(AddOrUpdate) will check first is there is alrady record exists or not in the database.
context.Roleses.AddOrUpdate(x => x.RoleName,
// master data
new Roles { RoleName = "Admin", Weightage = 3 },
new Roles { RoleName = "User", Weightage = 1 }
);
//adding the mast data for user table
context.Userses.AddOrUpdate(x=>x.EmailId,
new Users()
{
FirstName = "vir",
LastName = "acker",
EmailId = "vir.acker@xyz.com",
Password = password,
Gender = "Male",
Islocked = "false",
CreatedDate = DateTime.Now,
**Foreign key for Users table**
}
);
//context.Userses.AddRange(defaultUserDataList);
base.Seed(context);
}
In the above code I have passed the default for Roles and Users table. Only one column is left i.e RoleId in Users table.
How can I get the primary key of Roles table as a foreign key for the Users table?