Answer the question
In order to leave comments, you need to log in
How to perform Entity framework migration with data migration?
The bottom line is this: there used to be (and still is) a table with workers (without the right to access the server) and several more tables with users (managers and other management personnel) and their roles, which are created through ASP Net Identity. Several tables are tied to the workers.
Now you need to transfer the workers to the table with users.
I use EF Migration, with it the code for migration is automatically generated, and in the middle of this code (after the tables are created / updated and before the tables with workers and other things are deleted) I try to actually perform the data migration itself. Those. (simplified) something like this:
public override void Up()
{
//Тут какой то код, обновляющий структуру БД
using (var db = new CwDb())
{
const string getWorkersCommand = "Select * from workers";
...
var workers = db.Database.SqlQuery<Worker>(getWorkersCommand);
...
var userManager = new ApplicationUserManager(new UserStore<User>(db));
var roleManager = new ApplicationRoleManager(new RoleStore<Role>(db));
foreach (var worker in workers)
{
var newWorker = new User()
{
...
};
var result = userManager.Create(newWorker);
// Потом для каждого созданного воркера обновляем связи в связанных таблицах
db.SaveChanges();
}
//Тут какой то код, обновляющий структуру БД
The underlying context model "CwDb" has changed since the database was created. Consider updating the database with Code First Migrations ( go.microsoft.com/fwlink/?LinkId=238269).
Answer the question
In order to leave comments, you need to log in
Questions About Using EF
Questions about how to use EF can be asked on Stack Overflow using the entity-framework tag.
EF Team
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question