Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exclude certain column from Entity Framework select statment [duplicate]

I have an image column in the product table in a SQL Server database. The image column is used to save the images as bytes.

I know it is better to make a separate table for images, but I did not do that, so is there any way to exclude the image column when I am trying to list the products only without the images?

like image 703
Laila Avatar asked Aug 24 '17 10:08

Laila


People also ask

How to avoid duplicate records in Entity Framework?

Duplicate rows in Entity Framework SQL View Entity Framework auto set fields as entity key for those not null column and return the row that match those entity key that causes the problem. You can set AsNoTracking option directly on your view to resolve this issue.

How do I select specific columns in Entity Framework?

We can do that simply by using the “new” operator and selecting the properties from the object that we need. In this case, we only want to retrieve the Id and Title columns. There. That looks better.

How can I tell entity framework to save changes only for a specific DbSet?

Initially you find all entities whose state is not unchanged and save their entry. Then you set the state of every entity that isn't of your type TEntity and set their state to unchanged. Then call the base. SaveChanges() to save all changes to entities of your type.


1 Answers

Create a DTO with all the properties you need except the image property:

public class YourDTO
{
    public string YourProperty1 { get; set; }
    public string YourProperty2 { get; set; }
    // etc
}

You can then do:

var productDto = context.Products
                        .Where(x => x.Id == productId)
                        .Select(x => new YourDTO {
                            YourProperty1 = x.DbProperty1,
                            YourProperty2 = x.DbProperty2        
                            // etc, don't include the image column
                        });

Update:

If you don't want to map the results to YourDTO, you can project into an anonymous type:

var product = context.Products
                     .Where(x => x.Id == productId)
                     .Select(x => new {
                         x.DbProperty1,
                         x.DbProperty2        
                         // etc, don't include the image column
                     });

...and if you want to provide a custom name for each of the properties of the anonymous type:

var product = context.Products
                     .Where(x => x.Id == productId)
                     .Select(x => new {
                         YourProperty1 = x.DbProperty1,
                         YourProperty2 = x.DbProperty2        
                         // etc, don't include the image column
                     });

All of the above approaches would be functionally equivalent to the following SQL:

SELECT p.DbProperty1, p.DbProperty2 
FROM products p
WHERE p.Id = WhateverId;
like image 169
trashr0x Avatar answered Sep 20 '22 18:09

trashr0x