I have three entities in my (SQLite or MySQL) database called Identity, User and Portal. Identity is basically a Guid with some extra data, while portal and user are derived classes where portal adds an URL and User adds a username. Basically, a simple structure.
In my database, I want three tables with the Table-Per-Type principle. But I will have only one DbSet as I can get the others with Identity.OfType().
Now some code:
public abstract class BaseIdentityConfiguration<T> : IEntityTypeConfiguration<T> where T : Identity
{
public virtual void Configure(EntityTypeBuilder<T> builder)
{
builder
.Property(r => r.Id)
.HasColumnOrder(0)
.IsRequired()
.HasColumnName("Key")
.HasColumnType("varchar(36)")
.HasComment($"Primary key");
builder
.HasKey(r => r.Id)
.HasName("PX_ID");
}
}
public class UserEntityConfiguration : BaseIdentityConfiguration<User>
{
public override void Configure(EntityTypeBuilder<User> builder)
{
base.Configure(builder);
builder
.Property(r => r.Email)
.HasColumnName("MailAddress")
.IsRequired(true)
.HasMaxLength(128)
.IsUnicode()
.HasComment($"Email address");
builder
.HasIndex(r => new { r.Name, r.Email })
.HasDatabaseName("IDX_NAME_EMAIL")
.IsUnique();
builder
.HasIndex(r => r.Email)
.HasDatabaseName("IDX_MAIL")
.IsUnique(false);
}
}
public class PortalEntityConfiguration : BaseIdentityConfiguration<Portal>
{
public override void Configure(EntityTypeBuilder<Portal> builder)
{
base.Configure(builder);
builder
.Property(r => r.Url)
.HasColumnName("URL")
.IsRequired(true)
.HasMaxLength(256)
.IsUnicode()
.HasComment($"URL");
builder
.HasIndex(r => r.Url)
.HasDatabaseName("IDX_URL")
.IsUnique(true);
}
}
I was hoping I could inherit these configuration settings but apparently that's not possible. Unless I remove the base.Configure(builder); part and thus only configure the additional fields... But then I'm likely cause a problem as some developer will take over this project and wonders why I don't call the base class, then adds the base class call and runs into the same error...
For a second project, I only need a database with Portal records, which is a different database altogether. In that project there won't be identities but I want to use the same configuration to build a single Portal table in that project. (Not Portal and Identity but just Portal.) So, I need to know if the model has already configured the key field from Identity or not.
So, my question is simple. Can I determine in BaseIdentityConfiguration.Configure if the property for Id is already configured or not?