【发布时间】:2020-06-08 08:18:15
【问题描述】:
我有一个分区表列表。这些表有很多,但结构相同。意识到 EF 核心需要不同的类型来处理这些,但最终我想对数据使用通用类型转换,这样可以更轻松地处理数据。与 Oracle 合作,但认为问题一般适用。
这是错误。基本上我想将 T 的 DbSet 转换为 S 的 DbSet,其中 S 是 T 的父类。
System.InvalidCastException: '无法转换类型为'Microsoft.EntityFrameworkCore.Internal.InternalDbSet1[EfCoreBidTest.BidPhaseLh2]' to type 'Microsoft.EntityFrameworkCore.DbSet1[EfCoreBidTest.BidPhase]'的对象。'
请记住,我将同时处理同一个基的多个继承类型。
这是一个基类示例:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace EfCoreBidTest
{
public class BidPhase
{
[Column("YR")]
public int Yr { get; set; }
[Column("QTR")]
public string Qtr { get; set; }
[Column("AMB_CODE")]
public string AmbCode { get; set; }
[Column("SECT")]
public string Sect { get; set; }
[Column("PHASE_ITEM")]
public int PhaseItem { get; set; }
[Column("PHASE")]
public string Phase { get; set; }
[Column("PHASE_DATE")]
public DateTime? PhaseDate { get; set; }
}
}
还有一个继承的类(会有很多)
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Text;
namespace EfCoreBidTest
{
[Table("BID_LH2_MASTSTAT_PHASE")]
public class BidPhaseLh2 : BidPhase
{
}
}
尝试在 DbContext 上创建通用方法以获取阶段对象类型,这时我收到错误
public DbSet<BidPhase> GetPhaseSet(string sys, string stage)
{
var t = typeof(BidDatabase);
var setMethod = t.GetMethod("Set");
var setMethodSpecific = setMethod.MakeGenericMethod(new Type[] { typeof(BidPhaseLh2) }); // TO DO - This will eventually get replaced with something to get the type based on the assembly name
var dbSet = setMethodSpecific.Invoke(this, null);
return (DbSet <BidPhase> ) dbSet;
//sys = Char.ToUpper(sys[0]) + sys.Substring(1).ToLower();
//return (IQueryable<BidPhase>) this.GetType().GetProperty("BidPhase" + sys + stage).GetValue(this);
}
【问题讨论】:
标签: c# oracle entity-framework-core