I have a base class, like this for my POCO objects
public abstract class BASE
{
protected BASE()
{
Created = DateTime.UtcNow;
Modified = Created;
}
public int id { get; set; }
public DateTime Created { get; set; }
public DateTime Modified { get; set; }
[MaxLength(50)]
public string CreatedBy { get; set; }
[MaxLength(50)]
public string ModifiedBy { get; set; }
}
What I want to do is set the “MaxLength(50)” attributes with fluent API.
But if I do this in the Context.ModelCreating
modelBuilder.Entity<BASE>().Property(p => p.CreatedBy).HasMaxLength(50);
modelBuilder.Entity<BASE>().Property(p => p.ModifiedBy).HasMaxLength(50);
then “BASEs” table get generated in the database – which I want to avoid.
What am I missing, to be able to set these constraints in Fluent API ?
You can use
EntityTypeConfiguration<TEntity>to configure the max length.Create a base class to map the
BASEproperties.Then create
MyEntityMap<TEntity>for each derived class.