覆盖DbSet - 删除功能

时间:2015-07-24 10:11:25

标签: c# .net entity-framework dbset

所以我试图从EF覆盖DbSet,但它没有像我想象的那样工作。

成员'IQueryable.Provider'尚未在'RequestedDocumentDbSet'类型上实施

我只想覆盖RemoveRemoveRange方法。 这不可能吗?我必须覆盖一切吗?

如果是这样,有没有更好的解决方案呢?

这是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; }
}

您可以看到RequestedDocumentUploadedDocument的引用。

我需要覆盖的原因是我可以删除一个子对象,而不必将其加载到内存中,而不必在整个地方提供此代码,而是在一个集中的位置提供。

我想要的是,每次移除RequestedDocument时,UploadedDocument都会被删除。

0 个答案:

没有答案