none
Multi User problems with Entity Framework RRS feed

  • Question

  • i have 2 pc and im trying to access the database from the other pc. The connection string works well if only one is currently running but when i try to use both pc it give me an error whenever i try to access it. Only one works and other doesnt. Its says "cannot create file because it already exist. Change the file path or the file name, and retry the operation".

    I added this connection string on my app.config

     <connectionStrings>
        <add name="MainDBContext" connectionString ="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\DeLL\Desktop\BackUp\DB\ServiceDept.Model.MainDBContext.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient"/>
      </connectionStrings>

    This all my app.config codes

    <?xml version="1.0" encoding="utf-8"?>
    <configuration>
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
      </configSections>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
      </startup>
       <connectionStrings>
        <add name="MainDBContext" connectionString ="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\DeLL\Desktop\BackUp\DB\ServiceDept.Model.MainDBContext.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient"/>
      </connectionStrings>
      <entityFramework>
        <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
          <parameters>
            <parameter value="mssqllocaldb" />
          </parameters>
        </defaultConnectionFactory>
        <providers>
          <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
        </providers>
      </entityFramework>

    </configuration>

    • Edited by im.Juke Monday, September 10, 2018 2:24 PM
    Monday, September 10, 2018 9:25 AM

Answers

  • <add name="MainDBContext" connectionString ="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\DeLL\Desktop\BackUp\DB\ServiceDept.Model.MainDBContext.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient"/>

    Well, that's a problem for you, becuase using Localdb pointing to a detached MDF file from the MS SQL Express Database Engine means that only one program can have the MDF file open at any given time since Localdb is a single user instance DB solution. 

    You will need to move the MDF and LDF  files to the Data directory that is being discussed at the bottom of the below link.

    https://docs.microsoft.com/en-us/sql/sql-server/install/file-locations-for-default-and-named-instances-of-sql-server?view=sql-server-2017

    The folder structure is about the same for all versions on MS SQL Server Express.

    You will then use SSMS to attach the MDF file to the database engine so that the MDF file can be used in a multiple user environment.

    Of course you will need to change the connectionstring to stop using Localdb and use a MS SQL Server Express connectionstring, based on server name=machine name and catalog name = database name.

    You will also need to configure MS SQL Server Express for remote connections, if you expect a program on a machine to remotely access MS SQL Server Express being hosted on a machine on the LAN.

    The below is where you should post for help.

      https://social.msdn.microsoft.com/Forums/sqlserver/en-US/home?forum=sqlexpress


    • Marked as answer by im.Juke Tuesday, September 11, 2018 6:03 AM
    Monday, September 10, 2018 3:57 PM
  • Hi im.Juke,

    If multiple client connect one database, I would suggest that you could consider SQL Server, which connection string will be like below with IP address:

    Server=myServerAddress;Database=myDataBase;User Id=myUsername;
    Password=myPassword;

    Best regards,

    Zhanglong 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by im.Juke Tuesday, September 11, 2018 6:03 AM
    Tuesday, September 11, 2018 3:27 AM
    Moderator
  • How can centralize the connection string?Only thing i know is how to connect it on my pc alone.What will i do if i will try to use same database and use other pc?What would be the connection string?

     

    Again, I'll advise you to post to the correct forum for help.

    1) You do not know the basics about MS SQL Server Express database administration, which you must know now. Without you knowing the basics, which the forum can guide you, then all you'll be doing is banging your head up against the wall. 

    2) You don't know the basics about the who, what, when, where,  how and why about the different variations of how to construction a connectionstring so that your program can use the MS SQL Server Express database. 

    So far, you have been lucky,  and you have stumbled into making it this far in using LocalDb, bravo. But be true to yourself, becuase you don't know what Localdb is about that you stumbled into which is not a multiple user database solution and is only a cutdown version of the MS SQL Server Express Database Engine.

    https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-2016-express-localdb?view=sql-server-2017

    Now you need to change gears. And you need to post to the correct forum for help.

    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/home?forum=sqlexpress

    • Marked as answer by im.Juke Tuesday, September 11, 2018 6:03 AM
    Tuesday, September 11, 2018 5:17 AM

