So I'm trying to override the DbSet from EF but it's not working as I thought it would.
The member 'IQueryable.Provider' has not been implemented on type 'RequestedDocumentDbSet'
I only want to override the Remove
and RemoveRange
method.
Is this not possible? Do I have to override everything?
If so, is there a better solution to do this?
This is the model of the 2 objects:
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);
}
}
and my DB context looks in turn like this:
public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{
public RequestedDocumentDbSet RequestedDocuments { get; set; }
public DbSet<UploadedDocument> UploadedDocuments { get; set; }
}
So as you can see, RequestedDocument
references to UploadedDocument
.
The reason I need to override is so that I can delete a child object without having it loaded in memory and not having to supply this code all over the place but rather in one centralized place.
What I want is that every time a RequestedDocument
gets removed also the UploadedDocument
is removed.