c# - With a generic repository for entity framework, how do I query entities which implement a specific interface? -


After

I have been using the store pattern with the outline of the unit and have implemented a normal store.

However for some type, if I want to set the IsDeleted bool true, then I do not want to remove the record from the database.

For this effect, I have made this simple interface:

IEntityProtectedDelete.cs

  Public InterfaceIntityProtectedDelete {Bool isDeleted {get; Set; }}   

This has been implemented on my organization like this:

Queue.cs

  Public Class Queue: Remove IEntity Protect {Public Queue () {IsDeleted = false; } [Key] Public int queId {get; Set; } [Required] Public string name {get; Set; } [Required] Public Bool Isledged {get; Set; }}   

Below I am using a part of the generic repository. The delete method works as expected, and when setting the IsDelete property to correct when using the above interface.

However, how to correctly query the database, then the unit tool IEntityProtectedDelete .

GenericRepository.cs

  Do the public class GenericRepository & lt t> Where T: class {secure DBCct reference; Protected IDBSet dbSet; Public generic repository (iCext Reference) {this.context = context; This.dbSet = context.Set & lt; T & gt; (); } Public Virtual Zero Deletion (T Entity) {dbSet.Attach (unit); If the unit is ProtectedDelete (IEntity) {(unit as IEentity ProtectedDelete) .IsDeleted = true; Context.Entry (unit) .set = EntityState.Modified; } And {dbSet.Remove (unit); }} Public Virtual IEnumerable & lt; T & gt; GetAll () {if typeof (IEntityProtectedDelete) .IsAssignableFrom (typeof (t))} {context.Set & lt Return; T & gt; () OfType & Lt; IEntityProtectedDelete & gt; () where. (D = & gt; e.IsDeleted == incorrect). In the form of IONMERABLE The list (GettyBuilder) can be found in the list (Getty Images) (<< code>   In the,  OfType  throws an exception:  
  'IEntityProtectedDelete' type is not a valid metadata type for filtering functions Type filtering only entity type and Valid in complex types. "  

If this is not working, then what are the other options?

Soft display pat for unit framework The solution is considered and the accepted solutions are described here

The trick is to define your isDeleted discriminator column Form so that FE automatically adds subquery filtering.

Comments

Popular posts from this blog

Java - Error: no suitable method found for add(int, java.lang.String) -

java - JPA TypedQuery: Parameter value element did not match expected type -

c++ - static template member variable has internal linkage but is not defined -