Entity Framework Code First - How to ignore a column when saving

折月煮酒 提交于 2019-12-05 10:56:53

You can use DatabaseGeneratedAttribute with DatabaseGeneratedOption.Computed option:

[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public ComputedPropertyType ComputedProperty { get; set; }

or if you prefer fluent api you can use HasDatabaseGeneratedOption method in your DbContext class:

public class EntitiesContext : DbContext
{
    public DbSet<EntityType> Enities { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<EntityType>().Property(e => e.ComputedProperty).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
    }
}

Mark property as computed:

modelBuilder
    .Entity<MyEntityType>()
    .Property(_ => _.MyProperty)
    .HasDatabaseGeneratedOption(DatabaseGeneratedOption.Computed);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!