实体框架代码优先-保存时如何忽略列 [英] Entity Framework Code First - How to ignore a column when saving
问题描述
我首先使用实体框架代码将一个名为Client的类映射到数据库表.该表具有一个我需要在Client类中提供的计算字段,但我知道将无法写入该字段.有没有一种方法可以配置Entity Framework在保存时忽略该属性,但在读取时包括该属性?
I have a class called Client mapped to a database table using Entity Framework code first. The table has a computed field that I need available in my Client class, but I understand that it won't be possible to write to this field. Is there a way of configuring Entity Framework to ignore the property when saving, but include the property when reading?
我尝试在配置类中使用Ignore方法,或使用[NotMapped]属性,但是这些方法阻止从数据库读取该属性.
I have tried using the Ignore method in my configuration class, or using the [NotMapped] attribute, but these prevent the property from being read from the database.
推荐答案
您可以使用或者,如果您更喜欢流利的api,则可以使用DbContext类中的> HasDatabaseGeneratedOption
方法:
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);
}
}
这篇关于实体框架代码优先-保存时如何忽略列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!