【问题标题】:How do I migrate my code from .Net Framework to .Net Core 3.1?如何将我的代码从 .Net Framework 迁移到 .Net Core 3.1?
【发布时间】:2020-04-20 13:42:07
【问题描述】:

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

这是我在实体框架中的 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();
        }
    }
}

【问题讨论】:

  • 您应该开始将代码移植到 EF Core,如 docs 中所述。
  • 否,升级到支持 .Net 核心的 EF 6.4。除此之外,最好将ObjectContext 完全排除在外。
  • @GertArnold:EF6 不再积极开发,因此如果您认真考虑将应用程序升级到 .NET Core 并且当前使用 EF,那么您绝对应该考虑升级到 EF Core。
  • 确实EF不再积极开发了;因此,我决定将我的应用程序完全移动到网络核心,但我有点迷茫,因为我是新手
  • 当然,EF6 不是 EF 团队的重点版本,但它允许将应用程序移植到 .net 核心,而不会出现大爆炸场景。我个人在 OP 代码中的路线图是:1. 删除对 ObjectContext API 的任何引用,2. 升级到 EF 6.4,仍在 .net 框架中,3. 移植到 .net 核心。 4. 以最少的代码更改量移植到 EF 3 核心。这些步骤中的每一个都可能有容易扣除的回归,第 3 步可能是最难的。

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


【解决方案1】:

这是 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();
        }
    }
}

【讨论】:

  • Browne 感谢您的帮助...为什么要排除 ObjectContext 函数?
  • 因为 EF Core 中没有 ObjectContext。
猜你喜欢
  • 1970-01-01
  • 2021-03-20
  • 1970-01-01
  • 2021-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-08-22
  • 2021-05-03
相关资源
最近更新 更多