0

以前にもこの質問をしましたが、助けが得られることを願って、もう一度投稿します

データベース (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 and when I click on show all files button on solution explorer after running application, i get new database called`ASPNETDB.mdf`

私の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 では、Login メソッドは次のとおりです。

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

    }

ValidateUserLogin上記のアクションで使用されているメソッドは、CodeFirstMembershipProviderから継承するクラス class にあります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;
                }
            }
        }

私のWeb構成で、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

0 に答える 0