多框架实体库缺少条目

时间:2016-12-23 20:25:32

标签: c# .net-core entity-framework-core

我的实体结构有一个.NET Standard 1.6类库。我正在寻求与.NET Standard和& amp; .NET 4.6框架。我在xproj中指定了两个框架,并使用" buildoptions"排除了一些文件。 - > "编译" - > "排除"

MyContext.cs

#if NETSTANDARD1_6
using Microsoft.EntityFrameworkCore;

namespace MyEntity
{
    public class MyContext : DbContext
    {
        public DbSet<LogItem> Logs { get; set; }

        public static string ConnectionString =
            @"Server=localhost\SQLEXPRESS;Database=Logs;integrated security=true";

        #region Constructors

        public MyContext() { }

        public MyContext(DbContextOptions<MyContext> options) : base(options) { }

        #endregion

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(ConnectionString);
        }
    }
}
#endif

#if NET46
using Microsoft.Data.Entity;

namespace MyEntity
{
    public class MyContext : DbContext
    {
        public DbSet<LogItem> Logs { get; set; }

        public static string ConnectionString =
            @"Server=localhost\SQLEXPRESS;Database=Logs;integrated security=true";

        #region Constructors

        public MyContext() : base(ConnectionString) { }
    }
}
#endif

这编译但只生成NETSTANDARD1.6代码。我查看.NET 4.6库并没有包含MyContext。

project.json

{
  "dependencies": {
    "Moq": "4.6.38-alpha",
    "System.Xml.XmlSerializer": "4.3.0",
    "System.Data.Common": "4.3.0",
    "System.Diagnostics.StackTrace": "4.3.0",
    "System.Linq": "4.3.0",
    "Microsoft.AspNetCore.Server.Kestrel": "1.1.0",
    "Microsoft.EntityFrameworkCore": "1.1.0",
    "System.Threading": "4.3.0",
    "System.Reflection.TypeExtensions": "4.3.0",
    "System.ComponentModel": "4.3.0",
    "NLog": "5.0.0-beta03",
    "System.Data.SqlClient": "4.3.0"
  },

  "tools": {
    "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final"
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50",
      "dependencies": {
        "Microsoft.AspNetCore.Routing": "1.1.0",
        "Microsoft.Extensions.Configuration.Json": "1.1.0",
        "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0",
        "Microsoft.Extensions.FileSystemGlobbing": "1.1.0",
        "Microsoft.Extensions.Logging": "1.1.0",
        "Microsoft.Extensions.WebEncoders": "1.1.0",
        "Microsoft.Extensions.Logging.Console": "1.1.0",
        "Microsoft.Extensions.Configuration.FileExtensions": "1.1.0",
        "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0",
        "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0",
        "Microsoft.AspNetCore.Mvc.Core": "1.1.0",
        "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final",
        "NETStandard.Library": "1.6.1",
        "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0",
        "Microsoft.EntityFrameworkCore.Design": "1.1.0",
        "Microsoft.EntityFrameworkCore.InMemory": "1.1.0",
        "Microsoft.Extensions.FileProviders.Physical": "1.1.0",
        "Microsoft.Extensions.Configuration": "1.1.0",
        "Microsoft.EntityFrameworkCore.Tools.Core": "1.0.0-rc2-final",
        "Microsoft.AspNetCore.Mvc.ViewFeatures": "1.1.0",
        "Microsoft.EntityFrameworkCore.Relational.Design": "1.1.0",
        "Microsoft.Extensions.Logging.Debug": "1.1.0",
        "Microsoft.AspNetCore.Mvc": "1.1.0",
        "Microsoft.EntityFrameworkCore.Relational": "1.1.0",
        "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0"
      }
    },
    "dnx46": {
      "dependencies": {
        "EntityFramework": "6.1.3",
        "EntityFramework.SqlServerCompact": "6.1.3"
      },
      "buildOptions": {
        "compile": {
          "exclude": [ "Migrations" ]
        }
      }
    }
  }
}

在这里,您可以看到我已经设置了两个要构建的框架,我忽略了DNX46的Migrations文件夹。

如何将MyContext包含在内? LogItem包含在程序集中,因为它不是框架敏感的,并且不包含#if语句。

我已将代码转储到GitHub,以便进行实验。我使用JetBrains DotPeek分析了EFCrossFrameworkClassLibrary.dll(.NET 4.6和.NET标准版)。

4 个答案:

答案 0 :(得分:1)

取了你放在GitHub上的项目,稍微更改了project.json文件,似乎为.Net 4.6生成了MyContext(你可能需要更新{{1}的EF依赖项}})。

net46

