sql server - LocalDB .mdf file not attached automatically with AttachDbFileName in connection string -


i'm expecting behavior because of this msdn article attachdbfilename property

the first time connect database using option in connection string, sql server express or localdb instance attaches database, , stays attached. when want connect same database in future use initial catalog without attachdbfilename if prefer.

i have connection string used entity framework:

<add name="databasecontext" providername="system.data.sqlclient" connectionstring="server=(localdb)\mssqllocaldb;database=databasename;integrated security=true;attachdbfilename=|databasename|\databasename.mdf" /> 

this works fine after open database in sql management studio feeding attachdbfilename value absolute path additional prameter.

but if don't attached sql management studio first i'm getting following error when trying run ef migrations on database:

system.data.sqlclient.sqlexception (0x80131904): cannot create file 'c:\users\user\desktop\folder\databasename.mdf' because exists. change file path or file name, , retry operation. create database failed. file names listed not created. check related errors.    @ system.data.sqlclient.sqlconnection.onerror(sqlexception exception, boolean breakconnection, action`1 wrapcloseinaction)    @ system.data.sqlclient.sqlinternalconnection.onerror(sqlexception exception, boolean breakconnection, action`1 wrapcloseinaction)    @ system.data.sqlclient.tdsparser.throwexceptionandwarning(tdsparserstateobject stateobj, boolean callerhasconnectionlock, boolean asyncclose)    @ system.data.sqlclient.tdsparser.tryrun(runbehavior runbehavior, sqlcommand cmdhandler, sqldatareader datastream, bulkcopysimpleresultset bulkcopyhandler, tdsparserstateobject stateobj, boolean& dataready)    @ system.data.sqlclient.sqlcommand.runexecutenonquerytds(string methodname, boolean async, int32 timeout, boolean asyncwrite)    @ system.data.sqlclient.sqlcommand.internalexecutenonquery(taskcompletionsource`1 completion, string methodname, boolean sendtopipe, int32 timeout, boolean asyncwrite)    @ system.data.sqlclient.sqlcommand.executenonquery()    @ system.data.entity.infrastructure.interception.dbcommanddispatcher.<nonquery>b__0(dbcommand t, dbcommandinterceptioncontext`1 c)    @ system.data.entity.infrastructure.interception.internaldispatcher`1.dispatch[ttarget,tinterceptioncontext,tresult](ttarget target, func`3 operation, tinterceptioncontext interceptioncontext, action`3 executing, action`3 executed)    @ system.data.entity.infrastructure.interception.dbcommanddispatcher.nonquery(dbcommand command, dbcommandinterceptioncontext interceptioncontext)    @ system.data.entity.sqlserver.sqlproviderservices.<>c__displayclass1a.<createdatabasefromscript>b__19(dbconnection conn)    @ system.data.entity.sqlserver.sqlproviderservices.<>c__displayclass33.<usingconnection>b__32()    @ system.data.entity.sqlserver.defaultsqlexecutionstrategy.<>c__displayclass1.<execute>b__0()    @ system.data.entity.sqlserver.defaultsqlexecutionstrategy.execute[tresult](func`1 operation)    @ system.data.entity.sqlserver.defaultsqlexecutionstrategy.execute(action operation)    @ system.data.entity.sqlserver.sqlproviderservices.usingconnection(dbconnection sqlconnection, action`1 act)    @ system.data.entity.sqlserver.sqlproviderservices.usingmasterconnection(dbconnection sqlconnection, action`1 act)    @ system.data.entity.sqlserver.sqlproviderservices.createdatabasefromscript(nullable`1 commandtimeout, dbconnection sqlconnection, string createdatabasescript)    @ system.data.entity.sqlserver.sqlproviderservices.dbcreatedatabase(dbconnection connection, nullable`1 commandtimeout, storeitemcollection storeitemcollection)    @ system.data.entity.core.common.dbproviderservices.createdatabase(dbconnection connection, nullable`1 commandtimeout, storeitemcollection storeitemcollection)    @ system.data.entity.core.objects.objectcontext.createdatabase()    @ system.data.entity.migrations.utilities.databasecreator.create(dbconnection connection)    @ system.data.entity.migrations.dbmigrator.ensuredatabaseexists(action mustsucceedtokeepdatabase)    @ system.data.entity.migrations.dbmigrator.update(string targetmigration)    @ system.data.entity.migrations.infrastructure.migratorbase.update() 

this looks if doesn't attach database automatically reason , tries create if file didn't exist.

is there additional connection string option or localdb configuration can fix database attached automatically on first conneciton?

the answer trivial: .mdf , .ldf files created in newer version of sql server older version not attaching them if didn't exist.


Comments

Popular posts from this blog

magento2 - Magento 2 admin grid add filter to collection -

Android volley - avoid multiple requests of the same kind to the server? -

Combining PHP Registration and Login into one class with multiple functions in one PHP file -