Sinem Bozacı
Sinem Bozacı

Reputation: 45

Entity Framework - Writing function in a class - Need to call with same provider

I have two table called Project and ProjectComments on my database.

I mapped them these two tables to my Entity Framework Model with generator.

Each project have many ProjectComments. I don't want to add ProjectComments as a navigation property to Project because i want to write a GetProjectComments function to Project class.

Btw i can make desicion in GetProjectComments function for IsDeleted column of ProjectComments table. Because i don't really delete any rows from ProjectComments table, i only make its IsDeleted = true if i need to delete, thus i shouldn't return ProjectComments whose IsDeleted = true in GetProjectComments function.

It's okay up to now. The problem is that in GetProjectComments function of Project class i need provider(context object) as parameter to call SaveChanges method of the same provider.

Is there any method to make these approach without asking provider object as a parameter in GetProjectComments function. Am i doing something wrong??

Looking for you reply. Thanks a lot.

Upvotes: 0

Views: 315

Answers (1)

Raphaël Althaus
Raphaël Althaus

Reputation: 60503

Easier :

Add a ProjectComments navigation property in Project class (well let the generator do the job)

Add a new property in a partial "Project" class

public IEnumerable<ProjectComments> LivingComments {
   get {
      return this.ProjectComments != null 
         ? this.ProjectComments.Where(m => !m.IsDeleted) 
         : null;
   }
}

EDIT or as danludwig stated

public IEnumerable<ProjectComments> GetLivingComments() {
     return this.ProjectComments != null 
         ? this.ProjectComments.Where(m => !m.IsDeleted) 
         : null;
}

Upvotes: 2

Related Questions