使用EF连接现有数据库

时间:2022-10-27 09:46:14

I had a project which contains a Database(SIS.mdf) in its App_Data folder. Now I have a new MVC4 project. I manually added that database to my new App_Data folder by add existing item option on right click of App_Data folder. After that I did following things:

我有一个项目,它在App_Data文件夹中包含一个数据库(si .mdf)。现在我有一个新的MVC4项目。通过在App_Data文件夹的右键单击中添加现有项目选项,我手动将该数据库添加到新的App_Data文件夹。之后我做了以下的事情:

  1. Create DBcontext for that newly added database

    为新添加的数据库创建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: Is it possible now to interact with the database(SIS) that is in my App_Data folder which exactly contains same and more tables mentioned above?

问1:现在有可能与我的App_Data文件夹中的数据库(SIS)进行交互吗?

If yes I have a custom MembershipProvider class which uses the above SisContext class through which I'm trying to validate user

如果有,我有一个自定义的MembershipProvider类,它使用上面的SisContext类,通过这个类我试图验证用户

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

In my web config I have added my customMembership provider and also have the connection to my SIS database.

在我的web配置中,我添加了我的customMembership provider,并且还连接到我的SIS数据库。

When I run i get model backing the context has changed error. Any suggestion to solve my case?

当我运行时,我得到支持上下文的模型已经更改了错误。有什么解决我案子的建议吗?

1 个解决方案

#1


2  

pass connectionstring in base.See below

通过connectionstring基地。见下文

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

right connectionstring should be defined in web.config.

正确的connectionstring应该在web.config中定义。

#1


2  

pass connectionstring in base.See below

通过connectionstring基地。见下文

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

right connectionstring should be defined in web.config.

正确的connectionstring应该在web.config中定义。