0

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.

Tikkes
  • 4,599
  • 4
  • 36
  • 62
  • Please add the actual line the yields this error. – H H Jul 24 '15 at 11:04
  • 1
    It's hardly about the line generating the error but more about the class and the extention of DbSet – Tikkes Jul 24 '15 at 11:37
  • I can't reproduce this. List the version(s) involved and (again) the full error. The actual line (statement or declaration) does matter. – H H Jul 24 '15 at 12:07
  • I updated the question, hopefully it makes it more clear – Tikkes Jul 24 '15 at 12:12
  • No, still not clear and not complete. – H H Jul 24 '15 at 12:21
  • See: [How to create a Minimal, Complete, and Verifiable example.](http://stackoverflow.com/help/mcve) – H H Jul 24 '15 at 12:23
  • 1
    What are you trying to do? Overriding DbSet is *not* the place to put business logic. EF expects to find certain interfaces implemented on the class and obviously can't find them. You'd have to implement them in your own class as well. Better though to put your logic in the correct place – Panagiotis Kanavos Jul 24 '15 at 12:23
  • What I want is that every time a `RequestedDocument` gets removed also the `UploadedDocument` is removed. @Henk: I do not see what it is you want me to write more in my question. It seems to be a stupid downvote imho as this is a valid question in the first place. – Tikkes Jul 24 '15 at 12:30
  • So you are trying to enforce a cascade delete? Why not use `WillCascadeOnDelete(true)` on the relation, as shown eg [here](http://stackoverflow.com/questions/17487577/entity-framework-ef-code-first-cascade-delete-for-one-to-zero-or-one-relations)? – Panagiotis Kanavos Jul 24 '15 at 12:38
  • 1
    This is a classic case of composition is better than inheritance. Wrap the dbset in an adapter – Nick Bailey Jul 24 '15 at 13:08
  • @Tikkes - no it was not a valid question as I could not reproduce your error. So something was missing. – H H Jul 27 '15 at 09:43

0 Answers0