Home >Backend Development >C++ >How to Ignore Class Properties in Entity Framework Code First?
Ignoring a Class Property in Entity Framework 4.1 Code First
In Entity Framework (EF) 4.1, you may encounter the need to exclude certain properties from being included in the database schema. Fortunately, there are two approaches to achieve this.
[NotMapped] Attribute
This attribute is part of the System.ComponentModel.DataAnnotations namespace and can be applied to properties to indicate they should be ignored by EF.
[NotMapped] public int Age { get; set; }
Fluent API
Alternatively, you can employ the Fluent API to override the OnModelCreating function within your DBContext class:
protected override void OnModelCreating(DbModelBuilder modelBuilder) { modelBuilder.Entity<Customer>().Ignore(t => t.LastName); base.OnModelCreating(modelBuilder); }
Additional Notes
Asp.NET Core (2.0)
For newer versions of EF, the approach is similar:
[NotMapped] Attribute
[NotMapped] public int FullName { get; set; }
Fluent API
modelBuilder.Entity<Customer>().Ignore(t => t.FullName);
By using these techniques, you can effectively exclude properties from being mapped to the database, allowing for flexibility in your data modeling.
The above is the detailed content of How to Ignore Class Properties in Entity Framework Code First?. For more information, please follow other related articles on the PHP Chinese website!