首页 > 解决方案 > 数据库优先 - Mono 上的 EF6

问题描述

语境:

我需要在我的本地机器上运行一个“旧版”.Net 应用程序,它是一台 Macbook ......(我们的大部分堆栈都是 dotnetcore),但这个特定的应用程序有点旧。

虽然我确实意识到启动 Windows VM 并在其中构建它会容易得多,但我正在努力构建项目(并使用 MONO 运行)

到目前为止,我已经通过在 中添加一些条件检查解决了一些问题.csproj,这使得应用程序构建和运行......几乎没有,因为当我点击第一个检查数据库连接的 API 时它就死了。

当前设置:

该项目的结构如下:

Project.Data
  - App.Config
  - DataModel.edmx
Project.Api (which references the Project.Data) 
  - Web.Config

两者都包含以下连接字符串(为简洁起见缩进):

<add name="SomeEntities" 

connectionString="
metadata=
  res://*/DataModel.csdl|
  res://*/DataModel.ssdl|
  res://*/DataModel.msl;

provider=System.Data.SqlClient;

provider connection string=
  &quot;
    data source=somedb;
    initial catalog=some_catalog;
    user id=some_id;
    password=some_password;
    App=EntityFramework;
  &quot;
" 

providerName="System.Data.EntityClient" />

问题:

我认为正在发生的事情是常规构建任务不会从 . 生成正确的文件.edmx,但至于如何在我当前的环境中执行此操作......我开始认为这是不可能的。

错误:

Unable to load the specified metadata resource.
  at System.Data.Entity.Core.Metadata.Edm.MetadataArtifactLoaderCompositeResource.LoadResources

我看过各种关于修改连接字符串的帖子,都有不同的结果,但从未导致应用程序正常运行。

更改为,metadata=res://*/;导致:

Argument 'xmlReader' is not valid. A minimum of one .ssdl artifact must be supplied. 
  at System.Data.Entity.Core.EntityUtil.CheckArgumentEmpty[T]

更改为标准 SqlConnection 字符串:

System.Data.Entity.Infrastructure.UnintentionalCodeFirstException: 
The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development.  This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection. To learn more about Code First, Database First, and Model First see the Entity Framework documentation here: http://go.microsoft.com/fwlink/?LinkId=394715

问题:

这是可能的,还是我应该恢复获得一台 Windows 机器?

标签: .netentity-frameworkmsbuildmono

解决方案


因此,在浏览了网络一段时间后,并在一些论坛和群组中询问后,我得出的结论是,这可能是徒劳的,尤其是考虑到如今 dotnetcore 之类的东西已经司空见惯。

VS Code for Mac 团队的一位开发人员回答了有关该问题的更多背景信息...

在 Windows 上,有一些 Microsoft.Data.Entity MSBuild 任务。这些用于在构建时从 .edmx 文件生成各种 .csdl、.ssdl 和 .msl 文件。如果您在 Windows 上的 Visual Studio 中查看构建输出,您应该会看到类似于以下内容的内容:

完全构建目标“EntityDeployEmbeddedResources”。输出文件“obj\Debug\edmsREsourcesToEmbed\Model.ssdl”不存在。使用程序集“C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.Data.Entity.Build.Tasks.dll”中的“EntityDeploy”任务。Mac 不存在 Microsoft.Data.Entity.Build.Tasks.dll 和关联的 Microsoft.Data.Entity.targets 文件。

在 Mac 上,唯一的解决方法是使用跨平台的 Entity Framework Core,而不是使用 edmx 文件,因为这些文件不受支持。

因此选项是:

  • 端口到 dotnetcore,或
  • 获得一台windows机器

...对于这个旧版应用程序。而且由于我们正在以任何一种方式迁移到 dotnetcore... 我相信现实的解决方案是优先将此特定项目移植到 dotnetcore。


推荐阅读