I have a TPH base class, PayCaddyMessage
, configured like below in my code-first model:
sealed class PayCaddyMessageConfig : PayCaddyEntityConfig<PayCaddyMessage>
{
public PayCaddyMessageConfig()
{
Property(e => e.Subject)
.IsRequired()
.HasMaxLength(50);
HasRequired(e => e.Sender);
HasRequired(e => e.Receiver);
}
}
I have a class derived from PayCaddyMessage
:
public class PayBetRequest: PayCaddyMessage
{
public string SenderId { get; set; }
public string ReceiverId { get; set; }
public string Description { get; set; }
public int Amount { get; set; }
public bool PaymentAccepted { get; set; }
}
}
I have deleted my old database, and am trying to start a new one using Migrations. When I issue the command Add-Migration Create -Force
, I get an exception, with the following info:
System.Data.Entity.ModelConfiguration.ModelValidationException: One or more validation errors were detected during model generation:
ReceiverId: Name: Each property name in a type must be unique. Property name 'ReceiverId' is already defined.
SenderId: Name: Each property name in a type must be unique. Property name 'SenderId' is already defined.
I can find no duplicate property names in my code. Duh, it wouldn't compile, but what I strongly suspect is that EF adds the properties SenderId
and ReceiverId
to PayBetRequest
as foreign keys, while they are not explicitly declared in that class, and then the derived class, PayBetRequest
has these properties declared. Then, in the class created for the migration, I end up with duplicate property names.
If I ommit these poperties from the derived class, they won't be available in my code, as they are not declared on the base type, and I don't know how to declare them as foreign keys on the base type, so I have omitted them there, and HasRequired(e => e.Sender);
automatically generates a SenderId
in the migration.