简体   繁体   中英

Equivalent to “SELECT NULL AS ColumnName” in Entity Framework

I have a scenario which requires to return some column values as NULL.

I know you can use projections to limit the columns returned or SELECTed, but this is not what we would like to do.

As a simplified example, say I have a 5 column table, called dbo.Person

+------+---------+-----+------------------------------------------------------------------------+------------------------------------------------+
| Name | Surname | Age |                                 About                                  |                     Likes                      |
+------+---------+-----+------------------------------------------------------------------------+------------------------------------------------+
| Bob  | Doe     |  22 | Grew up in no-man's land. Very strong. Also known as Bobby             | Burgers, Taking care of fish, kissing dolphins |
| Jane | Doe     |  21 | Grew up on the space station. Supremely intelligent. Also known as Jay | Icecream, Pug named Star Freak, Petting lions  |
| John | Doe     |  25 | Grew up in a whale. Can sing in sonar. Also known as Unknown           | Krill, Box Jellyfish, snacking on sea weed.    |
+------+---------+-----+------------------------------------------------------------------------+------------------------------------------------+

I have an entity class related to the table above

[Table("Person")]
public class Person
{
    public string Name {get; set}

    public string Surname {get; set;}

    public string Age {get; set;}

    public string About {get; set;}

    public string Likes {get; set;}
}

I have defined a context corresponding to the database: PeopleContext : DbContext

I have public DbSet<Person> Person{ set; get;} public DbSet<Person> Person{ set; get;} defined in PeopleContext

Assume all else is setup and working,

Without having to project to another object other than Person , How do I write my Entity Framework query to be equivalent to the following?

SELECT
   Name,
   Surname,
   Age,
   NULL AS About,
   NULL AS Likes 
FROM dbo.Person

This is a ViewModel/Consumer problem, not an entity problem. An Entity should always reflect the actual data state, or the intended data state.

If you intend to clear out the About and Likes then:

var person = context.People.Single(x => x.PersonId == personId);
person.About = null;
person.Likes = null;

When/if you call SaveChanges() on the context, these intended changes will be persisted. This is a dangerous option if you don't intend to save the change because while it might work today, tomorrow could be the day someone introduces a requirement to save something with the context.

If you want to use a Person as though they have no About or Likes value:

var personViewModel = context.People
    .Where(x => x.PersonId == personId)
    .Select(x => new PersonViewModel
    {
        Name = x.Name,
        Surname = x.Surname,
        Age = x.Age
    }).Single();

The view-model can contain an About and Likes value that defaults to #null if needed.

Alternatively, if you just want to consume that data on the spot without pulling back the extra detail of the person's About & Likes you can use an anonymous type:

var personDetails = context.People
    .Where(x => x.PersonId == personId)
    .Select(x => new 
    {
        x.Name,
        x.Surname,
        x.Age
    }).Single();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM