从.NET Framework 4.52迁移到.NET Core后的通用存储库问题
本文关键字:NET 问题 存储 Core Framework 迁移 | 更新日期: 2023-09-27 18:14:29
我正在将遗留代码从.NET Framework 4.52迁移到netstandard1.4(FWIW,我也在从EF 6迁移到EF Core(。
我的问题源于IDbSet<T>
目前没有在EF Core中实现。
在EF 6中,IDbSet<Plugin>.Add(entity)
返回Plugin类型的代理。
在EF Core中,context.Set<Plugin>.Add(entity)
返回{Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry`1[Dna.NetCore.Core.BLL.Entities.Plugins.Plugin]}。
在EF Core中从Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry`1[Dna.NetCore.Core.BLL.Entities.Plugins.Plugin]转换为Dna.NetCore.Core.BLL.Enties.Plugins.Plugin时,我得到以下异常。
Q-我该如何铸造?
EF 6 RepositoryBase.cs
public abstract class RepositoryBase<T, U>
where T : class
where U : DbContext, new()
{
private U _dataContext;
private readonly IDbSet<T> _dbset;
public virtual T Add(T entity)
{
object dao = _dbset.Add(entity);
return dao as T;
}
}
EF核心存储库Base.cs
public abstract class RepositoryBase<T>
where T : class
{
//private readonly IDbSet<T> _dbset; // IDbSet is not implemented in .NET Core 1.0.1
private readonly Microsoft.EntityFrameworkCore.DbSet<T> _dbset;
private readonly CoreEFContext _context;
protected RepositoryBase(CoreEFContext context)
{
_context = context;
_dbset = _context.Set<T>();
}
public virtual T Add(T entity, out CustomMessage customMessage)
{
object dao = _dbset.Add(entity);
return dao as T;
}
}
GitHub中提供了该存储库。
在RepositoryBase.cs的第64行:
本地设置窗口:
这个_dbset。结果视图="枚举没有结果">
这个_上下文ChangeTracker.非公共成员。Microsoft.EntityFrameworkCore.Infrastructure.IInfrastructure.Istance.Entries[0]=确实包含实体
即时窗口:
(Dna.NetCore.Core.BLL.Entities.Plugins.Plugin(dao
"(Dna.NetCore.Core.BLL.Entities.Plugins.Plugin(dao"引发了类型为"System.InvalidCastException"的异常
数据:{System.Collections.ListDictionaryInternal}
HResult:-2147467262
帮助链接:null
InnerException:null
消息:"指定的强制转换无效。">
来源:空
StackTrace:空
dao as T
"dao as T"引发了类型为"System.NullReferenceException"的异常
数据:{System.Collections.ListDictionaryInternal}
HResult:-2147467261
帮助链接:null
InnerException:null
消息:"对象引用未设置为对象的实例。">
来源:空
StackTrace:空
dao.GetType((
{Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry
1[Dna.NetCore.Core.BLL.Entities.Plugins.Plugin]}
1[[Dna.NetCore.Core.LL.Enties.Plugins.Plugin,Dna-NetCore.Core.BLL,Version=1.0.0.0,Culture=neutral,PublicKeyToken=null]],Microsoft.EntityFrameworks Core,Version=1.1.0.1.0,Culture=neutral、PublicKeyToken=adb9793829ddae60">
Assembly: {Microsoft.EntityFrameworkCore, Version=1.0.1.0, Culture=neutral, PublicKeyToken=adb9793829ddae60}
AssemblyQualifiedName: "Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry
属性:Public | BeforeFieldInit
BaseType:{Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry}
ContainsGenericParameters:false
自定义属性:计数=0
声明的构造函数:{System.Reflection.ConstructorInfo1}
声明的事件:{System.Reflection.EventInfo[0]}
声明字段:{System.Reflection.FieldInfo[0]}
声明的成员:{System.Reflection.MemberInfo[5]}
声明的方法:{System.Reflection.MethodInfo[3]}
声明的嵌套类型:{System.Reflection.TypeInfo.d_23}
声明的属性:{System.Reflection.PropertyInfo1}
DeclaringMethod:"((System.RuntimeType((dao.GetType((((.DDeclaringMethod"引发了类型为"System.InvalidOperationException"的异常
DeclaringType:null
全名:"Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry1[[Dna.NetCore.Core.BLL.Entities.Plugins.Plugin, Dna.NetCore.Core.BLL, Version=1.0.1.0, Culture=neutral, PublicKeyToken=null]]"
1">
GUID: {bbf992a5-ba4d-3ab3-8a66-fa5a37a909ae}
GenericParameterAttributes: '((System.RuntimeType)(dao.GetType())).GenericParameterAttributes' threw an exception of type 'System.InvalidOperationException'
GenericParameterPosition: '((System.RuntimeType)(dao.GetType())).GenericParameterPosition' threw an exception of type 'System.InvalidOperationException'
GenericTypeArguments: {System.Type[1]}
GenericTypeParameters: {System.Type[0]}
HasElementType: false
ImplementedInterfaces: {System.Type[1]}
IsAbstract: false
IsAnsiClass: true
IsArray: false
IsAutoClass: false
IsAutoLayout: true
IsByRef: false
IsCOMObject: false
IsClass: true
IsConstructedGenericType: true
IsEnum: false
IsExplicitLayout: false
IsGenericParameter: false
IsGenericType: true
IsGenericTypeDefinition: false
IsImport: false
IsInterface: false
IsLayoutSequential: false
IsMarshalByRef: false
IsNested: false
IsNestedAssembly: false
IsNestedFamANDAssem: false
IsNestedFamORAssem: false
IsNestedFamily: false
IsNestedPrivate: false
IsNestedPublic: false
IsNotPublic: false
IsPointer: false
IsPrimitive: false
IsPublic: true
IsSealed: false
IsSecurityCritical: true
IsSecuritySafeCritical: false
IsSecurityTransparent: false
IsSerializable: false
IsSpecialName: false
IsUnicodeClass: false
IsValueType: false
IsVisible: true
MemberType: TypeInfo
MetadataToken: 33554824
Module (System.Reflection.MemberInfo): {Microsoft.EntityFrameworkCore.dll}
Module: {Microsoft.EntityFrameworkCore.dll}
Name: "EntityEntry
命名空间:"Microsoft.EntityFrameworkCore.ChangeTracking">
反射类型:null
StructLayoutAttribute:{System.Runtime.InteropServices.StructLayoutAttribute}
TypeHandle:{System.RuntimeTypeHandle}
TypeInitializer:null
UnderlyingSystemType:{Microsoft.EntityFrameworkCore.ChangeTracking.EntityEntry`1[Dna.NetCore.Core.BLL.Entities.Plugins.Plugin]}
Plugin_UpdateHandler.cs第56行返回:
Dna.NetCore.Core.DAL.EFCore.Repository.RepositoryBase.Add返回nullDna-NetCore.Core.BLL.Enties.Plugins.Plugin
您应该能够通过DbSet上的Entity
属性访问实体。您可以直接返回参数或访问EntityEntry<T>
对象,让ER Core完全控制要做的事情:
public virtual T Add(T entity, out CustomMessage customMessage)
{
var entry = _dbset.Add<T>(entity);
...
return entry.Entity;
}
请注意,目前EF Core没有EF 6的代理功能。