I have a class Tenant
that inherits from UserProfile
. I'm using table-per-type inheritance so my context class looks like this:
我有一个继承自UserProfile的类Tenant。我正在使用table-per-type继承,所以我的上下文类看起来像这样:
// 1 DbSet for superclass UserProfile
public DbSet<LetLord.Models.UserProfile> UserProfile { get; set; }
I'm using repository classes for my data access and I created TenantRepository
with the following package manager command:
我正在使用存储库类进行数据访问,并使用以下包管理器命令创建了TenantRepository:
Scaffold Controller Tenant -Repository
脚手架控制器租户 - 存储库
When I try to run my application, all references to Tenant in the TenantRepository throw the following error...
当我尝试运行我的应用程序时,TenantRepository中对Tenant的所有引用都会引发以下错误...
'MyNamespace.MyContext' does not contain a definition 'Tenant' and no extension for 'Tenant' accepting a first argument 'MyNamespace.MyContext' could be found.
'MyNamespace.MyContext'不包含定义'Tenant',并且没有'Tenant'接受第一个参数'MyNamespace.MyContext'的扩展名。
...such as the following reference:
......如下参考:
public IQueryable<Tenant> All
{
get { return context.Tenant; } // error line here
}
When using table-per-type inheritance only a DbSet
for base classes should be included, so I understand why I'm getting the error.
当使用每个类型的表继承时,只应包含基类的DbSet,所以我理解为什么我会收到错误。
How is using repositories with derived classes implemented in scenarios such as mine?
如何在我的场景中使用具有派生类的存储库?
EDIT
编辑
How is above accomplished when using .Add()
, .Find()
, .Remove()
etc?
使用.Add(),.Find(),. Remove()等时如何完成?
Same error as described above for the previously mentioned methods:
与前面提到的方法相同的错误:
public Tenant Find(int id)
{
return context.UserProfile.OfType<Tenant>().Find(id); // error at .Find()
}
1 个解决方案
#1
1
Try this:
尝试这个:
public IQueryable<Tenant> All
{
get { return context.UserProfile.OfType<Tenant>(); }
}
This will only return Tenants.
这只会返回租户。
For other methods like Add, Find, Remove:
对于其他方法,如添加,查找,删除:
public Tenant Find(int id)
{
// a few different options here -- assumes your key property is Id
return context.UserProfile.OfType<Tenant>().SingleOrDefault(t => t.Id == id);
// option 2
// even though your context does not expose a DbSet<Tenant>, you can still
// use the Set<TResult>() method to get only tenants this way
return context.Set<Tenant>().Find(id);
}
public void Add(Tenant tenant)
{
context.Add(tenant);
}
public void Remove(Tenant tenant)
{
context.Set<Tenant>().Remove(tenant);
}
#1
1
Try this:
尝试这个:
public IQueryable<Tenant> All
{
get { return context.UserProfile.OfType<Tenant>(); }
}
This will only return Tenants.
这只会返回租户。
For other methods like Add, Find, Remove:
对于其他方法,如添加,查找,删除:
public Tenant Find(int id)
{
// a few different options here -- assumes your key property is Id
return context.UserProfile.OfType<Tenant>().SingleOrDefault(t => t.Id == id);
// option 2
// even though your context does not expose a DbSet<Tenant>, you can still
// use the Set<TResult>() method to get only tenants this way
return context.Set<Tenant>().Find(id);
}
public void Add(Tenant tenant)
{
context.Add(tenant);
}
public void Remove(Tenant tenant)
{
context.Set<Tenant>().Remove(tenant);
}