简体   繁体   中英

MySql + EntityFramework “Backmodel have changed”

I'm working in an App (WinForm + ServiceStack WebServices) and in the AppHost when I try to store some data in a MySql db get this error: 在此处输入图片说明

I haven't changed the model. How can I solve this?

Here is MyDbContext

public class MySqlContext : System.Data.Entity.DbContext
    {
        public System.Data.Entity.DbSet<LoginModel> LoginModels { get; set; }
        public System.Data.Entity.DbSet<Role> Roles { get; set; }
        public System.Data.Entity.DbSet<Permission> Permissions { get; set; }

        public MySqlContext()
            : base("ConnectionStringName")
        {
            Database.SetInitializer <MySqlContext>(null);

        }


        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {


            modelBuilder.Entity<LoginModel>().HasMany(lm => lm.roles).WithMany(r => r.Users)
                  .Map(
                      m =>
                      {
                          m.MapLeftKey("lm_Id");
                          m.MapRightKey("role_Id");
                          m.ToTable("login_roles");
                      }
                  );

            modelBuilder.Entity<Role>().HasMany(r => r.Permissions).WithMany(p => p.Roles)
                .Map(
                    m =>
                    {
                        m.MapLeftKey("role_id");
                        m.MapRightKey("per_id");
                        m.ToTable("roles_permissions");
                    }
                );



        }

    }

Try

Database.SetInitializer<YourDbContext>(null);

In your DbContext

For Example

public class MyDbContext : DbContext
{
    public MyDbContext ()
    {
        Database.SetInitializer<MyDbContext >(null);
    }

    public DbSet<Users> Users { get; set; }    
}

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