2

Possible Duplicate:
Can a DbContext enforce a filter policy?

look at this code , this is one of my entities :

class User
{
    ...
    ...
    ...
    public bool IsDeleted { get ; set; }

}

when i want to delete an user , i set the IsDeleted property to true and update it. and this is my DbContext :

class DataContext : DbContext
{
    ...
    ...
    ...
    public DbSet<User> Users { get; set; }

}

Now here is the question :

How I can change my DbContext to return undeleted object when i just use new DataContext().Users

Sorry about my bad syntax .I am new in English. For more details comment me

UPDATE1 : I change my DBContext to this code , but i got an error

public DbSet<DT.DTO.User> Users
{
    get
    {
        return this.Set<User>().Where(rec => !rec.IsDeleted)
    }
    set;
 }

Error : Cannot implicitly convert type 'System.Linq.IQueryable' to 'System.Data.Entity.DbSet'. An explicit conversion exists (are you missing a cast?)

Community
  • 1
  • 1
Ali Foroughi
  • 4,540
  • 7
  • 42
  • 66
  • after setting the property `IsDeleted` are you saving the changes to the db ? – Habib Jul 16 '12 at 11:10
  • are you using model first (edmx file) or code first approach? – daryal Jul 16 '12 at 11:11
  • @daryal , sorry i forgot to mentioned that , I am using Code First Approach – Ali Foroughi Jul 16 '12 at 11:12
  • The property must be of type `IQueryable`, not `DbSet`. – Slauma Jul 16 '12 at 11:26
  • @Slauma , When I use IQueryable , the EF does not create my table when i user CreateDatabase method of my DBContext ,I think it must be just DBSet, an it inform EF to create a table in my database – Ali Foroughi Jul 16 '12 at 11:29
  • 1
    Try Ladislav's answer in the question Wouter has linked above, i.e.: add a private `DbSet` to the context class. If it doesn't work you probably need to make the DbSet public. You can also make an entity available if it is used with Fluent API, with any call like `modelBuilder.Entity().ToTable("Users");`. – Slauma Jul 16 '12 at 11:39
  • @Slauma , As you said , i found my question in the above link , thanks all – Ali Foroughi Jul 16 '12 at 11:52

1 Answers1

0

I'd recommend using some kind of service class or repository as a layer above your DataContext. Something similar is explained in another thread over here: https://stackoverflow.com/a/5680618/40853

The service should handle the data filtering or stuff like paging - the implementation depends heavily on what you want to achieve.

One aim of this kind of implementation could be to reduce the dependency on the database access layer (EF in your case) to a minimum outside of the code that is written specifially for the database access.

Something like the following could abstract away how you handle the data in the database.

public class DatabaseService : IDatabaseService
{
    public IEnumerable<User>  GetUsers(someconditions)
    {
        // someconditions can be any kind of condition to include in the query
        using (var db = new DataContext())
        {
            return db.Users.Where(rec => !rec.IsDeleted
            && someconditions).ToList();
        }
    }

    [...] implement stuff like DeleteUser(guid), UpdateUser(User user))

}
Community
  • 1
  • 1
mattanja
  • 1,442
  • 14
  • 21
  • 2
    Returning `IEnumerable` and disposing the context is not a good idea here. – Slauma Jul 16 '12 at 11:44
  • Oh, that's even worse. Now you always load all not-deleted users from the DB before you apply any further filters in memory. I meant: You need to return `IQueryable` and remove the `using` block (it disposes the context at the end of the block and you could not apply any filters on the returned Queryable). But this solution has already been proposed in the duplicate question, see link of Wouter de Kort in the comments to the question above. – Slauma Jul 16 '12 at 11:58
  • You're so right! What I had in mind was more like a service that handles all the data filtering in a way that the data returned by the service contains the complete result set to be displayed or handled... Corrected the answer. – mattanja Jul 16 '12 at 12:21