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:
- Make sure you do not expose a
DbSet<HairCutStyle>
in yourDbContext
-derived class - Make sure you do not have any mention of
HairCutStyle
in yourOnModelCreating
override - Mark your
HairCutStyle
property using theNotMapped
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
How to Change Network Settings (Ip Address, Dns, Wins, Host Name) with Code in C#
When to Use Ref and When It Is Not Necessary in C#
C# - Multiple Generic Types in One List
Making a Simple Ajax Call to Controller in ASP.NET MVC
C# Switch Statement Limitations - Why
Open Existing File, Append a Single Line
Reading 64Bit Registry from a 32Bit Application
Differencebetween a Shared Project and a Class Library in Visual Studio 2015
Error: "Cannot Modify the Return Value" C#
C# Reflection: How to Get Class Reference from String
How to Delete a Line from a Text File in C#
Entering Keys Manually with Entity Framework
Why Does My .Net 4 Application Know .Net 4 Is Not Installed
Web App Blocked While Processing Another Web App on Sharing Same Session
How to Make a .Net Windows Forms Application That Only Runs in the System Tray