Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/280.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/entity-framework/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用EF 5中具有公共基类的多个数据库集?_C#_Entity Framework - Fatal编程技术网

C# 如何使用EF 5中具有公共基类的多个数据库集?

C# 如何使用EF 5中具有公共基类的多个数据库集?,c#,entity-framework,C#,Entity Framework,我有两个POCO课程: class Email: Base { public int SomeProperty { get; set; } } class Photo: Base { public int SomeOtherProperty { get; set; } } 和一个基类 abstract class Base { public DateTime DateCreated { get; set; } } 以下是我的上下文定义: public class

我有两个POCO课程:

class Email: Base
{
   public int SomeProperty { get; set; } 
}

class Photo: Base
{
   public int SomeOtherProperty { get; set; } 
}
和一个基类

abstract class Base
{
     public DateTime DateCreated { get; set; }
}
以下是我的上下文定义:

public class EntitiesContext : DbContext
{
    public DbSet<Email> Emails { get; set; }
    public DbSet<Photo> Photos { get; set; }
}
参数1:无法从“System.Data.Entity.DbSet | Email |”转换为 'System.Data.Entity.DbSet | Base |'

传递非类型的DbSet将不起作用,因为Cast将失败


所以我不确定我还能尝试什么。有什么建议吗?

您能不能将
db.Emails
集合强制转换为包含
Base
类型对象的集合?为此,您的
操作
需要变成
操作
。然后您可以执行类似的操作:
fnProcess(db.Emails.Select(e=>(Base)e))
private void GoThroughAllTables(Action<DbSet<Base>> fnProcess, bool needSave)
{
    using (var db = new EntitiesContext())
    {
        fnProcess(db.Emails);
        fnProcess(db.Photos);

        if (needSave == true)
        {
            db.SaveChanges();
        }
    }
}

public IEnumerable<QueueStatus> GetQueueStatus()
{
    var res = new List<QueueStatus>();

    GoThroughAllTables((set) =>
    {
        res.Add(new QueueStatus
        {
            Count = set.Cast<Base>().Count(x => x.DateCreated > someDate),
        });
    }, false);

    return res;
}

public void DeleteFailedItems()
{
    GoThroughAllTables((set) =>
    {
        set.Cast<Base>().Remove(x => x.DateCreated > someDate);
    }, true);

    return res;
}
fnProcess(db.Emails);