0

I am not able to update database in PM console using code first.

I have done the following:

Each package is licensed to you by its owner. Microsoft is not responsible for, nor does it grant any licenses to, third-party packages. Some packages may include dependencies which are governed by additional licenses. Follow the package source (feed) URL to determine any dependencies.

Package Manager Console Host Version 2.12.0.817

Type 'get-help NuGet' to see all available NuGet commands.

PM> enable-migrations
Checking if the context targets an existing database...
Code First Migrations enabled for project Vidly.
PM> add-migration 'InitialModel'
Scaffolding migration 'InitialModel'.
The Designer Code for this migration file includes a snapshot of your current Code First model. This snapshot is used to calculate the changes to your model when you scaffold the next migration. If you make additional changes to your model that you want to include in this migration, then you can re-scaffold it by running 'Add-Migration InitialModel' again.
PM> add-migration InitialModel -force
Re-scaffolding migration 'InitialModel'.
PM> update-database
Specify the '-Verbose' flag to view the SQL statements being applied to the target database.
Applying explicit migrations: [201710241923025_InitialModel].
Applying explicit migration: 201710241923025_InitialModel.
System.Data.SqlClient.SqlException (0x80131904): Cannot attach the file 'C:\Users\Max\Documents\Visual Studio 2013\Projects\C#_Old\ExampleAsp\Wooly\Wooly\App_Data\aspnet-Wooly-20171013055250.mdf' as database 'aspnet-Wooly-20171013055250'.
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, UInt32 waitForMultipleObjectsTimeout, Boolean allowCreate, Boolean onlyOneCheckConnection, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionPool.TryGetConnection(DbConnection owningObject, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionFactory.TryGetConnection(DbConnection owningConnection, TaskCompletionSource`1 retry, DbConnectionOptions userOptions, DbConnectionInternal oldConnection, DbConnectionInternal& connection)
   at System.Data.ProviderBase.DbConnectionInternal.TryOpenConnectionInternal(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.ProviderBase.DbConnectionClosed.TryOpenConnection(DbConnection outerConnection, DbConnectionFactory connectionFactory, TaskCompletionSource`1 retry, DbConnectionOptions userOptions)
   at System.Data.SqlClient.SqlConnection.TryOpenInner(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.TryOpen(TaskCompletionSource`1 retry)
   at System.Data.SqlClient.SqlConnection.Open()
   at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.<Open>b__36(DbConnection t, DbConnectionInterceptionContext c)
   at System.Data.Entity.Infrastructure.Interception.InternalDispatcher`1.Dispatch[TTarget,TInterceptionContext](TTarget target, Action`2 operation, TInterceptionContext interceptionContext, Action`3 executing, Action`3 executed)
   at System.Data.Entity.Infrastructure.Interception.DbConnectionDispatcher.Open(DbConnection connection, DbInterceptionContext interceptionContext)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatementsInternal(IEnumerable`1 migrationStatements, DbConnection connection)
   at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClass30.<ExecuteStatements>b__2e()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.<>c__DisplayClass1.<Execute>b__0()
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute[TResult](Func`1 operation)
   at System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy.Execute(Action operation)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements, DbTransaction existingTransaction)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteStatements(IEnumerable`1 migrationStatements)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.ExecuteStatements(IEnumerable`1 migrationStatements)
   at System.Data.Entity.Migrations.DbMigrator.ExecuteOperations(String migrationId, VersionedModel targetModel, IEnumerable`1 operations, IEnumerable`1 systemOperations, Boolean downgrading, Boolean auto)
   at System.Data.Entity.Migrations.DbMigrator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.ApplyMigration(DbMigration migration, DbMigration lastMigration)
   at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
   at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
   at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
   at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
   at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
   at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
   at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
   at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
   at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
   at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
ClientConnectionId:fbcef5c3-f101-4ff7-8019-3a2733903d68
Error Number:1832,State:1,Class:14
Cannot attach the file 'C:\Users\Max\Documents\Visual Studio 2013\Projects\C#_Old\ExampleAsp\Wooly\Wooly\App_Data\aspnet-Wooly-20171013055250.mdf' as database 'aspnet-Wooly-20171013055250'.
PM> 

Please check the statement, unable to create database :

Cannot attach the file 'C:\Users\Max\Documents\Visual Studio 2013\Projects\C#_Old\ExampleAsp\Wooly\Wooly\App_Data\aspnet-Wooly-20171013055250.mdf' as database 'aspnet-Wooly-20171013055250'.

In 201710241923025_InitialModel.cs, under "Migrations" folder, the "dbo.Customers" is there :

 public override void Up()
        {
            CreateTable(
                "dbo.Customers",
                c => new
                    {
                        Id = c.Int(nullable: false, identity: true),
                        Name = c.String(),
                    })
                .PrimaryKey(t => t.Id);
.
.
.
.
.
.

Inside IdentityModels.cs, in folder Models, I have added "public DbSet<Customer> Customers { get; set; }", as shown in the following:

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public DbSet<Customer> Customers { get; set; }
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }

I am a newbie in ASP.NET MVC , please advice and help. Your effort is very much appreciated.

Thank you very much.

Cheers !

The following is my connection strings :

 <connectionStrings>
    <add name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\aspnet-Vidly-20171013055250.mdf;Initial Catalog=aspnet-Vidly-20171013055250;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>  

and

<entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework> 

Would you please advice how can I move from here ? Thank you very much Steve. Cheers !

newbuntu
  • 57
  • 6
  • Did you search about your error? For example I could find this https://stackoverflow.com/questions/13275054/ef5-cannot-attach-the-file-0-as-database-1 – Steve Oct 24 '17 at 19:57
  • or this one for Code-First https://stackoverflow.com/questions/15701935/asp-net-mvc4-code-first-cannot-attach-the-file-as-database-exception – Steve Oct 24 '17 at 19:59
  • @Steve. Thanks Steve. I am sorry about this. I check the post and follow the method. However, I still unable to see my .mdf file under the folder App_Data. Although I have successfully update the database : 'code' PM> update-database Specify the '-Verbose' flag to view the SQL statements being applied to the target database. Applying explicit migrations: [201710241923025_InitialModel]. Applying explicit migration: 201710241923025_InitialModel. Running Seed method. 'code' – newbuntu Oct 24 '17 at 20:09
  • Show your connection string. – Steve Greene Oct 24 '17 at 22:49
  • @Steve Greene , many thanks. I am sorry to tell out and ask. May I know what is connection string ? How can I retrieve it? or which part of a standard asp.net code have this ? I am still learning asp.net, code first. Thanks Steve Greene. – newbuntu Oct 25 '17 at 07:18
  • There are various techniques that tell EF what database to use. Start [here](https://msdn.microsoft.com/en-us/library/jj556606(v=vs.113).aspx) and [here](https://msdn.microsoft.com/en-us/library/jj592674(v=vs.113).aspx). – Steve Greene Oct 25 '17 at 15:13
  • @Steve Greene Many thanks. I will check it. Cheers ! – newbuntu Oct 27 '17 at 11:48
  • @Steve Greene , many thanks to you. It has been a while due to other projects. After check the article and my code, I have found the following in my web.config as shown in the above passage code <0>. Would you please advice how can I move from here ? Many thanks Steve. Cheers! – newbuntu Nov 01 '17 at 00:01

0 Answers0