I'm migrating a substantial EF model of ~80 entites from EF4 to EF6, and I'm also changing it from a Designer EDMX-generated database, to a Code First database.
Right now I'm configuring the entity relationships using EF fluent-api, and I'm not certain I'm doing it correctly.
It's type in the SQL Server database is varchar(50)
, so should I be configuring it like this?
mb.Entity<SomeObject>()
.Property(so => so.Type)
.IsUnicode(false)
.HasColumnName("Type")
.HasColumnType("varchar")
.HasMaxLength(50)
.IsRequired()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
or like this, without the HasMaxLength(50)
?
mb.Entity<SomeObject>()
.Property(crt => crt.Type)
.IsUnicode(false)
.HasColumnName("Type")
.HasColumnType("varchar(50)")
.IsRequired()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
Additionally, say I have another object with a GUID ID:
mb.Entity<AnotherObject>()
.Property(ao => ao.ID)
.HasColumnName("ID")
.HasColumnType("uniqueidentifier")
.IsRequired()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
In the database it has a default of newsequentialid()
, should I be configuring it with DatabaseGeneratedOption.None
, DatabaseGeneratedOption.Identity
, or DatabaseGeneratedOption.Computed
?
What is the difference between those options? Additionally, in the code GUIDs are mostly being assigned at object instantiation, like so:
Guid ID = new Guid.NewGuid()
Is that appropriate?
varchar(50) is not a column type itself, 'varchar' is a data type while (50) is the maximum length of characters in a string. you have to 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 dont give GUID, it will be set to default value of GUID in the settings of the database, if you want to set it, use the GUID generator class.