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 For this effect, I have made this simple interface: IEntityProtectedDelete.cs This has been implemented on my organization like this: Queue.cs Below I am using a part of the generic repository. The However, how to correctly query the database, then the unit tool GenericRepository.cs 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 bool true, then I do not want to remove the record from the database.
Public InterfaceIntityProtectedDelete {Bool isDeleted {get; Set; }}
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; }}
delete method works as expected, and when setting the
IsDelete property to correct when using the above interface.
IEntityProtectedDelete .
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. "
isDeleted discriminator column Form so that FE automatically adds subquery filtering.
Comments
Post a Comment