2

App_Data フォルダーにデータベース (SIS.mdf) を含むプロジェクトがありました。これで、新しい MVC4 プロジェクトができました。App_Data フォルダーを右クリックして既存の項目を追加するオプションを使用して、そのデータベースを新しい App_Data フォルダーに手動で追加しました。その後、次のことを行いました。

  1. その新しく追加されたデータベースの DBcontext を作成します

    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")//SIS is name of the database
        {
            if (HttpContext.Current == null)
            {
                Database.SetInitializer<SisContext>(null);
            }
        }
    }
    

Q1: App_Data フォルダーにあるデータベース (SIS) とやり取りすることは可能ですか? このフォルダーには、上記とまったく同じテーブルと、それ以上のテーブルが含まれていますか?

はいの場合、ユーザーを検証しようとしている上記の SisContext クラスを使用するカスタム MembershipProvider クラスがあります

public class CodeFirstMembershipProvider : MembershipProvider
{
   public override bool ValidateUser(string username, string password)
    {
        if (string.IsNullOrEmpty(username))
        {
            return false;
        }
        if (string.IsNullOrEmpty(password))
        {
            return false;
        }
        using (var Context = new SisContext())
        {

            AspNetUser User = Context.AspNetUsers.FirstOrDefault(Usr => Usr.Username == username);
            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 構成では、customMembership プロバイダーを追加し、SIS データベースにも接続しています。

実行するとmodel backing the context has changedエラーが発生します。私のケースを解決するための提案はありますか?

4

1 に答える 1

2

ベースに接続文字列を渡します。以下を参照してください

public SisContext():base(connectionstring)//SIS is name of the database
    {
        if (HttpContext.Current == null)
        {
            Database.SetInitializer<SisContext>(null);
        }
    }

正しい接続文字列は web.config で定義する必要があります。

于 2013-09-28T10:06:34.797 に答える