Problem connecting to database - user instance and Entity Framework issue

若如初见. 提交于 2019-11-29 16:11:22

Read this very good overview of what user instances really are.

In a nutshell:

  • SQL Server allows you to have multiple instances of itself on a single computer - the default instance can be accessed using just the machine name (or IP address) of the host machine - all other instances need to be specified by an instance name (like the SQLExpress on your machine)

  • for development purposes, you can also create a user instance - each user gets their own separate instance of SQL Server (works in the Express edition only) and a database is attached by its file name (path\Sales_DB.mdf) to that user instance. This instance is started up on demand, then runs and is shut down when no longer needed

While this works great for development, it's not really intended for later on - certainly not for production use.

In a production environment, you want to attach your database files to the actual SQL Server instance they should run on - typically using SQL Server Management Studio. When you do this, you no longer refer to that database via its file name, but instead you use the database name that was given to the database when being attached to the server. In this case, SQL Server will handle all details of operations - and that SQL Server instance is up and running all the time (doesn't need to be started "on demand" all the time), and you can use things like permissions and logins to control access to the database.

Your connection string would then look something like this:

<connectionStrings>
    <add name="Sales_DBEntities" 
         connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string='server=YourServerNameHere;database=Sales_DB;integrated security=True;App=EntityFramework'" 
        providerName="System.Data.EntityClient" />
</connectionStrings>

When you have attached a database to a production (or testing) SQL Server instance, you do not need to know what the files are that make up the database, and you don't need to specify those files - SQL Server will take care of that. You just refer to the database via its database name.

eran otzap

The solution is always simpler then anticipated:

  1. double click the model.edmx file -> the designer will open.
  2. right click on the designer -> Update model from database .
  3. new connection -> Choose Database file (.mdf) under type .
  4. Browse -> go to your App_Data folder, and choose the DB.
  5. copy the connection string created in App.config to web.config.

bye.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!