开发者

What does the [DatabaseGenerated(DatabaseGenerationOption.Computed)] data annotation do?

开发者 https://www.devze.com 2023-02-12 09:42 出处:网络
I am trying to figure out w开发者_运维问答hat the [DatabaseGenerated(DatabaseGenerationOption.Computed)]

I am trying to figure out w开发者_运维问答hat the

[DatabaseGenerated(DatabaseGenerationOption.Computed)]

DataAnnotation actually does. However, I cannot find any information via google searches or MSDN searches. Does anyone have any idea?


A computed column is a column on a table that is not updatable, but is instead based on other data in the row.

It is a similar concept to a View, but is more light-weight, and can be PERSISTED without having to build an indexed view.

For example, you could have a computed column to add together two numbers like this (in T-SQL):

CREATE TABLE [Foo]
(
    [FooId] int NOT NULL IDENTITY,
    CONSTRAINT [Foo_PK] PRIMARY KEY ([FooId]),

    [ItemA] int,
    [ItemB] int,
    [Sum] AS ([ItemA] + [ItemB])
)

Entity Framework needs to know about these columns so that it doesn't try to issue an Update statement that would try to change the value of that column.


That attribute is used to in code-first Entity Framework whereby it specifies how the value is generated. DatabaseGenerationOptions is an enum of Identity, Computed, None, where Identity maps to an identity column, Computed is a computed column.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号