0

I'd like to implement a multi-tenant scenario. I only want to select my custom entities which has an Active boolean value.

I tried with the custom IDbSet<MyEntityType> + FilteredDbSet<MyEntityType>(this, c => c.Active==true) from this answer, and with the custom partial class / custom named DbSet<MyEntityTpe> MyEntitiesTypeItems + a new property with the original name + the filter from this answer.

If I run the code in debug mode, for the first some inits for my partial class, MyEntityType.Load has the proper count of Active=true items (30), but afterwards something happens (possibly a reload), and the filter is no longer active, on the Dynamic Data Scaffolding page, it will list all items (400).

I tried switching off LazyLoading and Proxying, but with no luck. Any hints what am I doing wrong?

Community
  • 1
  • 1
balint
  • 3,391
  • 7
  • 41
  • 50
  • 1
    The only way to do this right is by applying "soft deletes" through command interceptors ([code here](https://github.com/rowanmiller/Demo-TechEd2014)). You'll have to upgrade to EF6 to do that. – Gert Arnold Feb 12 '15 at 21:05
  • Simple soft delete on a bool column can be achieved much, much easier as shown here http://www.wiktorzychla.com/2013/10/soft-delete-pattern-for-entity.html Works in ef6. – Wiktor Zychla Feb 12 '15 at 22:04
  • @WiktorZychla I've seen your approach before and it's elegant. The only downside is that it "abuses" TPH inheritance so it's impossible to use other inheritance strategies in the same model. What I like about Rowan Miller's approach is that it's all in the layer below the model and the mappings. I hope one day EF will support this out of the box, like the other ORM (you know). – Gert Arnold Feb 14 '15 at 14:42
  • 1
    @GertArnold: I would die for something similar to Nhibernate's listeners. Listeners are great because you turn them conditionally, even on the same instance. The biggest downside of my approach is that I need a family of inheriting contexts to support both the one that softly deletes and the one that doesn't. The approach with interceptors that modify trees is overhelmingly complicated. This would make my day http://www.wiktorzychla.com/2012/06/how-to-implement-xpos-soft-delete.html – Wiktor Zychla Feb 14 '15 at 19:33
  • I finally upgraded to EF6, but according to http://stackoverflow.com/questions/7876586/ef-4-1-onmodelcreating-not-called none of the above will work with database-first/EDMX? OnModelCreating is never called. I have ~40 tables, so I can't just switch to code first. – balint Feb 16 '15 at 14:45

0 Answers0