小编Ale*_*ADE的帖子

EF条件包含实体类型

请假设这个架构:

public class Mammal
{
    public int Id { get; set; }
    public string Name { get; set; }
}

public class Dog : Mammal
{
    public int TailId { get; set; }
    public Tail Tail { get; set; }
}

public class Bat : Mammal
{
    public int WingId { get; set; }
    public Wing Wing { get; set; }
}

public class Buffalo : Mammal
{
    public virtual ICollection<Horn> Horns { get; set; }
}

public class Tail …
Run Code Online (Sandbox Code Playgroud)

c# entity-framework entity-framework-6

8
推荐指数
1
解决办法
907
查看次数

继承自DbSet <T>以添加属性

有没有办法从DbSet继承?我想添加一些新属性,如下所示:

public class PersonSet : DbSet<Person>
{
    public int MyProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

但我不知道如何在我的DbContext中实例化它

public partial MyContext : DbContext
{
    private PersonSet _personSet; 
    public PersonSet PersonSet
    {
        get 
        {
            _personSet = Set<Person>(); // Cast Error here
            _personSet.MyProperty = 10;
            return _personSet;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这一点?

inheritance entity-framework dbcontext dbset

4
推荐指数
2
解决办法
3382
查看次数