Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the proper usage of HasColumnType and Database generated

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?

like image 370
kpk47 Avatar asked Jun 14 '14 21:06

kpk47


People also ask

When should I use Fluent API?

Entity Framework Fluent API is used to configure domain classes to override conventions. EF Fluent API is based on a Fluent API design pattern (a.k.a Fluent Interface) where the result is formulated by method chaining. In Entity Framework Core, the ModelBuilder class acts as a Fluent API.

What is Fluent API in and how is it different from data annotations?

Fluent API is an advanced way of specifying model configuration that covers everything that data annotations can do in addition to some more advanced configuration not possible with data annotations.

Which method is used to apply configuration to entities or their properties in the Entity Framework Core?

Property Mapping. The Property method is used to configure attributes for each property belonging to an entity or complex type.

What is OnModelCreating in Entity Framework?

The DbContext class has a method called OnModelCreating that takes an instance of ModelBuilder as a parameter. This method is called by the framework when your context is first created to build the model and its mappings in memory.


1 Answers

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.

like image 51
Aunn Raza Avatar answered Oct 25 '22 10:10

Aunn Raza