Using the ModelBase as base class for code-first entities

The ModelBase provides lots of functionality such as change tracking, property notifications and validation. It is possible to use this in code first. To do so, derive the entities from the ModelBase class as shown in the example below:

public partial class Person : ModelBase
    public string FirstName { get; set; }
    public string lastname { get; set; }
    public long PersonId { get; set; }
    public long FamilyId { get; set; }
    public virtual Family Family { get; set; }

In the context, add additional initialization code to ignore the Catel properties on the ModelBase class:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        .Property(e => e.FamilyName)
        .HasMany(e => e.Person)
        .WithRequired(e => e.Family)

Now the entities are ready to be used and can contain validation.  


We would like to thank the following contributors:

Want to contribute to the documentation? We have a guide for that!


Have a question about Catel or WildGums controls? Use StackOverflow with the Catel tag!