Bạn có thể sử dụng NotMappedchú thích dữ liệu thuộc tính để hướng dẫn Code-First để loại trừ một thuộc tính cụ thể
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]thuộc tính được bao gồm trong System.ComponentModel.DataAnnotationskhông gian tên.
Bạn có thể thay thế làm điều này với chức năng ghi Fluent APIđè OnModelCreatingtrong DBContextlớp của bạn :
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Customer>().Ignore(t => t.LastName);
base.OnModelCreating(modelBuilder);
}
http://msdn.microsoft.com/en-us/l Library / hh295847 (v = vs.103) .aspx
Phiên bản tôi đã kiểm tra là EF 4.3phiên bản ổn định mới nhất có sẵn khi bạn sử dụng NuGet.
Chỉnh sửa : SEP 2017
Lõi Asp.NET (2.0)
Chú thích dữ liệu
Nếu bạn đang sử dụng lõi asp.net ( 2.0 tại thời điểm viết bài này ), [NotMapped]thuộc tính có thể được sử dụng ở cấp độ thuộc tính.
public class Customer
{
public int Id { set; get; }
public string FirstName { set; get; }
public string LastName { set; get; }
[NotMapped]
public int FullName { set; get; }
}
API thông thạo
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; }
}