C# 如何将代码从.Net Framework迁移到.Net Core 3.1?

C# 如何将代码从.Net Framework迁移到.Net Core 3.1?,c#,wpf,entity-framework,.net-core,migration,C#,Wpf,Entity Framework,.net Core,Migration,我正在尝试使用wpf将我的项目从EF迁移到.NETCore。现在,我安装了EntityFrameworkCore 3.1,但它不支持ObjectSet、MergeOption、RefreshMode和ObjectContext以及所有EF函数。 如果我在.NETCore中实现它,我的代码会是什么样子 这是实体框架中的CommonDbContext.cs: using System.Collections; using System.Data.Entity; using System.Data.E

我正在尝试使用wpf将我的项目从EF迁移到.NETCore。现在,我安装了EntityFrameworkCore 3.1,但它不支持ObjectSet、MergeOption、RefreshMode和ObjectContext以及所有EF函数。 如果我在.NETCore中实现它,我的代码会是什么样子

这是实体框架中的CommonDbContext.cs:

using System.Collections;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Data.Objects;
using System.Linq;

namespace Infrastructure.Data.SQL
{
    public class CommonDbContext : DbContext
    {
        public CommonDbContext(string name)
            : base(name)
        {
        }

        public IQueryable<T> ReadOnly<T>() where T : class
        {
            ObjectSet<T> result = ObjContext().CreateObjectSet<T>();
            result.MergeOption = MergeOption.NoTracking;
            return result;
        }

        public IQueryable<T> Trackable<T>() where T : class
        {
            return ObjContext().CreateObjectSet<T>();
        }

        public void Refresh(IEnumerable collection)
        {
            ObjContext().Refresh(RefreshMode.StoreWins, collection);
        }

        public void Refresh(object item)
        {
            ObjContext().Refresh(RefreshMode.StoreWins, item);
        }

        public void Detach(object item)
        {
            ObjContext().Detach(item);
        }

        public void LoadProperty(object item, string propertyName)
        {
            ObjContext().LoadProperty(item, propertyName);
        }

        public void Close()
        {
            ObjContext().Connection.Close();
        }

        public ObjectContext ObjContext()
        {
            return ((IObjectContextAdapter)this).ObjectContext;
        }

        public void AcceptAllChanges()
        {
            ObjContext().AcceptAllChanges();
        }
    }
}
使用系统集合;
使用System.Data.Entity;
使用System.Data.Entity.Infrastructure;
使用System.Data.Object;
使用System.Linq;
命名空间Infrastructure.Data.SQL
{
公共类CommonDbContext:DbContext
{
公共CommonDbContext(字符串名称)
:base(名称)
{
}
public IQueryable ReadOnly(),其中T:class
{
ObjectSet结果=ObjectContext().CreateObjectSet();
result.MergeOption=MergeOption.NoTracking;
返回结果;
}
public IQueryable Trackable(),其中T:class
{
返回ObjContext().CreateObjectSet();
}
公共无效刷新(IEnumerable集合)
{
ObjContext().Refresh(RefreshMode.StoreWins,collection);
}
公共无效刷新(对象项)
{
ObjContext().Refresh(RefreshMode.StoreWins,item);
}
公共空拆离(对象项)
{
ObjContext().Detach(项);
}
公共void LoadProperty(对象项,字符串propertyName)
{
ObjContext().LoadProperty(项,propertyName);
}
公众假期结束()
{
ObjContext().Connection.Close();
}
公共ObjectContext ObjContext()
{
返回((IObjectContextAdapter)this).ObjectContext;
}
公共无效接受更改()
{
ObjContext().AcceptAllChanges();
}
}
}

这是EF Core 3的部分端口。但是有些事情只是工作方式不同,您需要调整代码的其他部分

using Microsoft.EntityFrameworkCore;
using System.Collections;
using System.Linq;


namespace Infrastructure.Data.SQL
{
    public class CommonDbContext : DbContext
    {

        public IQueryable<T> ReadOnly<T>() where T : class
        {
            return Set<T>().AsNoTracking();
        }

        public IQueryable<T> Trackable<T>() where T : class
        {
            return Set<T>();
        }

        public void Refresh(IEnumerable collection)
        {
            foreach(var e in collection)
            {
                Refresh(e);
            }
        }

        public void Refresh(object item)
        {
            Entry(item).Reload();
        }

        public void Detach(object item)
        {
            Entry(item).State = EntityState.Detached;
        }

        public void LoadProperty(object item, string propertyName)
        {
            Entry(item).Reference(propertyName).Load();
        }

        public void Close()
        {
            Dispose();
        }

        //public ObjectContext ObjContext()
        //{
        //    return ((IObjectContextAdapter)this).ObjectContext;
        //}

        public void AcceptAllChanges()
        {
            ChangeTracker.AcceptAllChanges();
        }
    }
}
使用Microsoft.EntityFrameworkCore;
使用系统集合;
使用System.Linq;
命名空间Infrastructure.Data.SQL
{
公共类CommonDbContext:DbContext
{
public IQueryable ReadOnly(),其中T:class
{
返回集().AsNoTracking();
}
public IQueryable Trackable(),其中T:class
{
返回集();
}
公共无效刷新(IEnumerable集合)
{
foreach(集合中的变量e)
{
刷新(e);
}
}
公共无效刷新(对象项)
{
条目(条目).Reload();
}
公共空拆离(对象项)
{
条目(项).State=EntityState.Distached;
}
公共void LoadProperty(对象项,字符串propertyName)
{
条目(项).Reference(propertyName).Load();
}
公众假期结束()
{
处置();
}
//公共ObjectContext ObjContext()
//{
//返回((IObjectContextAdapter)this).ObjectContext;
//}
公共无效接受更改()
{
ChangeTracker.AcceptAllChanges();
}
}
}

您应该开始将代码移植到EF Core,如.No中所述,升级到EF 6.4,它支持.Net Core。除此之外,将
ObjectContext
完全排除在外是一个好主意。@GertArnold:EF6不再是积极开发的,因此如果您真的想将应用升级到.NET Core并且目前正在使用EF,你一定要考虑升级到EF核心,EF不再积极发展了。因此,我决定将我的应用程序完全迁移到net core,但我有点迷茫,因为我是这方面的新手。当然,EF6不是EF团队的焦点版本,但它允许将应用程序移植到.net core,而不会出现大爆炸的情况。在OP的代码中,我的个人路线图是:1。删除对
ObjectContext
API的任何引用,2。升级到EF 6.4,仍在.net framework中,3。端口到.net核心。以尽可能少的代码更改量,4。端口到EF 3核心。这些步骤中的每一步都可能有容易扣除的回归,步骤3可能是所有步骤中最难的。布朗谢谢你的帮助。。。为什么要排除ObjectContext函数?因为EF Core中没有ObjectContext。