问题
This question already has an answer here:
- Ignoring a class property in Entity Framework 4.1 Code First 2 answers
Entity Framework Code First will auto-create a table in the database base based on the Model.
Is there an attribute that will avoid this?
回答1:
Add the [System.ComponentModel.DataAnnotations.Schema.NotMapped] attribute to the property.
回答2:
Per the accepted answer and similar question/answer, in addition to [NotMapped]
you can also specify it using the Fluent API:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<TheModelAffected>().Ignore(t => t.TheIgnoredProperty);
base.OnModelCreating(modelBuilder);
}
回答3:
[NotMapped]
is the short version if you like conciseness. And of course, you would add:
using System.ComponentModel.DataAnnotations.Schema;
to your class.
来源:https://stackoverflow.com/questions/10572442/how-to-ignore-a-property-when-using-entity-framework-code-first