如何与.Net核心网站和UWP应用程序共享.Net核心项目?

时间:2016-09-08 15:55:44

标签: .net asp.net-core uwp entity-framework-core

我正在创建一个概念解决方案的证明,该解决方案将使用Entity Framework进行共享项目。我希望共享项目能够被UWP项目和.Net Core网站引用。我能够让网站引用该项目并正确使用Entity Framework Core。当我尝试让UWP应用程序使用相同的代码时,它会编译,但在运行时会失败。我也尝试使用NetStandard1.4和NetStandard1.6更改框架,但实体框架核心无法正常工作。

我的共享项目是.Net Core类库(不是VS中的实际共享项目类型)。

这是我运行UWP应用程序时遇到的错误。当我尝试创建一个继承自DbContext的新Context类时会发生这种情况:

  

发生了'System.IO.FileLoadException'类型的异常   mscorlib.ni.dll但未在用户代码中处理

     

其他信息:无法加载文件或程序集   'System.Runtime,Version = 4.1.0.0,Culture = neutral,   PublicKeyToken = b03f5f7f11d50a3a'或其依赖项之一。该   找到程序集的清单定义与程序集不匹配   参考。 (HRESULT异常:0x80131040)

这是我对共享项目的project.json。

{
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.EntityFrameworkCore.Sqlite": "1.0.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "NETStandard.Library": "1.6.0",
    "System.ComponentModel.Annotations": "4.1.0",
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    }
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }

  }
}

这是UWP项目的project.json

{
  "dependencies": {
    "Microsoft.EntityFrameworkCore.Sqlite": "1.0.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.NETCore.UniversalWindowsPlatform": "5.2.2"
  },
  "frameworks": {
    "uap10.0": {
      "imports": [
        "netcoreapp1.0"
      ]
    }
  },
  "runtimes": {
    "win10-arm": {},
    "win10-arm-aot": {},
    "win10-x86": {},
    "win10-x86-aot": {},
    "win10-x64": {},
    "win10-x64-aot": {}
  }
}

这是网站项目的project.json

{
  "dependencies": {
    "Domain": "1.0.0-*",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0",
    "Microsoft.AspNetCore.Mvc": "1.0.0",
    "Microsoft.AspNetCore.Razor.Tools": {
      "version": "1.0.0-preview2-final",
      "type": "build"
    },
    "Microsoft.AspNetCore.Server.IISIntegration": "1.0.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0",
    "Microsoft.AspNetCore.StaticFiles": "1.0.0",
    "Microsoft.EntityFrameworkCore.Sqlite": "1.0.0",
    "Microsoft.EntityFrameworkCore.SqlServer": "1.0.0",
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.0.0",
    "Microsoft.Extensions.Configuration.Json": "1.0.0",
    "Microsoft.Extensions.Logging": "1.0.0",
    "Microsoft.Extensions.Logging.Console": "1.0.0",
    "Microsoft.Extensions.Logging.Debug": "1.0.0",
    "Microsoft.Extensions.Options.ConfigurationExtensions": "1.0.0",
    "Microsoft.NETCore.App": {
      "version": "1.0.0",
      "type": "platform"
    },
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0"
  },

  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.0.0-preview2-final",
    "BundlerMinifier.Core": "2.0.238",
    "Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final",
    "Microsoft.AspNetCore.Server.IISIntegration.Tools": "1.0.0-preview2-final"
  },

  "frameworks": {
    "netcoreapp1.0": {
      "imports": [
        "dotnet5.6",
        "portable-net45+win8"
      ]
    }
  },

  "buildOptions": {
    "emitEntryPoint": true,
    "preserveCompilationContext": true
  },

  "runtimeOptions": {
    "configProperties": {
      "System.GC.Server": true
    }
  },

  "publishOptions": {
    "include": [
      "wwwroot",
      "Views",
      "Areas/**/Views",
      "appsettings.json",
      "web.config"
    ]
  },

  "scripts": {
    "prepublish": [ "bower install", "dotnet bundle" ],
    "postpublish": [ "dotnet publish-iis --publish-folder %publish:OutputPath% --framework %publish:FullTargetFramework%" ]
  }
}

2 个答案:

答案 0 :(得分:0)

我想出来了,请参阅Brice Lambson在GitHub

上的答案

答案 1 :(得分:0)

我不确定UWP项目是否适用于.NET Core项目。也许您可以尝试将EF项目更改为可移植类库项目。