How to ignore a property when using Entity Framework Code First [duplicate]

送分小仙女□ 提交于 2019-11-27 11:52:08

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!