【问题标题】:Can't I use Code First fluent API with .NET Portable?我不能将 Code First fluent API 与 .NET Portable 一起使用吗?
【发布时间】:2017-08-31 08:28:19
【问题描述】:

我已经创建了一个针对 .NETPortable, v4.0 的可移植类库

在下面的课程中

public class AddressConfiguration : EntityTypeConfiguration<Address>
    {
        public AddressConfiguration()
        {
            ToTable("Addresses");
            HasKey(c => c.Id);
        }
    }

在行HasKey(c =&gt; 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>

【问题讨论】:

  • 错误是什么?
  • @Nikolaus 你是什么意思?我已将其包含在问题中
  • 你用的是哪个版本的VS?
  • @Nikolaus 2015.
  • 你能把你的 project.csproj 添加到列出所有引用的地方吗?

标签: c# entity-framework visual-studio-2015 portable-class-library ef-fluent-api


【解决方案1】:

这里的错误有点误导。当您通过 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 的内容,您会发现两个包含二进制文件的文件夹(net40net45)。

事实上,EF 根本不支持便携式配置文件。您可能需要考虑使用 EF7。

您可能已经手动引用了.NET40.NET45 DLL 之一。现在您在这里面临 API 不匹配的问题。

另一种选择是重新考虑应用程序的架构。您可以将业务逻辑保留在 PCL 中,使用存储抽象出工作并将抽象注入业务逻辑类。

您必须将上述抽象的实际实现保留在 .NET 4.5 项目中。

在不了解您项目的任何细节的情况下,我只能提供这些建议。

【讨论】:

    猜你喜欢
    • 2012-04-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-07
    • 1970-01-01
    • 2013-03-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多