How to ignore a property when using Entity Framewo

2019-01-22 05:06发布

This question already has an answer here:

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?

3条回答
你好瞎i
2楼-- · 2019-01-22 05:58

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楼-- · 2019-01-22 06:00

[NotMapped] is the short version if you like conciseness. And of course, you would add:

using System.ComponentModel.DataAnnotations.Schema;

to your class.

查看更多
冷血范
4楼-- · 2019-01-22 06:02
登录 后发表回答