5

I'm using Entity Framework in C#. And I want exclude some columns while getting data from database. For example, my student table has 10 colums. And there is some columns in table like CreatedDate, CreatedBy, CreateRole, UpdatedDate, Updatedby, UpdatedRole. I want generic solution for exclude this column while getting list from database like below.

I'm looking for like below

 context.Students   
.Exclude("CreatedDate","CreatedBy","CreateRole","UpdatedDate","Updatedby","UpdatedRole")
.ToList();

Please don't advice below solution, because this is not what I'm looking for.

context.Students.Select(p=>new {
p.Name,
p.Surname,
p.Number,
p.BirthDate
}).ToList();
6
  • You might wan't to add the code for your model as well. The correct solution depends on whether or not these properties are included on your model. Commented Nov 30, 2018 at 7:54
  • Most of time, I don't use this colums. But sometime I use. So, I can't remove from model. If I remove from model, I can't access when I need for example CreatedDate column @Noceo Commented Nov 30, 2018 at 7:59
  • I don't know any clean way, to only load some model properties, without always excluding them (using the [NotMapped] attribute). You could move the rarely used attributes, to a model of their own, and included it in the primary model (creating a one-to-one relationship). That would make it possible to only load these extra properties on demand. Commented Nov 30, 2018 at 8:07
  • Thanks for your reply @Noceo Commented Nov 30, 2018 at 8:25
  • An important thing I forgot to ask is: why do you need to exclude these properties sometimes? In most scenarios the performance hit is likely so tiny, that it won’t justify the added complexity to the code. Commented Nov 30, 2018 at 9:09

2 Answers 2

1

As stated in my comments, I would create a model, for the properties which are only needed occasionally:

public class CreateAndUpdatePropertiesModel
{
    public DateTime CreatedDate { get; set; }
    public string CreatedBy { get; set; }
    public DateTme ModifiedDate { get; set; }
    public string ModifiedBy { get; set; }
    // ...and so on
}

And then use this model as a property in my primary model:

public class StudentModel
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime DateOfBirth { get; set; }
    // ...rest of the properties here

    // And then add the CreateAndUpdateProperties model as a property
    public CreateAndUpdateProperties { get; set; }
}

Then when you select items from Entity Framework, you can use

.Include(s => s.CreateAndUpdateProperties)

if you want to include the create and update properties. Without this include, it would just be null.

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

3 Comments

Thanks a lot @Noceo. This is very helpful and useful information. And it can solve the most people's problem who looking for question telling. But, I can't change my entity infrastructure. Because, I did a lot of customization. So, I will looking for class level instead of base level.
I suppose you could make two different EF contexts for the same database. One which loads the full model and one which only loads it partially. This can be controlled with FluentAPI. But it might be quite tricky to handle.
Yes.It can be also. Thanks again @Noceo. I found a solution for my situation. ProjectTo method of AutoMapper is solved my problem. You can see from my this post. stackoverflow.com/a/53564646/10176050
-1

You can modify DBContext as follows.

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    modelBuilder.Entity<Students>().Ignore(t=>t.CreatedDate).Ignore(t=>t.CreatedBy).Ignore(t=>t.CreateRole).Ignore(t=>t.UpdatedDate).Ignore(t=>t.Updatedby).Ignore(t=>t.UpdatedRole);

    base.OnModelCreating(modelBuilder);
}

1 Comment

Thanks @jungosong. I thought this solution, but in this case, when I need CreatedDate , it wil be problem.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.