I'm working with Entity Framework 6.0.2 Code First with Sql Server.
I have a base class called Entity and since we can't extend enums I need to redefine a property type for another class called Company, so I'm using the new keyword to hide the base property and redefine it.
public interface IEntity
{
Guid Id { get; set; }
State State { get; set; }
}
public abstract class Entity : IEntity
{
public Guid Id { get; set; }
public State State { get; set; }
}
public enum State
{
Inactive = 0,
Active = 1
}
public class Company : Entity
{
public new CompanyState State { get; set; }
public string SomeOtherProp { get; set; }
}
public enum CompanyState
{
Inactive = 0,
Active = 1,
Extra = 2
}
The problem I get is when Entity Framework is trying to create the DbContext it crashes with this error: "The item with identity 'State' already exists in the metadata collection. Parameter name: item"
I have a workaround: I could change the State propery in Entity class to int and cast the appropriate enum to int, but I think I'll lose type safety / restriction that the enums have.
I'd like to change the metadata info to avoid this error but I don't know how.
This guy here found a solution for some similar problem.
Neither your, nor his solution is nice. It is and remains a hack.
I would go with the solution you already mentioned. Change the state to stateId. And add a State Property to your Entity:
public State State {get {return (State)stateId;}
In your Company override this Property with new:
public new CompanyState State {get {return (CompanyState )stateId;}
But I think best solution would be, to change your inheritance hierarchy. I think either your IEntity should not have a state, or your company should not inherit from Entity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With