All replies

  • Oh, maybe Houdini can pull that error message out of the magical hat, since you didn't bother to post it.  
    • Edited by DA924x Monday, September 10, 2018 10:52 AM
    Monday, September 10, 2018 10:51 AM
  • Oh, maybe Houdini can pull that error message out of the magical hat, since you didn't bother to post it.  
    Sorry. Well there i already edited the question. Im using code first
    • Edited by im.Juke Monday, September 10, 2018 2:26 PM
    Monday, September 10, 2018 2:21 PM
  • <add name="MainDBContext" connectionString ="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\DeLL\Desktop\BackUp\DB\ServiceDept.Model.MainDBContext.mdf;Integrated Security=True;Connect Timeout=30" providerName="System.Data.SqlClient"/>

    Well, that's a problem for you, becuase using Localdb pointing to a detached MDF file from the MS SQL Express Database Engine means that only one program can have the MDF file open at any given time since Localdb is a single user instance DB solution. 

    You will need to move the MDF and LDF  files to the Data directory that is being discussed at the bottom of the below link.

    https://docs.microsoft.com/en-us/sql/sql-server/install/file-locations-for-default-and-named-instances-of-sql-server?view=sql-server-2017

    The folder structure is about the same for all versions on MS SQL Server Express.

    You will then use SSMS to attach the MDF file to the database engine so that the MDF file can be used in a multiple user environment.

    Of course you will need to change the connectionstring to stop using Localdb and use a MS SQL Server Express connectionstring, based on server name=machine name and catalog name = database name.

    You will also need to configure MS SQL Server Express for remote connections, if you expect a program on a machine to remotely access MS SQL Server Express being hosted on a machine on the LAN.

    The below is where you should post for help.

      https://social.msdn.microsoft.com/Forums/sqlserver/en-US/home?forum=sqlexpress


    • Marked as answer by im.Juke Tuesday, September 11, 2018 6:03 AM
    Monday, September 10, 2018 3:57 PM
  • Hi im.Juke,

    If multiple client connect one database, I would suggest that you could consider SQL Server, which connection string will be like below with IP address:

    Server=myServerAddress;Database=myDataBase;User Id=myUsername;
    Password=myPassword;

    Best regards,

    Zhanglong 


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by im.Juke Tuesday, September 11, 2018 6:03 AM
    Tuesday, September 11, 2018 3:27 AM
    Moderator
  • How can centralize the connection string?Only thing i know is how to connect it on my pc alone.What will i do if i will try to use same database and use other pc?What would be the connection string?

     
    Tuesday, September 11, 2018 3:35 AM
  • How can centralize the connection string?Only thing i know is how to connect it on my pc alone.What will i do if i will try to use same database and use other pc?What would be the connection string?

     

    Again, I'll advise you to post to the correct forum for help.

    1) You do not know the basics about MS SQL Server Express database administration, which you must know now. Without you knowing the basics, which the forum can guide you, then all you'll be doing is banging your head up against the wall. 

    2) You don't know the basics about the who, what, when, where,  how and why about the different variations of how to construction a connectionstring so that your program can use the MS SQL Server Express database. 

    So far, you have been lucky,  and you have stumbled into making it this far in using LocalDb, bravo. But be true to yourself, becuase you don't know what Localdb is about that you stumbled into which is not a multiple user database solution and is only a cutdown version of the MS SQL Server Express Database Engine.

    https://docs.microsoft.com/en-us/sql/database-engine/configure-windows/sql-server-2016-express-localdb?view=sql-server-2017

    Now you need to change gears. And you need to post to the correct forum for help.

    https://social.msdn.microsoft.com/Forums/sqlserver/en-US/home?forum=sqlexpress

    • Marked as answer by im.Juke Tuesday, September 11, 2018 6:03 AM
    Tuesday, September 11, 2018 5:17 AM