Entity Framework core 2.0 HasColumnType throw run time exception

entity-framework-core

Question

I have a custom enum type EmployementState (complex type in EF 6 term, I think)

in OnModelCreating, the following code throw an run time exception.

modelBuilder.Entity<Employee>().Property(e => e.EmployementState.Value).HasColumnType("int");

The exception show below:

  Message=The expression 'e => e.EmployementState.Value' is not a valid property expression. The expression should represent a property access: 't => t.MyProperty'.

cannot figure out how to get the syntax right or is there are something else I was missing?

Thank you for your help.

1
1
12/6/2017 8:02:20 AM

Accepted Answer

Assuming you have the following model for your EmployementState object that will hold the different states for your employee:

public class EmployementState
{
    public int Id { get; set; }
    public string Name { get; set; }
}

You can then add a reference like:

public class Employee
{
    public int Id { get; set; }
    public string FirstName { get; set; }
    // All your user properties here

    public int EmployementStateId { get; set; }
    public virtual EmployementState EmployementState { get; set; }
}

I recommend this because is the best approach when you are working with states describing your objects.

Of course being two separate models they are configurable so configurations like this one below are easy to implement.

modelBuilder.Entity<Employee>().Property(e => e.EmployementStateId)
    .HasColumnName("employement_state_ID");
1
12/11/2017 3:37:17 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow