更新:我删除并重新创建SQL Server后再次迁移,没有错误。所以似乎初始提供程序在模型快照中的某处编码。
更新
Created video to visualize the problem
我没有意识到这一点,但似乎 Entity Framework Code First Migrations 绑定到特定的DBMS。我在我的演示应用程序SQL Server Compact Edition 4.0中使用,我将它迁移到SQL Server 2014 Express LocalDB。
SQL Server中的数据库架构是正确创建的,但是当我尝试读取或写入数据库时出现以下错误
找不到具有不变名称“System.Data.SqlServerCe.4.0”的ADO.NET提供程序的实体框架提供程序。
我现在不在任何地方使用SQL Server CE,所有NuGet包都已卸载,提供程序已从App.Config
中删除。
的app.config
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<section name="entityFramework"
type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
requirePermission="false" />
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 --></configSections>
<connectionStrings configSource="ConnectionStrings.config" />
<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>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
ConnectionStrings.config
<?xml version="1.0" encoding="utf-8"?>
<connectionStrings>
<clear/>
<add name="CarRentalDb" connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=CarRentalDb;Integrated Security=True;multipleactiveresultsets=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
CarRentalDbContext.cs
public class CarRentalDbContext : DbContext
{
public CarRentalDbContext()
//only used until development, will not be checked in
: base(@"Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=CarRentalDb;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=True;ApplicationIntent=ReadWrite;MultiSubnetFailover=False")
{
Database.SetInitializer(new CarRentalDbInitializer());
}
public CarRentalDbContext(string nameOrConnectionString) : base(nameOrConnectionString)
{
Database.SetInitializer(new CarRentalDbInitializer());
}
}
dbo._MigrationHistories
现在,当我删除 dbo._MigrationHistories 表时,错误消失了。我是否需要重新创建迁移以解决SQL Server而不是SQL Server CE?
NewCustomerIsCreated.cs
[TestMethod]
public void NewCustomerIsCreated()
{
var customerLastName = CustomersLastNameToBeDeleted;
//Arrange
var systemUnderTest = new CarRentalBusinessLayer();
var customerToBeCreated = _autoFixture.Build<CustomerModel>()
.Without(property => property.CustomerId)
.With(property => property.CustomerType, CustomerModel.Consumer)
.With(property => property.LastName, customerLastName).Create();
var expectedResult = customerToBeCreated;
//Act
systemUnderTest.CreateNewCustomer(customerToBeCreated.FirstName, customerToBeCreated.LastName, customerToBeCreated.DateOfBirth, customerToBeCreated.Street, customerToBeCreated.City, customerToBeCreated.Postcode,
customerToBeCreated.CustomerType);
//Assert
Customer actualResult;
using (var carRentalDbContext = new CarRentalDbContext(_connectionString))
{
actualResult = carRentalDbContext.Customers.SingleOrDefault(c => c.LastName == customerLastName);
}
Assert.IsTrue(actualResult != null && expectedResult.LastName == actualResult.LastName);
}