Ignoring a Class Property in Entity Framework 4.1 Code First

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; }
}

How to ignore a property when using Entity Framework Code First

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

Entity Framework code first: How to ignore classes

There are three things to ensure:

  1. Make sure you do not expose a DbSet<HairCutStyle> in your DbContext-derived class
  2. Make sure you do not have any mention of HairCutStyle in your OnModelCreating override
  3. Mark your HairCutStyle property using the NotMapped attribute.

Entity Framework Code First - Ignore Base Class

NotMappedAttribute attribute is your friend:

[MetadataType(typeof(UserMd)]
public class User : System.Web.Security.MembershipUser
{
////read description bellow code
//static User()
//{
// var type = typeof(User);
// TypeDescriptor.AddProviderTransparent(
// new AssociatedMetadataTypeTypeDescriptionProvider(type), type);
//}

public int UserId { get; set; }
public string SomeProperty { get; set; }

internal class UserMd
{
[NotMapped]
public string UserName { get; set; }
}
}

I'm not sure if code first processes the metadata automatically by itself, try the above, and if it doesn't work, attach the metadata manually (i.e. uncomment the commented lines).

Update
I do like the above solution better, but if it's too verbose for you, use the StructuralTypeConfiguration<TStructuralType>.Ignore<TProperty> Method method:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
var config = modelBuilder.Entity<User>();
config.Ignore(u => u.UserName);
}

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



Related Topics



Leave a reply



Submit