ASP.net MVC - 属性未映射到数据库

pin*_*ker -1 c# asp.net asp.net-mvc

是否可以在模型中具有未映射到数据库中的列的属性?

我正在使用ASP.net Entity Framework和MVC 5.

Dav*_*idG 12

您可以使用以下NotMapped属性:

public class SomeModel
{
    public int MappedProperty { get; set; }

    [NotMapped]
    public int UnmappedProperty { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

或者使用流畅的API:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
   modelBuilder.Entity<SomeModel>().Ignore(m => m.UnmappedProperty );
   base.OnModelCreating(modelBuilder);
}
Run Code Online (Sandbox Code Playgroud)