简体   繁体   中英

Entity Framework 6 - can't connect to database server

This row causes the exception:

var query = from customer in ctx.Customers select customer;

Exception:

An unhandled exception of type 'System.Data.Entity.Core.ProviderIncompatibleException' occurred in EntityFramework.dll

An error occurred while getting provider information from the database. This can be caused by Entity Framework using an incorrect connection string. Check the inner exceptions for details and ensure that the connection string is correct.

Connection string from Database.Connection.ConnectionString :

Data Source=.\SQLEXPRESS;Initial Catalog=Customer.CustomersContext;Integrated Security=True;MultipleActiveResultSets=True

App.config :

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="v11.0" />
      </parameters>
    </defaultConnectionFactory>
  </entityFramework>
  <connectionStrings>
  <add name="Customer.CustomersContext" providerName="System.Data.SqlClient" connectionString="Data Source=(localdb)\Projects;Initial Catalog=master;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False" />
</connectionStrings>
</configuration>

Context class:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace Customer
{
    class CustomersContext : DbContext
    {
        public CustomersContext() : base("Customer.CustomersContext")
        {
            System.Console.WriteLine(Database.Connection.ConnectionString);
        }

        public DbSet<CustomerDb> Customers { get; set; }
        public DbSet<Contact> Contacts { get; set; }
    }
}

You can see I even tried to pass connection string name from app.config to context constructor (localdb should be used), but the printed connection string is still SQLExpress .

Have a look at this . If you are using localdb, you need to accordingly change your connection string. So, your connection string should be Data Source=(localdb)\\v11.0;Integrated Security=true;AttachDbFileName=C:\\Path-to-your-database.mdf

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