Exclude a Field/Property from the Database with Entity Framework 4 & Code-First

Exclude a field/property from the database with Entity Framework 4 & Code-First

In the current version the only way to exclude a property is to explicitly map all the other columns:

builder.Entity<Employee>().MapSingleType(e => new {
e.Id,
e.Name,
e.FatherName,
e.IsMale,
e.IsMarried
});

Because AddressAs is not referenced it isn't part of the Entity / Database.

The EF team is considering adding something like this:

builder.Entity<Employee>().Exclude(e => e.AddressAs);

I suggest you tell leave a comment on the EFDesign blog, requesting this feature :)

Hope this helps

Alex

Exclude columns getting data with Entity Framework

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.

Ignoring a class property in Entity Framework 4.1 Code First

You can use the NotMapped attribute data annotation to instruct Code-First to exclude a particular property

public class Customer
{
public int CustomerID { set; get; }
public string FirstName { set; get; }
public string LastName{ set; get; }
[NotMapped]
public int Age { set; get; }
}

[NotMapped] attribute is included in the System.ComponentModel.DataAnnotations namespace.

You can alternatively do this with Fluent API overriding OnModelCreating function in your DBContext class:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().Ignore(t => t.LastName);
base.OnModelCreating(modelBuilder);
}

http://msdn.microsoft.com/en-us/library/hh295847(v=vs.103).aspx

The version I checked is EF 4.3, which is the latest stable version available when you use NuGet.


Edit : SEP 2017

Asp.NET Core(2.0)

Data annotation

If you are using asp.net core (2.0 at the time of this writing), The [NotMapped] attribute can be used on the property level.

public class Customer
{
public int Id { set; get; }
public string FirstName { set; get; }
public string LastName { set; get; }
[NotMapped]
public int FullName { set; get; }
}

Fluent API

public class SchoolContext : DbContext
{
public SchoolContext(DbContextOptions<SchoolContext> options) : base(options)
{
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().Ignore(t => t.FullName);
base.OnModelCreating(modelBuilder);
}
public DbSet<Customer> Customers { get; set; }
}

EF Code First exclude column

You can add a [NotMapped] attribute to the property you want to exclude from the database:

public string Address { get; set; }

[StringLength(40)]
public string City { get; set; }

[StringLength(30)]
public string State { get; set; }

[StringLength(10)]
[NotMapped]
public string Zip { get; set; }

Exclude column from being updateable in Entity Framework 4.1 Code First

If you are working with attached entities you EF will generate updates only for fields which have changed. If you are working with detached entities you must manually say EF what has changed. If you call this:

context.Entry(yourEntity).State = EntityState.Modified;

you are saying EF that all properties should by modified. But if you instead call this:

context.Entry(youreEntity).Property(e => e.SomeProperty).IsModified = true;

you will say that only SomeProperty is modified (only this property will be in update). I'm not sure if you can do the reverse operation by marking the whole entity as modified and select properties which should not be modified but you can test it yourselves.

If your CreatedOn is filled in the database you can mark it as DatabaseGeneratedOption.Identity and it will be never modified by your application.

Always exclude property from Entity Framework 4 Query

Create a User Repository that uses a POCO to store the User without the Password and Salt fields.

The Repository handles calling Entity Framework and populating your POCO with its data.

namespace MyCompany.Data.Repositories
{
public class User
{
public int Id { get; set; }
public string Username { get; set; }
public string Name { get; set; }
public bool Locked { get; private set; }
}

public class UserRepository
{
public User GetAll() { }
public User GetById() { }

// Add your check password method here
}
}

How to ignore a property when using Entity Framework Code First

Add the [System.ComponentModel.DataAnnotations.Schema.NotMapped] attribute to the property.

Exclude certain column from Entity Framework select statment

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;


Related Topics



Leave a reply



Submit