I’m attempting to use TPH mapping for the following classes. Ignore that there’s only one derived class for now, the real code has many derivations.
public abstract class Account
{
public virtual int Id { get; set; }
}
public class UserAccount : Account
{
public virtual string Username { get; set; }
public virtual string Password { get; set; }
public virtual string PasswordSalt { get; set; }
public virtual bool Verified { get; set; }
}
Mappings…
public class AccountMapping : EntityTypeConfiguration<Account>
{
public AccountMapping()
{
this.ToTable("Account");
}
}
public class UserAccountMapping : EntityTypeConfiguration<UserAccount>
{
public UserAccountMapping()
{
this.Property(x => x.Username).HasMaxLength(50);
this.Property(x => x.Password).HasMaxLength(68);
this.Property(x => x.PasswordSalt).HasMaxLength(24);
this.Map(x => x.Requires("AccountTypeId").HasValue((int)AccountType.User));
}
}
I get the following error when attempting to call Add-Migration…
Map was called more than once for type ‘UserAccount’ and at least one of the calls
didn’t specify the target table name.
Changing the UserAccountMapping to…
public class UserAccountMapping : EntityTypeConfiguration<UserAccount>
{
public UserAccountMapping()
{
this.Property(x => x.Username).HasMaxLength(50);
this.Property(x => x.Password).HasMaxLength(68);
this.Property(x => x.PasswordSalt).HasMaxLength(24);
this.Map(x =>
{
x.ToTable("Account");
x.Requires("AccountTypeId").HasValue((int)AccountType.User));
});
}
}
seems to fix that problem but then I get errors about properties being mapped more than once:
Properties for type ‘UserAccount’ can only be mapped once. The non-key property ‘Username’ is mapped more than once. Ensure the Properties method specifies each non-key property only once.
What am I doing wrong?
This was down to some weird mapping configuration in an existing project. I can’t pinpoint exactly what the issue was but it’s working now. Should have tested in a clean project before posting!