Reputation: 8674
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:
Create DBcontext for that newly added database
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?
If yes I have a custom MembershipProvider class which uses the above SisContext class through which I'm trying to validate user
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.
When I run i get model backing the context has changed
error.
Any suggestion to solve my case?
Upvotes: 2
Views: 476
Reputation: 49
pass connectionstring in base.See below
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.
Upvotes: 2