可以在.NET Framework应用程序中使用EF Core程序集的DbContext吗?

huangapple go评论54阅读模式
英文:

Can I use an EF Core assembly DbContext in a .NET Framework app?

问题

我有一个大型的遗留 ASP.NET MVC 应用程序,它在 .NET 4.8 Framework 上运行。它使用 EF 6,但我正在尝试通过创建另一个程序集,针对 .NET Core,并从我的现有应用程序中调用它来将其迁移到 EF Core。

(我这样做是因为整个应用程序非常庞大,将整个应用程序迁移到 Core 将是一个巨大的项目,所以我打算分块处理。)

Visual Studio 允许我添加对新的 Core 程序集的引用,但当我尝试使用其中的 DbContext 时,我会收到这个错误:

错误 BC30652:需要对包含类型 'DbContext' 的程序集 'Microsoft.EntityFrameworkCore, Version=7.0.8.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' 进行引用。请向您的项目添加一个引用。

但当我尝试添加这个引用时,我会收到以下错误:

无法安装包 'Microsoft.EntityFrameworkCore 7.0.8'。您正在尝试将此包安装到一个目标为 '.NETFramework,Version=v4.8' 的项目中,但该包不包含与该框架兼容的任何程序集引用或内容文件。有关更多信息,请联系包的作者。

有没有办法满足我的需求?

英文:

I've got a large legacy ASP.NET MVC app that's running the .NET 4.8 Framework. It uses EF 6, but I'm trying to port this to EF Core by making another assembly, targeting .NET Core, and calling it from my existing app.

(I'm doing this because the entire app is enormous, and porting the whole thing to Core will be a huge project, so I'm picking it off in chunks.)

Visual Studio lets me add a reference to my new Core assembly, but when I try to use the DbContext from it, I get this error:

> Error BC30652 Reference required to assembly 'Microsoft.EntityFrameworkCore, Version=7.0.8.0, Culture=neutral, PublicKeyToken=adb9793829ddae60' containing the type 'DbContext'. Add one to your project.

But when I try to add the reference, I get the error:

> Could not install package 'Microsoft.EntityFrameworkCore 7.0.8'. You are trying to install this package into a project that targets '.NETFramework,Version=v4.8', but the package does not contain any assembly references or content files that are compatible with that framework. For more information, contact the package author.

Is there any way to do what I need here?

答案1

得分: 3

.NET Framework 4.8仅支持.netstandard 2.0及以下。它无法与仅针对.NET 7的目标一起使用。针对.netstandard 2.0的最新版本的EF Core是3.1.32。
在这个示例中,我连接了.NET Framework 4.8和EF Core 3.1.32。

using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;

namespace ConsoleApplication1
{
    public class Test
    {
        [Key]
        public Guid Id { get; set; }
    }
    
    public class ApplicationContext : DbContext
    {
        public DbSet<Test> Tests { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseInMemoryDatabase("Test");

            base.OnConfiguring(optionsBuilder);
        }
    }
    
    internal class Program
    {
        public static void Main(string[] args)
        {
            var dbContext = new ApplicationContext();

            dbContext.Tests.Add(new Test()
            {
                Id = Guid.NewGuid()
            });
            dbContext.SaveChanges();

            Console.WriteLine("代码正常运行");
        }
    }
}
英文:

.NET Framework 4.8 supports only .netstandard 2.0 and lower. It is not going to work with anything that targets only .NET 7. The latest version of EF Core that targets .netstandard 2.0 is 3.1.32.
In this example I connected .NET Framework 4.8 with EF Core 3.1.32

using System;
using System.ComponentModel.DataAnnotations;
using Microsoft.EntityFrameworkCore;

namespace ConsoleApplication1
{
    public class Test
    {
        [Key]
        public Guid Id { get; set; }
    }
    
    public class ApplicationContext : DbContext
    {
        public DbSet&lt;Test&gt; Tests { get; set; }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseInMemoryDatabase(&quot;Test&quot;);
            
            base.OnConfiguring(optionsBuilder);
        }
    }
    
    internal class Program
    {
        public static void Main(string[] args)
        {
            var dbContext = new ApplicationContext();

            dbContext.Tests.Add(new Test()
            {
                Id = Guid.NewGuid()
            });
            dbContext.SaveChanges();

            Console.WriteLine(&quot;Code works&quot;);
        }
    }
}

huangapple
  • 本文由 发表于 2023年6月30日 04:14:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76584348.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定