2

I have built a lookup table based on enums, pretty much based on that suggestion: How to create a table corresponding to enum in EF6 Code First?

Now my problem is that I am unable to configure the foreign key for MyEntity.OrderType to the OrderType table.

For more context, these are my types:

public enum OrderTypeEnum
{
    Buy = 1, Sell = 2
}

public class OrderType : EntityEnumAdapterBase<OrderTypeEnum>
{
    // this base class implements Id, Name and Description properties as can be seen here:
    // https://stackoverflow.com/questions/34557574/how-to-create-a-table-corresponding-to-enum-in-ef6-code-first/34558339#34558339
}

public partial class MyEntity
{
    [Required]
    public OrderType Type { get; set; } // stored as int using a value conversion
}

Now as the database schema for MyEntity is generated, it's lacking the foreign key constraint on MyEntity.Type to the OrdeType.Id. How can I achieve that?

What I have tried so far, was adding a navigaion property on MyEntity as such:

public class OrderType : EntityEnumAdapterBase<OrderTypeEnum>
{
    public IList<MyEntity> MyEntities { get; set; }
}

Then configure it as such in OnModelCreating(ModelBuilder modelBuilder)

modelBuilder.Entity<MyEntity>()
    .HasOne(bc => bc.Type)``
    .WithMany(c => c.MyEntities)
    .HasForeignKey(bc => bc.TypeId);

This fails with messsage: 'Type' cannot be used as a property on entity type 'MyEntity' because it is configured as a navigation.

So, how do I tackle adding FK constraints to tables generated from enums? The mentioned approach also seems a bit verbose, as I had to add MyEntity.TypeId (of type int) and OrderType.MyEntities.

Ideally on MyEntity I even wouldn't want to have OrderType, but rather OrderTypeEnum instead as such:

public partial class MyEntity
{
    [Required]
    public OrderTypeEnum Type { get; set; } // stored as int using a value conversion
}
Collin Barrett
  • 2,441
  • 5
  • 32
  • 53
the berserker
  • 1,553
  • 3
  • 22
  • 39
  • 3
    *'Type' cannot be used as a property on entity type 'MyEntity' because it is configured as a navigation* This is *not* coming from the shown fluent configuration. What concerns me is the comment *stored as int using a value conversion*, so check for fluent configuration using something like `modelBuilder.Entity().Property(e => e.Type)`. Reference property in this case represents FK navigation property and should store the Id, hence should not use value conversion. And in general the message is telling you that `Property` method cannot be used with navigation properties. – Ivan Stoev Sep 12 '18 at 12:35
  • This is the conversion and only appearance of `modelBuilder.Entity().Property(e => e.Type)`: `modelBuilder.Entity().Property(p => p.Type).HasConversion(new EnumToNumberConverter());` Do I need to get away from that? – the berserker Sep 12 '18 at 13:08
  • Indeed. I guess it was needed when the property type was `OrderTypeEnum`, but with the new design it's incorrect, causing the exception and should be removed. – Ivan Stoev Sep 12 '18 at 13:12
  • Oh, great, that actually worked, thanks @IvanStoev If you post this as an answer, I will accept it. BTW, do you know what are my chances of getting rid of OrderType.MyEntities list, that I need only in the `.WithMany(c => c.MyEntities)` part? – the berserker Sep 12 '18 at 20:03

0 Answers0