K
K
Konstantin2018-09-20 01:04:36
C++ / C#
Konstantin, 2018-09-20 01:04:36

What to fix in Entity Framework C# database connection string?

There is a database on SQL Server'e. There is an application that works with this database using this connection string:

<connectionStrings>
    <add name="IPhoneDBContext" connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=DESKTOP-K4U1293;initial catalog=IPhoneDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
  </connectionStrings>
  <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>

In the application itself, I created a connection through 5ba2c6cad00ff694705304.pngProblem : I tried to move the database files to the folder with the application and try to connect through different connection strings, but the application threw some exceptions. Is there a database connection string that is located in the working directory of the program? All this is done so that it is possible to transfer the application + database to other machines. C# language.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
basrach, 2018-09-22
@basrach

In order to transfer the database, you need to choose what to use first:
1) Sql Server CE (Compact Edition) - a stripped-down engine that you can carry with you as a library. But he has been deprecated since the age of 13.
2) Sql Server LocalDB. But it needs to be installed separately on each machine. It used to come with VisualStudio, I don't know now.
3) sqlite, which will work everywhere, but it has its drawbacks.
4) Use a regular Sql Server instance (maybe Express, it doesn't matter), and carry only the database with you.
Depending on what you choose, the connection string will change. Now you have it for a regular Sql Server instance:

data source=DESKTOP-K4U1293;initial catalog=IPhoneDB;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework
.
For example, for option 4, you need to change it to:
Server=DESKTOP-K4U1293;AttachDbFilename=|DataDirectory|mydbfile.mdf;Database=IPhoneDB;
Trusted_Connection=Yes;
.
For option 2:
Server=(localdb)\v11.0;Integrated Security=true;
AttachDbFileName=|DataDirectory|mydbfile.mdf;

There are many examples here.

D
Dmitry Kovalsky, 2018-09-20
@dmitryKovalskiy

So I wrote a screenshot for you. What else do you want? According to the screenshot, your models are explicitly described by the CodeFirst approach, and the shown connection string has links to class mapping models - entities in the database (DatabaseFirst). There is either a cross or shorts. As far as I remember the CodeFirst approach - the connection string should contain only the location of the database server and details sufficient to manipulate both data and table structures. Regarding the transfer, I didn’t understand and didn’t see the problem at all, these are already some of your fantasies. The application contains a config, it contains the connection strings with the database. What else do you want? Yes, when deploying a project - in the resulting distribution, or whatever you put there, there will be a config containing a connection string

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question