此外,内部{ "dependencies": { "Moq": "4.6.38-alpha", "System.Xml.XmlSerializer": "4.3.0", "System.Data.Common": "4.3.0", "System.Diagnostics.StackTrace": "4.3.0", "System.Linq": "4.3.0", "Microsoft.AspNetCore.Server.Kestrel": "1.1.0", "System.Threading": "4.3.0", "System.Reflection.TypeExtensions": "4.3.0", "System.ComponentModel": "4.3.0", "System.Data.SqlClient": "4.3.0", "Microsoft.EntityFrameworkCore": "1.1.0" }, "tools": { "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final" }, "frameworks": { "net46": { "dependencies": { "EntityFramework": "6.1.3", "EntityFramework.SqlServerCompact": "6.1.3" }, "buildOptions": { "compile": { "exclude": [ "Migrations" ] } } }, "netstandard1.6": { "imports": "dnxcore50", "dependencies": { "Microsoft.AspNetCore.Routing": "1.1.0", "Microsoft.Extensions.Configuration.Json": "1.1.0", "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0", "Microsoft.Extensions.FileSystemGlobbing": "1.1.0", "Microsoft.Extensions.Logging": "1.1.0", "Microsoft.Extensions.WebEncoders": "1.1.0", "Microsoft.Extensions.Logging.Console": "1.1.0", "Microsoft.Extensions.Configuration.FileExtensions": "1.1.0", "Microsoft.Extensions.Configuration.EnvironmentVariables": "1.1.0", "Microsoft.AspNetCore.Server.IISIntegration": "1.1.0", "Microsoft.AspNetCore.Mvc.Core": "1.1.0", "Microsoft.EntityFrameworkCore.Tools": "1.1.0-preview4-final", "NETStandard.Library": "1.6.1", "Microsoft.Extensions.Options.ConfigurationExtensions": "1.1.0", "Microsoft.EntityFrameworkCore.Design": "1.1.0", "Microsoft.EntityFrameworkCore.InMemory": "1.1.0", "Microsoft.Extensions.FileProviders.Physical": "1.1.0", "Microsoft.Extensions.Configuration": "1.1.0", "Microsoft.EntityFrameworkCore.Tools.Core": "1.0.0-rc2-final", "Microsoft.AspNetCore.Mvc.ViewFeatures": "1.1.0", "Microsoft.EntityFrameworkCore.Relational.Design": "1.1.0", "Microsoft.Extensions.Logging.Debug": "1.1.0", "Microsoft.AspNetCore.Mvc": "1.1.0", "Microsoft.EntityFrameworkCore.Relational": "1.1.0", "Microsoft.EntityFrameworkCore.SqlServer.Design": "1.1.0" } } } } 更改:

LogContext.cs

using Microsoft.Data.Entity;

答案 1 :(得分:1)

以下是为了让您的代码编译并生成类而必须进行的更改:

  1. 将条件编辑更改为 #if DNX46 。 您现在拥有的方式( NET46 )隐藏了另一个问题,因为该类实际上并未编译。

  2. 将名称空间更改为System.Data.Entity

  3. 删除了构造函数DbContextOptions,因为此类在EF 6上不存在。
  4. 删除带有DbContextOptionsBuilder的覆盖,因为此类在EF 6上不存在。
  5. 据我所知,DNX46中的最后一个类可能不是你想要的,因为这个类最终与其他框架不同。

答案 2 :(得分:1)

我创建了一个分支,请查看https://github.com/wonea/EFCrossFrameworkClassLibrary/pull/1

project.json现在看起来更清晰了!

{
  "dependencies": {
    "Microsoft.EntityFrameworkCore.SqlServer": "1.1.0"
  },
  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50",
      "dependencies": {
        "NETStandard.Library": "1.6.1"
      }
    },
    "net46": {
      "buildOptions": {
        "compile": {
          "exclude": [ "Migrations" ]
        }
      }
    }
  }
}

我删除了project.json中的大部分未使用的数据,并添加了net46作为框架。好的一点是,EF.Core支持“降级”,因此您不需要做任何其他事情(至少对于您的简单场景; - ))。

这也意味着你不需要上下文文件中的编译标志,所以我也删除了它们。

using Microsoft.EntityFrameworkCore;

namespace MyEntity
{
    public class LogContext : DbContext
    {
        public DbSet<LogItem> Logs { get; set; }

        public static string ConnectionString =
            @"Server=localhost\SQLEXPRESS;Database=MyLog;integrated security=true";

        #region Constructors

        public LogContext() { }

        public LogContext(DbContextOptions<LogContext> options) : base(options) { }

        #endregion

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(ConnectionString);
        }
    }
}

编译它,你的bin中会出现2个文件夹:一个用于asp.net 4.6,另一个用于.net Core。

答案 3 :(得分:0)

尝试net46的框架?在您的构建中,请确保也将其定位。在命令行上,类似于dotnet build -f net46