Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am trying to figure out what the

[DatabaseGenerated(DatabaseGenerationOption.Computed)]

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

like image 904
KallDrexx Avatar asked Feb 20 '11 21:02

KallDrexx


People also ask

What is DatabaseGenerated?

The DatabaseGenerated attribute specifies how values are generated for a property by the database. The attribute takes a DatabaseGeneratedOption enumeration value, which can be one of three values: Computed. Identity. None.

What is DatabaseGeneratedOption identity?

An identity column in a database (and DatabaseGeneratedOption. Identity ) denote that the column generates the value automatically upon insert. These are typically int columns, and similar, that auto-increment. An INSERT for a row that uses an identity column omit a value from the query (generated by the database)

What is data annotations in Entity Framework?

Advertisements. DataAnnotations is used to configure the classes which will highlight the most commonly needed configurations. DataAnnotations are also understood by a number of .

How can we specify computed generated column in Entity Framework?

The Entity Framework Core Fluent API HasComputedColumnSql method is used to specify that the property should map to a computed column. The method takes a string indicating the expression used to generate the default value for a database column.


1 Answers

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.

like image 137
John Gietzen Avatar answered Oct 04 '22 00:10

John Gietzen