Julian Dormon
Julian Dormon

Reputation: 1779

MVC EF5 Framework Initialize Database Error

I am using EF 5 and MVC 4.

After I first deploy the project (using web deploy) and the website is launched, my database is automatically created and I can see that the simplemembership tables are created. All my other tables are not. The simplemembership tables and my tables are combined into one DB. I did not use two separate DBs. This works fine in my development environment.

When I go to a controller that lists another table's data (not part of simplemembership) I receive the following error:

    [SqlException (0x80131904): There is already an object named 'UserProfile' in the database.]
   System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction) +388
   System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose) +688
   System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean& dataReady) +4403
   System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout) +2755286
   System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, String methodName, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite) +527
   System.Data.SqlClient.SqlCommand.ExecuteNonQuery() +290
   System.Data.Entity.Migrations.DbMigrator.ExecuteSql(DbTransaction transaction, MigrationStatement migrationStatement) +247
   System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements) +202
   System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements) +19
   System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration) +472
   System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) +175
   System.Data.Entity.Migrations.Infrastructure.MigratorBase.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId) +19
   System.Data.Entity.MigrateDatabaseToLatestVersion`2.InitializeDatabase(TContext context) +150
   System.Data.Entity.<>c__DisplayClass2`1.<SetInitializerInternal>b__0(DbContext c) +97
   System.Data.Entity.Internal.<>c__DisplayClass8.<PerformDatabaseInitialization>b__6() +23
   System.Data.Entity.Internal.InternalContext.PerformInitializationAction(Action action) +66
   System.Data.Entity.Internal.InternalContext.PerformDatabaseInitialization() +225
   System.Data.Entity.Internal.RetryAction`1.PerformAction(TInput input) +208
   System.Data.Entity.Internal.LazyInternalContext.InitializeDatabaseAction(Action`1 action) +235
   System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) +36
   System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() +71
   System.Data.Entity.Internal.Linq.InternalSet`1.GetEnumerator() +21
   System.Data.Entity.Infrastructure.DbQuery`1.System.Collections.Generic.IEnumerable<TResult>.GetEnumerator() +62
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +446
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +80
   ProofPixModels.Controllers.SubscriberController.Index() in SubscriberController.cs:21
   lambda_method(Closure , ControllerBase , Object[] ) +43
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +248
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39
   System.Web.Mvc.Async.AsyncControllerActionInvoker.InvokeSynchronousActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +15
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +15
   System.Web.Mvc.Async.<>c__DisplayClass39.<BeginInvokeActionMethodWithFilters>b__33() +120
   System.Web.Mvc.Async.<>c__DisplayClass4f.<InvokeActionMethodFilterAsynchronously>b__49() +452
   System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeActionMethodWithFilters>b__36(IAsyncResult asyncResult) +15
   System.Web.Mvc.Async.<>c__DisplayClass2a.<BeginInvokeAction>b__20() +32
   System.Web.Mvc.Async.<>c__DisplayClass25.<BeginInvokeAction>b__22(IAsyncResult asyncResult) +230
   System.Web.Mvc.<>c__DisplayClass1d.<BeginExecuteCore>b__18(IAsyncResult asyncResult) +28
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +15
   System.Web.Mvc.<>c__DisplayClass8.<BeginProcessRequest>b__3(IAsyncResult asyncResult) +42
   System.Web.Mvc.Async.<>c__DisplayClass4.<MakeVoidDelegate>b__3(IAsyncResult ar) +15
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +14
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +288

There are obviously two InitializeDatabase commands going on here, but I cannot figure out why the clash over this one table. Any help would be greatly appreciated.

Upvotes: 3

Views: 2896

Answers (4)

Chamilgreat
Chamilgreat

Reputation: 1

Had same issue then manually edited the Initialmigration script with drop table UserProfile at top and executed Update-Database -Verbose. Failed as there is n foreign key constraint. Went to my local db deleted UserProfile table with all tables having foreign keys to that. Executed the migration and table created.

Upvotes: 0

ZaneDarken
ZaneDarken

Reputation: 789

Same as above, if you have already run an asp-net membership script table, you will need to clean this up by dropping all these other auto-gen tables. Once you do that you can run the command update-database -verbose and you will see it create the UserProfile table no problem.

Upvotes: 3

Anuj Pandey
Anuj Pandey

Reputation: 938

This would also happen if you already have asp-net membership tables created earlier, which tries to prevent you from accidentally loosing the credentials for all users.

Drop those tables and views and this error goes away.

Upvotes: 1

Julian Dormon
Julian Dormon

Reputation: 1779

I discovered that the problem was caused in the migrations file. One of my models included a collection of UserProfile(s) so this made EF create a Foreign Key which seems to force EF to write code to create the table. It is unaware that the simplemembership will do this for this table apparently.

Anyway I solved the problem by commenting out the create UserProfile table block in the migrations folder. I did make sure to replace it with an AddForeignKey method to keep the relationship intact.

Not sure this is the correct way, but it worked.

Upvotes: 2

Related Questions