是否可以将基于 EF 6(带有 .NetFramework 4.0)和 EF Core 5.0(.Net Standard 2.1)的两个应用程序的不同版本的实体框架(核心)实体与相同的数据库连接到一个实体接口中,而不是使用实际两个实体框架提供的数据库类,使用包含实际数据库实体的接口并针对 EF 6 和 EF Core 5 支持的框架平台的项目?
<!--EntityTypes.csproj-->
...
<TargetFrameworks>netstandard2.1; net40; net45</TargetFrameworks>
....
// IEntity.cs
namespace EntityTypes
{
public interface IEntity
{
long EntityID { get; set; }
string EntityName { get; set; }
}
}
在 .Net Core 和 Framework 4.0 项目中,我需要以某种方式使用类似的语法
var _entitiesContext = new EntitiesContext();
public static List<IEntity> BuildSet<T>() where T : class
{
return _entitiesContext.Entities.ToList().Cast<IEntity>().ToList();
}
正如我已经在这个问题中发现的那样:How to Cast DBSet to Interface in Anonymous Method - EF But without getting into the Runtime Error
System.InvalidCastException: 'Unable to cast object of type
'System.Data.Entity.DynamicProxies.Entity_33C84C30E063218CBB4881E395838375014F3EFBA09108F25ACF2FB2FCA1E92D'
to type 'EntityTypes.IEntity'.'