简体   繁体   English

如何告诉实体框架忽略数据库生成用于插入或更新的值?

[英]how to tell Entity Framework to ignore database generates values for insert or update?

I'm new to Entity Framework in C# . 我是C# Entity Framework新手。

I'm having one computational column in my table table1 (example). 我的表table1有一个计算列(示例)。 I will calculate the computational column value based on the other columns value. 我将基于其他列值来计算计算列值。 I don't want Entity Framework to include the computational column while performing insert/update operations. 我不希望Entity Framework在执行插入/更新操作时包括计算列。 How can I make the EF to avoid that particular column. 如何使EF避免出现该特定列。 But I want to set the value for that particular column manually. 但是我想手动设置该特定列的值。

I searched for the same but i couldn't able to get the answer for my question. 我搜索了相同的内容,但无法获得我的问题的答案。 Kindly help me and thanks in advance. 请帮助我,并在此先感谢。

You can use the NotMapped Annotation 您可以使用NotMapped注释

Code first convention dictates that every property that is of a supported data type is represented in the database. 代码优先约定规定,数据库中表示具有受支持数据类型的每个属性。 That property can be created dynamically and does not need to be stored. 该属性可以动态创建,不需要存储。 You can mark any properties that do not map to the database with the NotMapped annotation. 您可以使用NotMapped批注标记任何未映射到数据库的属性。

[NotMapped]
public string Something
{
    get
    {
        return _something;
    }
    set
    {
        _something = value
    }
}

Update : this is will not map to the dB, so is probably not what you are looking for 更新 :这不会映射到dB,所以可能不是您想要的


Just to make this a more complete the DatabaseGenerated Annotation, are the droids you are looking for 只是为了使它变得更完整, DatabaseGenerated Annotation是您要寻找的机器人

An important database features is the ability to have computed properties. 数据库的一项重要功能是具有计算属性的能力。 If you're mapping your Code First classes to tables that contain computed columns, you don't want Entity Framework to try to update those columns. 如果要将Code First类映射到包含计算列的表,则不希望Entity Framework尝试更新这些列。 But you do want EF to return those values from the database after you've inserted or updated data. 但是,您确实希望EF在插入或更新数据之后从数据库返回这些值。 You can use the DatabaseGenerated annotation to flag those properties in your class along with the Computed enum. 您可以使用DatabaseGenerated批注来标记类中的那些属性以及Computed枚举。 Other enums are None and Identity. 其他枚举是“无”和“身份”。

Which can be used with the DatabaseGeneratedOption 可以与DatabaseGeneratedOption一起使用

  • Computed : The database generates a value when a row is inserted or updated. 计算 :插入或更新行时,数据库会生成一个值。

  • Identity : The database generates a value when a row is inserted. 身份 :插入行时,数据库会生成一个值。

  • None : The database does not generate values. :数据库不会生成值。

[DatabaseGenerated(DatabaseGenerationOption.Computed)]
public string Something { get; set; }

As you calculate your column server side the correct way is to configure the field at the context level with: 在计算列服务器端时,正确的方法是使用以下命令在上下文级别配置字段:

If you use NotMapped , you will not get the value from the database. 如果使用NotMapped则不会从数据库中获取值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM