簡體   English   中英

如何將我的代碼從 .Net Framework 遷移到 .Net Core 3.1?

[英]How do I migrate my code from .Net Framework to .Net Core 3.1?

我正在嘗試使用 wpf 將我的項目從 EF 遷移到.Net Core。 現在,我安裝了 EntityFrameworkCore 3.1,但它不支持 ObjectSet、MergeOption、RefreshMode 和 ObjectContext,所有 EF 功能。 如果我在 .Net 核心中實現它,我的代碼會是什么樣子?

這是我在實體框架中的 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 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();
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM