Как перенести мой код из. Net Framework в. Net Core 3.1? - PullRequest
0 голосов
/ 20 апреля 2020

Я пытаюсь перенести свой проект из EF в. Net Core с wpf. Теперь я установил EntityFrameworkCore 3.1, но он не поддерживает ObjectSet, MergeOption, RefreshMode и ObjectContext, все функции EF. Как бы выглядел мой код, если бы я реализовал его в. Net ядре?

Это мой CommonDbContext.cs в Entity Framework:

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

1 Ответ

1 голос
/ 20 апреля 2020

Вот частичный порт этого для 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();
        }
    }
}
...