3

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();
Gert Arnold
  • 105,341
  • 31
  • 202
  • 291
realist
  • 2,155
  • 12
  • 38
  • 77
  • 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. – Jakob Busk Sørensen Nov 30 '18 at 07: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 – realist Nov 30 '18 at 07: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. – Jakob Busk Sørensen Nov 30 '18 at 08:07
  • Thanks for your reply @Noceo – realist Nov 30 '18 at 08: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. – Jakob Busk Sørensen Nov 30 '18 at 09:09
  • If the application is not big, you're right. But in big project, it's important that performance. Also, I wrote to question only six coolumn, but in real scenario, eleven column.@Noceo – realist Nov 30 '18 at 09:33

2 Answers2

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.

Jakob Busk Sørensen
  • 5,599
  • 7
  • 44
  • 96
  • 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. – realist Nov 30 '18 at 13:40
  • 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. – Jakob Busk Sørensen Nov 30 '18 at 15:47
  • 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. https://stackoverflow.com/a/53564646/10176050 – realist Nov 30 '18 at 20:52
-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);
}
junho song
  • 19
  • 3
  • 2
    Thanks @jungosong. I thought this solution, but in this case, when I need `CreatedDate` , it wil be problem. – realist Nov 30 '18 at 08:28