简体   繁体   English

实体框架和SQL Server数据库连接字符串

[英]Entity Framework and SQL Server database connection string

I have worked with EF for a while now and i have always used LocalDb's for storing data. 我已经与EF合作了一段时间,并且我一直使用LocalDb来存储数据。 I want to start working with SQL Server databases instead but I'm having some issues setting up the connection string. 我想开始使用SQL Server数据库,但是在设置连接字符串时遇到了一些问题。

https://msdn.microsoft.com/en-us/library/jj653752(v=vs.110).aspx#sse https://www.connectionstrings.com/ https://msdn.microsoft.com/zh-CN/library/jj653752(v=vs.110).aspx#sse https://www.connectionstrings.com/

and looked over google but none of the answers made it work in my case so I must be doing something wrong (some of the connections strings throw an exception others didn't but wouldn't insert anything either into the database neither) 并查看了google,但在我的情况下,没有任何答案使它正常工作,因此我必须做错了事(某些连接字符串引发异常,其他人则没有,但也不会向数据库中插入任何内容)

My question is when working with EF & SQL Server, should I use both the connection string in the App.config & setting the path of the DB in the CTOR of the context (by using AppDomain.CurrentDomain.SetData("DataDirectory", path); ) or is the app.config sufficient ? 我的问题是使用EF和SQL Server时,我是否应该同时使用App.config的连接字符串和设置上下文的CTOR中数据库的路径(通过使用AppDomain.CurrentDomain.SetData("DataDirectory", path); )或app.config足够?

I have tried the following connection strings: 我尝试了以下连接字符串:

Data Source=.\GURUBEAST-PC\GURUSQL;Initial Catalog=iManager;Trusted_Connection=True;MultipleActiveResultSets=True;

Data Source=.\GURUBEAST-PC\GURUSQL;Database=iManager;Integrated Security=True;Trusted_Connection=True;MultipleActiveResultSets=True;

Data Source=.\GURUBEAST-PC\GURUSQL;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL11.GURUSQL\MSSQL\DATA\iManager.mdf;Database=iManager;Trusted_Connection=True;MultipleActiveResultSets=True;

Data Source=.\GURUBEAST-PC\GURUSQL;AttachDbFilename=C:\Program Files\Microsoft SQL Server\MSSQL11.GURUSQL\MSSQL\DATA\iManager.mdf;Database=iManager;Trusted_Connection=True;

Data Source=.\GURUBEAST-PC\GURUSQL;Database=iManager;Trusted_Connection=True;

Data Source=.\GURUBEAST-PC\GURUSQL;Initial Catalog=iManager;Integrated Security=SSPI;

Data Source=.\GURUBEAST-PC\GURUSQL;Initial Catalog=iManager;User id=GURUBEAST-PC\GuruBeast;

Where "iManager" is the name of the database. 其中“ iManager”是数据库的名称。 I use Windows auth for my SQL Server instance. 我将Windows身份验证用于SQL Server实例。

What am I doing wrong ? 我究竟做错了什么 ? Should I set my path to the program files folder or the App_Data (I have seen both and tried both but both didn't work)? 我应该将路径设置为程序文件文件夹还是App_Data (我已经看过两个并且都尝试过,但是都没有用)?

Kind regards! 亲切的问候!

Once you get your host name figured out, Entity Framework will generate your connection string for you. 一旦弄清主机名,Entity Framework就会为您生成连接字符串。 Here's a sample of what your connection string could look like if you were attempting to connect to AdventureWorks database hosted on your local instance of SQL Server 2014 aptly named sql2014. 如果您尝试连接到适当命名为sql2014的SQL Server 2014本地实例上托管的AdventureWorks数据库,则以下示例显示了连接字符串的外观。

<connectionStrings>
    <add name="AdventureWorksEntities" connectionString="metadata=res://*/DataModels.AdventureWorksDb.csdl|res://*/DataModels.AdventureWorksDb.ssdl|res://*/DataModels.AdventureWorksDb.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=.\sql2014;initial catalog=AdventureWorks;persist security info=True;user id=App_AdventureWorks;password=asdasdfasdfasdf;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient" />
</connectionStrings>

Your db context would then look something like this.Again, EF generates this for you. 然后您的数据库上下文看起来像这样.EF再一次为您生成了这个。

public partial class AdventureWorksEntities : DbContext
{
    public AdventureWorksEntities()
        : base("name=AdventureWorksEntities")
    {
    }

The Data Source key is used to find the machine on which the Sql Server instance runs. 数据源键用于查找运行Sql Server实例的计算机。
You can have different strings for it but the most common used in a LAN environment is composed using the name of the server machine followed by an eventual instance name. 您可以使用不同的字符串,但是在LAN环境中最常用的字符串是使用服务器计算机的名称以及最终的实例名称组成的。

So, if your local PC is named GURUBEAST-PC and, at install time, you haven't specified any instance name, the connectionstring Data Source contains only the name of the machine GURUBEAST-PC . 因此,如果您的本地PC名为GURUBEAST-PC,并且在安装时未指定任何实例名称,则连接字符串Data Source仅包含计算机GURUBEAST-PC的名称。 If you have an instance name then you should add that instance name to you Data Source key. 如果您有实例名称,则应将该实例名称添加到数据源键中。 GURUBEAST-PC\\GURUSQL GURUBEAST-PC \\ GURUSQL

This will guarantee to all the PC in the same LAN the possibility to have the same connectionstring also if the connection is made from the same PC where the SQL Server runs. 如果连接是从运行SQL Server的同一台PC进行的,则这将保证同一LAN中的所有PC都有相同的连接字符串的可能性。

If the Data Source points at the local pc, you can use many shortcuts to represent the local PC: 如果数据源指向本地PC,则可以使用许多快捷方式来表示本地PC:

(LOCAL)
localhost
.
\.

and eventually add the instance name to these shortcuts without repeating the PC name 并最终将实例名称添加到这些快捷方式中,而无需重复PC名称

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实体框架数据库优先和100多个SQL Server数据库的动态连接字符串 - Entity Framework database first and dynamic connection string for 100+ SQL Server databases 实体框架中 SQL Server Express 数据库没有计算机用户 ID 密码的连接字符串 - Connection string without computer User ID-Password for SQL Server Express database in Entity Framework Linux Entity Framework Core上的SQL Server连接字符串无效 - Invalid SQL Server Connection String on Linux Entity Framework Core 实体框架代码的SQL Server Compact 4.0连接字符串首先? - SQL Server Compact 4.0 connection string for Entity Framework code first? 连接字符串C#实体框架SQL Server Express - Connection string C# Entity Framework SQL Server Express 数据库优先和实体框架动态连接字符串 - Database first and Entity Framework Dynamic Connection String 使用实体框架测试与SQL Server的连接 - Test connection to SQL Server with Entity Framework 实体框架连接字符串“找不到服务器” - Entity Framework Connection String “The Server was not found” 实体框架到远程服务器(连接字符串) - Entity Framework To A Remote Server (Connection String) 远程服务器上的实时实体框架连接字符串 - Live entity framework connection string on remote server
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM