0

I have quite some entities in the DB and all of them have a column decimal DBSTATE indicating if the entry is active (1) or not (0). To make a quick work of getting the instances, I created the following generic function that returns only the active columns of entities:

IEnumerable<DBType> GetActiveEntries<DBType>()
       where DBType : class, IDBEntry
{
   return db.Set<DBType>().Where(e => e.DBStateInDB == (decimal)DBState.Active).AsEnumerable();
}

IDBEntry is an interface that all the model classes implement by returning its DBSTATE value, e.g. this is how REGULARCUSTOMER implements it (irrelevant parts are omitted):

public decimal DBStateInDB => this.DBSTATE

As it turns out, this is not possible, because EF can only work with its own types in queries, this is why the following non-generic function works:

IEnumerable<REGULARCUSTOMER> GetActives_TEMP()
{
   return db.REGULARCUSTOMERs.Where(e => e.DBSTATE == (decimal)DBState.Active).AsEnumerable();
}

So my question: is it possible to somehow avoid writing separate functions/switch-cases for all the entities or I really stuck with that option?

7
  • It seems like a rather trivial function. Can't you just do away with it? Is db private or exposed to users of the class that implements GetActiveEntries? Commented Mar 13, 2018 at 14:13
  • 2
    Why are you introducing a new, notmapped property for querying? Isn't it called DBSTATE for all entities? In other words, why don't your GetActiveEntries() and IDBEntry use the property named DBSTATE? Commented Mar 13, 2018 at 14:14
  • @CodeCaster: Yes, this is true, but I don't know the exact type of the entry (hence the use of generics) so how can make sure that the generic param DBType has DBSTATE parameter? Should I use dynamic? Commented Mar 13, 2018 at 14:23
  • Your interface IDBEntry apparently has a decimal DBStateInDB { get; set; } property. Rename that to DBSTATE and you're done. Commented Mar 13, 2018 at 14:26
  • @CodeCaster: wouldn't that make it ambigous with the existing DBSTATE model classes? Commented Mar 13, 2018 at 14:29

1 Answer 1

1

I'd say you have to go around the problem.

One possible way of doing it would be to add Navigational properties (by creating a new entity type) to your entities, as DbSet<EntityState> (for example) which your entity would have a ForeignKey defining the State, this would oblige you to have a supplementary table which would contain the ID's of every entity and a bool for their state, making it clean and accessible. This also means that with your newly created Navigational property you could access it with YOUR_ENTITY.EntityState.State or YOUR_ENTITY.EntityState.Id, it would also give you the capacity to modify/access a entities State regardless of their type because of the new table. Your type enherited rules will still apply and changing the table will change the entity, regardless of Type.

But again, this is only a way of doing. It should be relatively simple to implement and can be quite powerful.

Let me know what you think.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you, but CodeCaster's simple solution solved my issue :)
@rTECH No problem, Good on you ! ;) Good luck on the project

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.