What is the proper use of the generated HasColumnType and database

I am transferring the substantial EF model ~ 80 entites from EF4 to EF6, and I am also changing it from the database created by Designer EDMX to the Code First database.

Right now I'm setting up entity relationships using EF fluent-api, and I'm not sure if I am doing this correctly.

This type is in the varchar(50) SQL Server database, so what should I configure it like that?

  mb.Entity<SomeObject>() .Property(so => so.Type) .IsUnicode(false) .HasColumnName("Type") .HasColumnType("varchar") .HasMaxLength(50) .IsRequired() .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 

or how is it without HasMaxLength(50) ?

  mb.Entity<SomeObject>() .Property(crt => crt.Type) .IsUnicode(false) .HasColumnName("Type") .HasColumnType("varchar(50)") .IsRequired() .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 

Also, let's say I have another object with a GUID:

  mb.Entity<AnotherObject>() .Property(ao => ao.ID) .HasColumnName("ID") .HasColumnType("uniqueidentifier") .IsRequired() .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 

In the database, it has a default value of newsequentialid() , should I configure it using DatabaseGeneratedOption.None , DatabaseGeneratedOption.Identity or DatabaseGeneratedOption.Computed ?

What is the difference between these options? In addition, in the code, in most cases, GUIDs are assigned when creating an object, for example:

 Guid ID = new Guid.NewGuid() 

Respectively?

+6
source share
2 answers

varchar (50) is not the type of column itself, "varchar" is the data type, and (50) is the maximum length of characters in a string. you should do it like this:

 mb.Entity<SomeObject>() .Property(so => so.Type) .IsUnicode(false) .HasColumnName("Type") .HasColumnType("varchar") .HasMaxLength(50) .IsRequired() .HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 

for your second question, if you do not specify a GUID, it will be set to the default GUID in the database settings, if you want to set it, use the GUID generator class.

+10
source

I am not an expert on how these things are implemented internally, but I cannot tell you what I know from their use.

As for your first question using the HasColumnType method, for example:

 HasColumnType("varchar(50)") 

Entity Framework does not recognize the type "varchar (50)", but it recognizes the type "varchar". The only correct use that works with ef (verified in version 6):

 .HasColumnType("varchar") .HasMaxLength(50) 

Regarding the second question - DatabaseGeneratedOption. From my experience for EF, there is no difference between DatabaseGeneratedOption.Computed and DatabaseGeneratedOption.None - it treats them the same way. The difference is made only for DatabaseGeneratedOption.Identity - in this case, the column is defined as the identifier column, and the value passed to it when inserting data into db from the application will be ignored, instead it will be assigned by the database. If you define your column as DatabaseGeneratedOption.None or DatabaseGeneratedOption.Computed, you need to specify a value for the ID column, as shown:

 Guid ID = new Guid.NewGuid() 

Otherwise, it will always try to set the default value for the GUID (all digits are set to zero). If you specify your column as DatabaseGeneratedOption.Identity, you do not need to worry about assigning a value to your identifier before storing it in db. Instead, it sets the default dbms value (which in your case is "newsequentialid ()"). So for you there is a DatabaseGeneratedOption.Identity option.

+2
source

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


All Articles