I've got an abstract class where a property "CreatedBy" is required. This is used for ALMOST every entities but one (UserProfile itself)
How can I remove the Required attribute from the UserProfile without removing it from the other entities inheriting from EntityBase?
public abstract class EntityBase: IEntityBase
{
[Key, Column(Order = 0)]
public Guid? Id { get; set; }
[Key, Column(Order = 1)]
public int Version { get; set; }
[Required]
public DateTime Created { get; set; }
[Required]
public UserProfile CreatedBy { get; set; }
public bool IsDeleted { get; set; }
}
public class UserProfile: EntityBase
{
[Required, Index("Username", 3, IsUnique = true), MaxLength(900)]
public string Username { get; set; }
[Required, Index("Email", 4, IsUnique = true), MaxLength(900)]
public string Email { get; set; }
}
I tried overriding my OnModelCreating, but that doesn't work... Anybody any ideas?
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<UserProfile>().HasOptional(profile => profile.CreatedBy).WithMany();
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
Strange thing is, in my database, the columns CreatedBy_Id and CreatedBy_Version can be null in the UserProfile table.
When I seed my UserProfile table, I get a validation error for each of them saying: "The CreatedBy field is required."