所以我试图从 EF 覆盖 DbSet,但它没有像我想象的那样工作。
成员“IQueryable.Provider”尚未在“RequestedDocumentDbSet”类型上实现
我只想覆盖Remove
andRemoveRange
方法。这不可能吗?我必须覆盖所有内容吗?
如果是这样,是否有更好的解决方案来做到这一点?
这是 2 个对象的模型:
public class RequestedDocument
{
[DataMember]
public int Id { get; set; }
[ForeignKey("UploadedDocument")]
[DataMember]
public int? UploadedDocumentId { get; set; }
public UploadedDocument UploadedDocument { get; set; }
//some other properties
}
public class UploadedDocument
{
public int Id { get; set; }
public Byte[] Bytes { get; set; }
[DataMember]
[NotMapped]
public string FileName { get; set; }
}
public class RequestedDocumentDbSet : DbSet<RequestedDocument>
{
private readonly ApplicationDbContext _context;
public RequestedDocumentDbSet()
{
}
public RequestedDocumentDbSet(ApplicationDbContext context):base()
{
_context = context;
}
public override RequestedDocument Remove(RequestedDocument entity)
{
if (entity.UploadedDocumentId != null)
{
UploadedDocument uploadedDocument = new UploadedDocument {Id = entity.UploadedDocumentId.Value};
// base.Attach()
}
return base.Remove(entity);
}
}
我的数据库上下文依次如下所示:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public RequestedDocumentDbSet RequestedDocuments { get; set; }
public DbSet<UploadedDocument> UploadedDocuments { get; set; }
}
如您所见,RequestedDocument
对UploadedDocument
.
我需要覆盖的原因是,我可以删除一个子对象,而无需将其加载到内存中,也不必在整个地方提供此代码,而是在一个集中的地方提供。
我想要的是每次 aRequestedDocument
被删除时也UploadedDocument
被删除。