Dejosel
Dejosel

Reputation: 115

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

I am trying to migrate my project from EF to .Net Core with wpf. Now, I installed the EntityFrameworkCore 3.1 but it doesn't support ObjectSet, MergeOption, RefreshMode and ObjectContext, all EF functions. How would my code look if I implemented it in .Net core?

This's my CommonDbContext.cs in 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();
        }
    }
}

Upvotes: 0

Views: 709

Answers (1)

David Browne - Microsoft
David Browne - Microsoft

Reputation: 89361

Here's a partial port of that to EF Core 3. But some things just work differently, and you'll need to adapt other parts of the code.

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

Upvotes: 1

Related Questions