I'm using this InitializeDatabaseConnection in my global.cs file but I keep getting an error:
There is already an object named 'Users' in the database.
when I try use my DB context
protected void Application_Start() { AreaRegistration.RegisterAllAreas(); FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters); RouteConfig.RegisterRoutes(RouteTable.Routes); BundleConfig.RegisterBundles(BundleTable.Bundles); ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory()); ModelBinders.Binders.Add(typeof (MasterUserSessionModel), new MasterModelBinder()); AuthConfig.RegisterAuth(); WebSecurity.InitializeDatabaseConnection("db", "Users", "Id", "UserName", autoCreateTables: true); }
public class DB : DbContext, IDbContext { public DB() : base("db") { this.Configuration.LazyLoadingEnabled = false; } public DbSet<User> Users { get; set; } public DbSet<Company> Companies { get; set; } // public DbSet<User> CustomProfiles { get; set; } public DbSet<Investor> Investors { get; set; } protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<User>() .HasMany(u => u.InvestorsFollowing) .WithMany(i => i.FollowingUsers); modelBuilder.Entity<User>() .HasMany(u => u.CompaniesFollowing) .WithMany(i => i.FollowingUsers); Database.SetInitializer(new MigrateDatabaseToLatestVersion<DB, Configuration>()); base.OnModelCreating(modelBuilder); //try //{ // using (var context = new DB()) // { // if (!context.Database.Exists()) // { // // Create the SimpleMembership database without Entity Framework migration schema // ((IObjectContextAdapter)context).ObjectContext.CreateDatabase(); // } // } // WebSecurity.InitializeDatabaseConnection("db", "Users", "UserId", "UserName", autoCreateTables: true); //} //catch (Exception ex) //{ // throw new InvalidOperationException("The ASP.NET Simple Membership database could not be initialized. For more information, please see http://go.microsoft.com/fwlink/?LinkId=256588", ex); //} } public IQueryable<T> Find<T>() where T : class { return this.Set<T>(); } public void Rollback() { this.ChangeTracker.Entries().ToList().ForEach(x => x.Reload()); } }