【发布时间】: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. 删除对
ObjectContextAPI 的任何引用,2. 升级到 EF 6.4,仍在 .net 框架中,3. 移植到 .net 核心。 4. 以最少的代码更改量移植到 EF 3 核心。这些步骤中的每一个都可能有容易扣除的回归,第 3 步可能是最难的。
标签: c# wpf entity-framework .net-core migration