gpt4 book ai didi

c# - 多框架实体库缺少条目

转载 作者:太空宇宙 更新时间:2023-11-03 23:05:38 25 4
gpt4 key购买 nike

我的实体结构有一个 .NET Standard 1.6 类库。我希望针对 .NET Standard 和 .NET 4.6 框架进行构建。我在 xproj 中指定了两个框架,并使用“buildoptions”->“compile”->“exclude”排除了一些文件

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。

项目.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 Standard) .

最佳答案

我创建了一个 fork ,请看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);
}
}
}

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

关于c# - 多框架实体库缺少条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41307461/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com