prospector
prospector

Reputation: 3469

Change an entity condition in linq

In the entity designer I set the conditional mapping on an entity where it filters all customers by an 'isactive' field.

That works great no problem with that, but the problem comes in my linq.

What happens when I need to set a customer to 'IsActive=false'. The property isn't mapped to my entity because the condition is mapped, but in Linq I can't find a way to change that.

using (var db = new CustDbConn())
{

   Customer customer= db.Customers.Single(p => p.Id == idFromEmail);
   customer.IsActive = false; //<----NOT FOUND, can not resolve 'isActive'

}

There has to be a way to access and change the conditions in LINQ, can someone shed some light on this? I thought about bypassing the model and just updating it using SqlCommand, but I shouldn't have to go through all that.

Upvotes: 0

Views: 180

Answers (1)

Dennis
Dennis

Reputation: 37770

You can't change model (that's you're trying to do) using LINQ query.

The only way to manipulate IsActive property is to bring it into Customer model, and throw away conditional mapping.

UPD.

You're misunderstanding the main purpose of conditional mapping - inheritance.
Note, that using EF you don't work with database tables, you work with entity types. All you have, is a model. If any table field is missing from model, than yes, you can't access it via model. With reference to your question, yes, you can't change patients activity status, because there's no activity status in the model.

And yes, if you plan to use some sort of deletion/activity marker (like IsActive in you sample), you have to include it in your queries, otherwise you'll get inactive/deleted items in results. Of course, you can make helper/repository/wrapper/etc to automate it:

interface IEntity
{
    int Id { get; }
    bool IsActive { get; set; }
}

interface IRepository<T>
    where T : IEntity
{
    IQueryable<T> Get(bool includeInactiveEntities = false);
}

Upvotes: 1

Related Questions