C# 将数据库集转换为数据库集<;T>;其中T是dbset的元素类型的接口

C# 将数据库集转换为数据库集<;T>;其中T是dbset的元素类型的接口,c#,entity-framework,C#,Entity Framework,我可以从一个数据库集到另一个数据库集吗 public IQueryable<T> GetData<T>() where T : class, IData { var factory = EntityFrameworkConfiguration.TypeFactory[typeof(T)]; var context = factory.Item2(); // context is a DbContext. var

我可以从一个数据库集到另一个数据库集吗

  public IQueryable<T> GetData<T>() where T : class, IData
  { 
      var factory = EntityFrameworkConfiguration.TypeFactory[typeof(T)];
      var context = factory.Item2();         // context is a DbContext.
      var set = context.Set(factory.Item1);  //factory.Item1 is a Type object.
      return set.Cast<T>();

  }
public IQueryable GetData(),其中T:class,IData
{ 
var factory=EntityFrameworkConfiguration.TypeFactory[typeof(T)];
var context=factory.Item2();//上下文是DbContext。
var set=context.set(factory.Item1);//factory.Item1是一个类型对象。
返回set.Cast();
}
factory.Item1是实现Iterface T的类的类型

简单案例:

context.Set(typeof(MyClass));
how to return DbSet<IMyClass> ?  
context.Set(typeof(MyClass));
如何返回DbSet?
更新 只需补充一点信息:

System.InvalidCastException:无法从非泛型数据库集为“AzureStoreSubscriptionWrapper”类型的对象创建数据库集。以及守则:

[Title("AzureStoreSubscription"), DataScope("public"), KeyPropertyName("id_AzureStoreSubscriptionId"), ImmutableTypeId("{7a71540b-2eeb-32ae-a850-61c97e28df06}"), RelevantToUserType(UserType.Developer)]
public interface IAzureStoreSubscription : IData, IEntityFrameworkProvided
{
    // Properties
    [StoreFieldType(PhysicalStoreFieldType.DateTime), ImmutableFieldId("{35977476-f662-528b-8236-e993b65796a5}")]
    DateTime dt_ChangedTime { get; set; }
    [ImmutableFieldId("{cdc95aeb-a951-533c-87de-811fd4967ea6}"), StoreFieldType(PhysicalStoreFieldType.DateTime)]
    DateTime dt_CreatedTime { get; set; }
    [StoreFieldType(PhysicalStoreFieldType.String, 50), ImmutableFieldId("{83e0e743-0ec3-5f29-a4bd-14a1b97a13a8}")]
    DateTime? dt_DeletedTime { get; set; }
    [StoreFieldType(PhysicalStoreFieldType.Guid), ImmutableFieldId("{82f8da9f-00fa-5a00-b392-d47cb2d362d5}")]
    Guid id_AzureStoreSubscriptionId { get; set; }
    [ImmutableFieldId("{3bbef642-c88b-5a13-be89-65d537de3eb6}"), StoreFieldType(PhysicalStoreFieldType.Guid)]
    Guid id_TenantId { get; set; }
    [ImmutableFieldId("{273bd594-7aea-5ca0-b8da-4680a264bcfd}"), StoreFieldType(PhysicalStoreFieldType.String, 50)]
    string nvc_Email { get; set; }
    [StoreFieldType(PhysicalStoreFieldType.String, 50), ImmutableFieldId("{64d29805-d76a-55b0-a61a-abb76b5fd096}")]
    string nvc_EntityState { get; set; }
    [StoreFieldType(PhysicalStoreFieldType.String, 50), ImmutableFieldId("{a2172fef-de9a-559c-b33c-6c32d7f99727}")]
    string nvc_OptIn { get; set; }
    [ImmutableFieldId("{e1bb5d48-2d72-5ffc-b86f-f78216ea4ee4}"), StoreFieldType(PhysicalStoreFieldType.String, 50)]
    string nvc_RawData { get; set; }
    [ImmutableFieldId("{3e879595-9bbd-555f-8f63-f438e722915a}"), StoreFieldType(PhysicalStoreFieldType.String, 50)]
    string nvc_ResourceType { get; set; }
    [StoreFieldType(PhysicalStoreFieldType.String, 50), ImmutableFieldId("{67bd2cba-9c36-594c-8586-39c71974e572}")]
    string nvc_SubscriptionId { get; set; }
}




public class C1AzureStoreRPContext : DbContext
{
    // Methods
    public C1AzureStoreRPContext(string connectionstring);

    // Properties
    public DbSet<AzureStoreResourceWrapper> AzureStoreResource { get; set; }
    public DbSet<AzureStoreSubscriptionWrapper> AzureStoreSubscription { get; set; }
}




[Table("AzureStoreSubscriptions", Schema="cwac")]
public class AzureStoreSubscriptionWrapper : AzureStoreSubscription, IAzureStoreSubscription, IData, IEntityFrameworkProvided
{
    // Fields
    private DataSourceId _dataSourceId;

