我不能使用.NET Portable的Code First流利API吗?
问题描述:
我创建了一个可移植类库针对.NETPortable, v4.0
我不能使用.NET Portable的Code First流利API吗?
在下面的类
public class AddressConfiguration : EntityTypeConfiguration<Address>
{
public AddressConfiguration()
{
ToTable("Addresses");
HasKey(c => c.Id);
}
}
在行HasKey(c => c.Id);
视觉工作室抱怨
The type 'Expression<>' is defined in an assembly that is not referenced.
You must add a reference to assembly 'System.Core, Version=4.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089'.
我失去的是什么?
的App.config
是
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
<section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<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>
</configuration>
而packages.config
是
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="EntityFramework" version="6.1.3" targetFramework="portable40-net45+sl5" />
</packages>
答
错误是有点误导这里。当你刚刚通过的NuGet安装EF:
Install-Package EntityFramework
如安装的NuGet会报告:
Adding package 'EntityFramework.6.1.3' to folder '\\psf\home\Documents\Visual Studio 2015\Projects\Solution2\packages'
Added package 'EntityFramework.6.1.3' to folder '\\psf\home\Documents\Visual Studio 2015\Projects\Solution2\packages'
Added package 'EntityFramework.6.1.3' to 'packages.config'
Executing script file '\\psf\home\Documents\Visual Studio 2015\Projects\Solution2\packages\EntityFramework.6.1.3\tools\init.ps1'
Executing script file '\\psf\home\Documents\Visual Studio 2015\Projects\Solution2\packages\EntityFramework.6.1.3\tools\install.ps1'
两个脚本上面会但是修改您的PROJ文件,app.config
以及package.config
文件,实际的dll不会被引用。
如果您检查packages\EntityFramework.6.1.3\lib
的内容,您会发现两个带有二进制文件的文件夹(net40
和net45
)。
实际上,便携式配置文件完全不受EF的支持。你可能想考虑使用EF7。
您可能手动引用了.NET40
或.NET45
DLL之一。现在,您在这里遇到API不匹配的问题。
另一种选择是重新考虑应用程序的体系结构。您可以将业务逻辑保留在PCL中,抽象出存储的工作并将抽象注入到业务逻辑类中。
上面提到的抽象的实际实现必须保留在.NET 4.5项目中。
不知道任何有关您的项目的细节,这就是我所能建议的。
什么是错误? – Nikolaus
@尼古拉斯你是什么意思?我已将它包含在问题 – OrElse
您使用哪个版本的VS? – Nikolaus