1

当我尝试使用数据库(SIS)中可用的详细信息登录时,我收到此错误。

The model backing the 'SisContext' context has changed since the database was created. Consider using Code First Migrations to update the database. Thing is Database(SIS) was manually added using add existing item and not generated automatically.

我的 SisContext 看起来像这样

public class SisContext : DbContext
    {
        //protected override void OnModelCreating(DbModelBuilder modelBuilder)
        //{
        //    modelBuilder.Conventions.Remove<System.Data.Entity.Infrastructure.IncludeMetadataConvention>();
        //}
        public DbSet<User> Users { get; set; }
        public DbSet<AspNetUser> AspNetUsers { get; set; }
        public DbSet<Role> Roles { get; set; }
        public DbSet<BusinessUnit> BusinessUnits { get; set; }
        public DbSet<LicenseHolder> LicenseHolders { get; set; }
        public DbSet<License> Licenses { get; set; }

        public SisContext():base("SIS")
        {
            if (HttpContext.Current == null)
            {
                Database.SetInitializer<SisContext>(null);
            }
        }
    }

我有另一个类DataContextInitializer,看起来像这样

public class DataContextInitializer:DropCreateDatabaseAlways<SisContext>
    {
        protected override void Seed(SisContext context)
        {
        WebSecurity.Register("Demo", "123456", "demo@demo.com", true, "Demo", "Demo");
        Roles.CreateRole("Admin");
        Roles.AddUserToRole("Demo", "Admin");
        }
    }

在我的 AccountController 中,登录方法如下;

 public class AccountController : Controller
    {

        //
        // GET: /Account/
        [HttpGet]
        public ActionResult Login()
        {
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        public ActionResult Login(LoginModel model)
        {
            if(Membership.ValidateUser(model.UserName, model.Password))
            {
                FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);
                return RedirectToAction("Index", "Home");
            }
            ModelState.AddModelError("", "The user name or password provided is incorrect.");
            return View(model);
        }

        public ActionResult Index()
        {
            return View();
        }

    }

ValidateUser上述Login操作中使用的方法在CodeFirstMembershipProvider继承自的类中MembershipProvider

 public override bool ValidateUser(string username, string password)
        {
            if (string.IsNullOrEmpty(username))
            {
                return false;
            }
            if (string.IsNullOrEmpty(password))
            {
                return false;
            }
            using (SisContext Context = new SisContext())
            {
                AspNetUser User = null;
                User = Context.AspNetUsers.FirstOrDefault(Usr => Usr.Username == username);//error mentioned above is shown here
                if (User == null)
                {
                    return false;
                }
                if (!User.IsApproved)
                {
                    return false;
                }
                if (User.IsLockedOut)
                {
                    return false;
                }
                String HashedPassword = User.Password;
                Boolean VerificationSucceeded = (HashedPassword != null && Crypto.VerifyHashedPassword(HashedPassword, password));
                if (VerificationSucceeded)
                {
                    User.PasswordFailuresSinceLastSuccess = 0;
                    User.LastLoginDate = DateTime.UtcNow;
                    User.LastActivityDate = DateTime.UtcNow;
                }
                else
                {
                    int Failures = User.PasswordFailuresSinceLastSuccess;
                    if (Failures < MaxInvalidPasswordAttempts)
                    {
                        User.PasswordFailuresSinceLastSuccess += 1;
                        User.LastPasswordFailureDate = DateTime.UtcNow;
                    }
                    else if (Failures >= MaxInvalidPasswordAttempts)
                    {
                        User.LastPasswordFailureDate = DateTime.UtcNow;
                        User.LastLockoutDate = DateTime.UtcNow;
                        User.IsLockedOut = true;
                    }
                }
                Context.SaveChanges();
                if (VerificationSucceeded)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

在我的网络配置中,我添加了我的 custommembershipProvider

 <membership defaultProvider="CodeFirstMembershipProvider">
      <providers>
        <clear />
        <add name="CodeFirstMembershipProvider" type="Sorama.CustomAuthentication.CodeFirstMembershipProvider" connectionStringName="SisContext" />
      </providers>
    </membership>
    <roleManager enabled="true" defaultProvider="CodeFirstRoleProvider">
      <providers>
        <clear />
        <add name="CodeFirstRoleProvider" type="Sorama.CustomAuthentication.CodeFirstRoleProvider" connectionStringName="SisContext" />
      </providers>
    </roleManager>

    <!--<httpModules>
      <add name="PerRequestLifestyle" type="Castle.MicroKernel.Lifestyle.PerWebRequestLifestyleModule, Castle.Windsor" />
      <add name="DomainServiceModule" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </httpModules>-->
    <httpRuntime maxRequestLength="2147483647" executionTimeout="180" />
    <httpModules>
      <add name="DomainServiceModule" type="System.ServiceModel.DomainServices.Hosting.DomainServiceHttpModule, System.ServiceModel.DomainServices.Hosting, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" />
    </httpModules>
  </system.web>
  <connectionStrings>
    <add name="SISContext" connectionString="Data Source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\SIS.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework" providerName="System.Data.SqlClient" />
  </connectionStrings>
4

1 回答 1

-1

Have you tried following this link.... http://msdn.microsoft.com/en-us/data/jj591621 . I am using MVC3 and EF6.0.0.1 I had the same problem, but by Enabling Code First Migrations the error disappeared.

于 2013-12-02T04:29:39.623 回答