Is there a way to override nvarchar (max) with a given maxlength using the EF 4.2 codefirst configuration?

By default, EF 4.2 codefirst sets the database column for string properties to nvarchar (max).

Individually (per property) I can override this convention by specifying the [MaxLength(512)] attribute.

Is there a way to globally apply a configuration that does this? It seems that the api configuration on modelbuilder allows only for redefinition of the entity and that the api convention on modelbuilder only allows deleting. See this question.

+4
source share
2 answers

No global configuration available. User agreements have been removed from EF Code First in the CTP phase.

+3
source

I think you can do it. Just above your property in the model table, add the StringLength attribute. Like after. (Remember that you will need to enable the use of System.ComponentModel.DataAnnotations;)

  [StringLength(160)] public string Title { get; set; } 

Update

  • First of all, you cannot create an index in the nvarchar (MAX) column. You can use full-text indexing, but you cannot create an index in a column to improve query performance.

  • From the perspective of storage, there is no difference between nvarchar (max) and nvarchar (N) when N <4000. Data is stored in a row or on line overflow pages when they are not suitable. When you use nvarchar (max) and store more than 4000 characters (8000 bytes), SQL Server uses a different method for storing data - similar to the old TEXT data types - it is stored on LOB pages.

  • Performance - again, for N <4000 and (max) - there is no difference. Well, technically this affects row size estimation and may introduce some problems - you can read more about this: The best way for sortable column widths

  • What can affect system performance is line size. If you have queries on the SCAN table, a large row size will lead to more data pages in the table -> more io operations -> performance degradation. If so, you can try to perform vertical splitting and move the nvarchar field to another table.

+2
source

Source: https://habr.com/ru/post/1381405/


All Articles