    // Methods
    public AzureStoreSubscriptionWrapper();

    // Properties
    [StoreFieldType(PhysicalStoreFieldType.DateTime), ImmutableFieldId("{35977476-f662-528b-8236-e993b65796a5}")]
    DateTime IAzureStoreSubscription.dt_ChangedTime { get; set; }
    [ImmutableFieldId("{cdc95aeb-a951-533c-87de-811fd4967ea6}"), StoreFieldType(PhysicalStoreFieldType.DateTime)]
    DateTime IAzureStoreSubscription.dt_CreatedTime { get; set; }
    [StoreFieldType(PhysicalStoreFieldType.String, 50), ImmutableFieldId("{83e0e743-0ec3-5f29-a4bd-14a1b97a13a8}")]
    DateTime? IAzureStoreSubscription.dt_DeletedTime { get; set; }
    [StoreFieldType(PhysicalStoreFieldType.Guid), ImmutableFieldId("{82f8da9f-00fa-5a00-b392-d47cb2d362d5}")]
    Guid IAzureStoreSubscription.id_AzureStoreSubscriptionId { get; set; }
    [ImmutableFieldId("{3bbef642-c88b-5a13-be89-65d537de3eb6}"), StoreFieldType(PhysicalStoreFieldType.Guid)]
    Guid IAzureStoreSubscription.id_TenantId { get; set; }
    [ImmutableFieldId("{273bd594-7aea-5ca0-b8da-4680a264bcfd}"), StoreFieldType(PhysicalStoreFieldType.String, 50)]
    string IAzureStoreSubscription.nvc_Email { get; set; }
    [StoreFieldType(PhysicalStoreFieldType.String, 50), ImmutableFieldId("{64d29805-d76a-55b0-a61a-abb76b5fd096}")]
    string IAzureStoreSubscription.nvc_EntityState { get; set; }
    [StoreFieldType(PhysicalStoreFieldType.String, 50), ImmutableFieldId("{a2172fef-de9a-559c-b33c-6c32d7f99727}")]
    string IAzureStoreSubscription.nvc_OptIn { get; set; }
    [ImmutableFieldId("{e1bb5d48-2d72-5ffc-b86f-f78216ea4ee4}"), StoreFieldType(PhysicalStoreFieldType.String, 50)]
    string IAzureStoreSubscription.nvc_RawData { get; set; }
    [ImmutableFieldId("{3e879595-9bbd-555f-8f63-f438e722915a}"), StoreFieldType(PhysicalStoreFieldType.String, 50)]
    string IAzureStoreSubscription.nvc_ResourceType { get; set; }
    [StoreFieldType(PhysicalStoreFieldType.String, 50), ImmutableFieldId("{67bd2cba-9c36-594c-8586-39c71974e572}")]
    string IAzureStoreSubscription.nvc_SubscriptionId { get; set; }
    public DataSourceId DataSourceId { get; }
}
[标题(“AzureStoreSubscription”)、数据范围(“public”)、KeyPropertyName(“id_AzureStoreSubscriptionId”)、ImmutableTypeId(“7a71540b-2eeb-32ae-a850-61c97e28df06})、相关类型(UserType.Developer)]
公共接口IAzureStoreSubscription:IData,提供了IEntityFramework
{
//性质
[StoreFieldType(PhysicalStoreFieldType.DateTime),ImmutableFieldId(“{35977476-f662-528b-8236-e993b65796a5}”)]
DateTime dt_ChangedTime{get;set;}
[ImmutableFieldId(“{cdc95aeb-a951-533c-87de-811fd4967ea6}”),StoreFieldType(PhysicalStoreFieldType.DateTime)]
DateTime dt_CreatedTime{get;set;}
[StoreFieldType(PhysicalStoreFieldType.String,50),ImmutableFieldId(“{83e0e743-0ec3-5f29-a4bd-14a1b97a13a8}”)]
DateTime?dt_DeletedTime{get;set;}
[StoreFieldType(PhysicalStoreFieldType.Guid),ImmutableFieldId(“{82f8da9f-00fa-5a00-b392-d47cb2d362d5}”)]
Guid id_AzureStoreSubscriptionId{get;set;}
[ImmutableFieldId(“{3bbef642-c88b-5a13-be89-65d537de3eb6}”),StoreFieldType(PhysicalStoreFieldType.Guid)]
Guid id_TenantId{get;set;}
[ImmutableFieldId(“{273bd594-7aea-5ca0-b8da-4680a264bcfd}”),StoreFieldType(PhysicalStoreFieldType.String,50)]
字符串nvc_电子邮件{get;set;}
[StoreFieldType(PhysicalStoreFieldType.String,50),ImmutableFieldId(“{64d29805-d76a-55b0-a61a-abb76b5fd096}”)]
字符串nvc_EntityState{get;set;}
[StoreFieldType(PhysicalStoreFieldType.String,50),ImmutableFieldId(“{a2172fef-de9a-559c-b33c-6c32d7f99727}”)]
字符串nvc_OptIn{get;set;}
[ImmutableFieldId(“{e1bb5d48-2d72-5ffc-b86f-f78216ea4ee4}”),StoreFieldType(PhysicalStoreFieldType.String,50)]
字符串nvc_RawData{get;set;}
[ImmutableFieldId(“{3e879595-9bbd-555f-8f63-f438e722915a}”),StoreFieldType(PhysicalStoreFieldType.String,50)]
字符串nvc_ResourceType{get;set;}
[StoreFieldType(PhysicalStoreFieldType.String,50),ImmutableFieldId(“{67bd2cba-9c36-594c-8586-39c71974e572}”)]
字符串nvc_SubscriptionId{get;set;}
}
公共类C1AzureStoreRPContext:DbContext
{
//方法
公共C1AzureStoreRPContext(字符串连接字符串);
//性质
公共数据库集AzureStoreResource{get;set;}
公共DbSet AzureStoreSubscription{get;set;}
}
[表(“AzureStoreSubscriptions”,Schema=“cwac”)]
公共类AzureStoreSubscriptionWrapper:AzureStoreSubscription、IAzureStoreSubscription、IData、IEntityFrameworkProvided
{
//田地
私有数据源ID_DataSourceId;
//方法
public AzureStoreSubscriptionWrapper();
//性质
[StoreFieldType(PhysicalStoreFieldType.DateTime),ImmutableFieldId(“{35977476-f662-528b-8236-e993b65796a5}”)]
DateTime IAzureStoreSubscription.dt_ChangedTime{get;set;}
[ImmutableFieldId(“{cdc95aeb-a951-533c-87de-811fd4967ea6}”),StoreFieldType(PhysicalStoreFieldType.DateTime)]
DateTime IAzureStoreSubscription.dt_CreatedTime{get;set;}
[StoreFieldType(PhysicalStoreFieldType.String,50),ImmutableFieldId(“{83e0e743-0ec3-5f29-a4bd-14a1b97a13a8}”)]
DateTime?IAzureStoreSubscription.dt_DeletedTime{get;set;}
[StoreFieldType(PhysicalStoreFieldType.Guid),ImmutableFieldId(“{82f8da9f-00fa-5a00-b392-d47cb2d362d5}”)]
Guid IAzureStoreSubscription.id_AzureStoreSubscriptionId{get;set;}
[ImmutableFieldId(“{3bbef642-c88b-5a13-be89-65d537de3eb6}”),StoreFieldType(PhysicalStoreFieldType.Guid)]
Guid IAzureStoreSubscription.id_TenantId{get;set;}
[ImmutableFieldId(“{273bd594-7aea-5ca0-b8da-4680a264bcfd}”),StoreFieldType(PhysicalStoreFieldType.String,50)]
字符串IAzureStoreSubscription.nvc_电子邮件{get;set;}
[StoreFieldType(PhysicalStoreFieldType.String,50),ImmutableFieldId(“{64d29805-d76a-55b0-a61a-abb76b5fd096}”)]
字符串IAzureStoreSubscription.nvc_EntityState{get;set;}
[StoreFieldType(PhysicalStoreFieldType.String,50),ImmutableFieldId(“{a2172fef-de9a-559c-b33c-6c32d7f99727}”)]
字符串IAzureStoreSubscription.nvc_OptIn{get;set;}
[ImmutableFieldId(“{e1bb5d48-2d72-5ffc-b86f-f78216ea4ee4}”),StoreFieldType(PhysicalStoreFieldType.String,50)]
字符串IAzureStoreSubscription.nvc_RawData{get;set;}
[ImmutableFieldId(“{3e879595-9bbd-555f-8f63-f438e722915a}”),StoreFieldType(PhysicalStoreFieldType.String,50)]
字符串IAzureStoreSubscription.nvc_ResourceType{get;set;}
[StoreFieldType(PhysicalStoreFieldType.String,50),ImmutableFieldId(“{67bd2cba-9c36-594c-8586-39c71974e572}”)]
字符串IAzureStoreSubscription.nvc_SubscriptionId{get;set;}
公共数据源ID数据源ID{get;}
}

您不能在DbSet上再次运行Cast方法吗

因此,如果以下是您的数据库集:

var myClassSet = context.Set(typeof(MyClass));
你应该可以这样施展:

return myClassSet.Cast<IMyClass>();
返回myClassSet.Cast();
它应该通过直接强制转换进行强制转换,这样就不会出现向继承类强制转换的问题。如果不起作用,请查看以下链接:

我希望这会有所帮助。

我得到一个错误:“无法从AzureStoreResourceWrapper类型的对象的非泛型数据库集创建数据库集。”AzureStoreResourceWrapper实现了IAzureStoreResource。上下文有DbSet