简体   繁体   中英

Connection string to SQL by EntityFramework and MVC

I write web page in MVC, which use Entity Framework as ORM mapper. I put my connection string to web.config file. Here is it:

<add name="JP_CMS" connectionString="Data Source=.;Initial Catalog=JP_CMS;
User Id=CmsWebUser;Password=abcd1234abcd1234;"
providerName="System.Data.SqlClient"/>

I try to get data by this C# code

    public DataBaseContext()
        : base("JP_CMS")
    {
        Database.SetInitializer(new DropCreateDatabaseIfModelChanges<DataBaseContext>());
    }

//------------------------------

    var db = new DataBaseContext();
    var result = db.Articles.ToList(); //<- here is thrown exception
//------------------------------

My local SQL server is running, this table is exists, but I get excepion like in this:

在此处输入图片说明

I think it is the most important expection: "A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"

Could you help my by give some idea what I make wrong?

"The system cannot find the file specified" error. Check carefully your connection string.

Instead of DataSource =. try DataSource = .\\sqlInstanceName . Also DataSource=(local) might work.

hi Error indicates EF tried to connect to the server but failed.

Could you Checkk,

1) SQL Server should be up and running.

Go to All Programs >> Microsoft SQL Server 2008 >> Configuration Tools >> SQL Server Configuration Manager >> SQL Server Services, and check if SQL Server service status is “Running”.

In addition, ensure that your remote server is in the same network. Run “sqlcmd -L” in your command prompt to ascertain if your server is included in your network list.

2) Enable TCP/IP in SQL Server Configuration

For more info you can go here

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM