Search code examples
asp.net-coredatabase-migrationentity-framework-migrations

How to pass a class to database migration?


I have a class that describes a person:

public class Person
{
    public int Id { get; set; }

    public string FirstName { get; set; }
    public string LastName { get; set; }

    public int Age { get; set; }
}

Also I have two classes that inherit this Person class and each of them has just one different property.

I'm going to create a migration and I have to set some attributes for properties in these classes, but I don't need to add Person class to the database. How can I do it?

Can I set the attributes in Person class, inherit it but pass it in migration?


Solution

  • I hope this fits your needs! If you don't want the Person class in the database, just leave it out of the DbContext. And if you find yourself needing to tweak configurations for each child class, the ideal solution is to use IEntityTypeConfiguration. I've included an example below to help you out.

    Define a base class 'Person' with common properties.

    public class Person
    {
        public int Id { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public int Age { get; set; }
    }
    

    Create a derived class 'ChildPerson1' that inherits from 'Person'. Add a specific property 'FavoriteToy' to this class.

      public class ChildPerson1 : Person
    {
        public string? FavoriteToy { get; set; }
    }
    

    Create another derived class 'ChildPerson2' that inherits from 'Person'. Add a specific property 'Surname' to this class.

      public class ChildPerson2 : Person
    {
        public string Surname { get; set; }
    }
    

    Define a database context 'PersonDbContext' that inherits from DbContext. Include DbSet properties for each derived class. Apply configurations using the 'OnModelCreating' method.

    public class PersonDbContext : DbContext
    {
       public PersonDbContext(DbContextOptions<PersonDbContext> options)
        : base(options)
       {
       }
    
       public DbSet<ChildPerson1> ChildPerson1s => Set<ChildPerson1>();
       public DbSet<ChilPerson2> ChilPerson2s => Set<ChilPerson2>();
    
       protected override void OnModelCreating(ModelBuilder modelBuilder)
       {
        
          modelBuilder
         .ApplyConfigurationsFromAssembly(typeof(PersonDbContext).Assembly);
       }
    
    }
    

    Define a configuration class 'ChildPerson1Configurations' that implements 'IEntityTypeConfiguration'. Configure specific properties for the 'ChildPerson1' entity.

     public class ChildPerson1Configurations : 
     IEntityTypeConfiguration<ChildPerson1>
     {
            public void Configure(EntityTypeBuilder<ChildPerson1> builder)
            {
                // Additional configurations specific to ChildPerson1.
                // For example, set the maximum length for the 'FavoriteToy' property.
                builder.Property(p => p.FavoriteToy).HasMaxLength(50);
        
                // Change the column name for the 'FirstName' property.
                builder.Property(p => p.FirstName).HasColumnName("First_Name");
            
    
        
           }
    }
    

    Add ChildPerson2Configurations if needed.