0

I have a scenario as follow:

  1. Project is using EF Database first (so not modifying the generated EF model)
  2. We don't create new Model nor using AutoMapper as it is overkill for project.
  3. Often time, all column from database will be return, exclude some sensitive data.
  4. Suggestion to create an anonymous type (eg: x => new { }) and specific binding one by one is also tedious (as it has many field to return and also need to specify explicitly one by one the fields)

So, after certain considerations, I am thinking excluding properties in linq queries result is the best solution for my case.

Expectation: Expect that we can EXCLUDE some properties. Note that ExcludeProp should already exclude select those properties when generate linq to sql queries. How can I do that?

For example:

entities.tblCampaigns.Include(x => x.tblUser).Include(x => x.tblCampaignsPhotos)
  .ExcludeProp(x => x.UpdatedDT) // Ignore campaign updated datetime
  .ExcludeProp(x => x.tblUser.Password) // Ignore user password
  .ExcludeProp(x => x.tblCampaignPhotos.tblCampaign) // Ignore entire campaign reference in campaign photos
eulercode
  • 1,107
  • 4
  • 16
  • 29
  • 3
    You don't exclude properties, you just don't `Select` them – DavidG Apr 06 '21 at 10:41
  • Excluding property from the query, mean that instead of `SELECT * FROM ...` EF should generate `SELECT` with only included columns. – Fabio Apr 06 '21 at 10:41
  • No, it's not. The solution is to use a `Select` call with the properties you want. If using an anonymous type is so tedius, it means the *entities are wrong* and contains fields they don't need. Or it means you're treating the entities as if they were tables (they aren't). – Panagiotis Kanavos Apr 06 '21 at 10:42
  • @DavidG yes i mean when i call ExcludeProp then linq should auto generate the select statement without those properties in. – eulercode Apr 06 '21 at 10:44
  • If you only want a certain set of properties, create an *entity* that contains only those properties. An entity isn't a table, a DbContext isn't a database connection or model. It's perfectly fine to have different DbContexts with different entities mapping to the same tables for different use cases or scenarios. A DbContext is a Unit-of-Work containing the entities needed to work for a specific use case/bounded context – Panagiotis Kanavos Apr 06 '21 at 10:44
  • @eulercode `when i call ExcludeProp` properties aren't created by themselves. They have to be explicitly added by the developer. If you don't want some properties, don't add them – Panagiotis Kanavos Apr 06 '21 at 10:45
  • it's looks like request to EF developers – demo Apr 06 '21 at 10:45
  • @PanagiotisKanavos you are talking about propert OOP software engineering principal. I am looking for solution that is least time consuming for the project. In this case I would like the ExcludeProp in linq function to do the job to ignore select those field when converting to sql query. Because at most of the time we only need to exclude certain properties, so definitely we are not going to have a DTO layer and automapping. is that possible? – eulercode Apr 06 '21 at 10:46
  • @eulercode no, I'm talking about the easy solution to the problem. You're looking to cover up a bug. EF Core doesn't work that way. If anything, you're actually asking for AutoMapper. The features you ask for *are provided by AutoMapper* – Panagiotis Kanavos Apr 06 '21 at 10:47
  • @PanagiotisKanavos by using automapper that means we need to create another DTO layer, which are things that we are avoiding as it is troublesome and time consuming. And we are using same naming for database column and return properties. As you can see directly exclude select properties when linq to sql is the most time saving. – eulercode Apr 06 '21 at 10:49
  • If you are returning your entities to your domain/presentation layer, then you're doing things wrong. There's a very good reason those things are split up. – DavidG Apr 06 '21 at 10:50
  • 2
    @eulercode but that's exactly what you asked for here - what kind of object would your assumed `ExcludeProp` create? – Panagiotis Kanavos Apr 06 '21 at 10:50
  • 1
    https://stackoverflow.com/a/10385738/3917754 maybe something like this? just use NotMapped attr? – demo Apr 06 '21 at 10:50
  • @PanagiotisKanavos for example by default is SELECT * from tblUser, so when i use ExcludeProp(x => x.Password) i expect linq to auto generate relevant query to select all field in tblUser except Password. – eulercode Apr 06 '21 at 10:51
  • @demo as mentioned my table model is directly from EF Model first. So we cant directly modified as it is auto generated by EF. and we are not planning to create a new class for that as well. – eulercode Apr 06 '21 at 10:53
  • 1
    @eulercode that's not what I asked. What *object* would you expect that query to return? The original with `null` for the missing properties? That would lead to a ton of other bugs. Some other type? Wouldn't you have to *write* that type? – Panagiotis Kanavos Apr 06 '21 at 10:53
  • @eulercode Model first means *you* create the model. You can add the entities you need. Frankly, this question can't be answered. You're trying to find a way to misuse EF Core instead of fixing the problems in the model. Well, that's just not how EF or any other ORM works – Panagiotis Kanavos Apr 06 '21 at 10:55
  • @PanagiotisKanavos It should be default value based on the excluded properties type. But of course, it would be best if it can directly remove the entire property name from return result. – eulercode Apr 06 '21 at 10:55
  • @PanagiotisKanavos sorry I mean database first to generate model – eulercode Apr 06 '21 at 10:56
  • @PanagiotisKanavos Well yes that's why i use EF, but I expect linq can help in exclude properties and linq will directly transfer it into select statement with those properties excluded and this will save tonnes of job. If we can explicitly select with **anonymous type**, I don't see how explicitly excluding properties does not fit in. – eulercode Apr 06 '21 at 10:59
  • What does "remove the entire property name from return result" mean in C# terms? C# is strongly type driven, and types used by EF are static and have properties. The same type can't both have a property and not have a property - a type without one of its properties __is a different type__. So what you are asking for isn't possible with EF Core / C#. – NetMage Apr 06 '21 at 20:47
  • @eulercode, I afraid you spent more time here then you would spent to write `.Select` method to return anonymous object with only properties you need ;) – Fabio Apr 07 '21 at 01:29

0 Answers0