Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Seed Object with Shadow Properties

I'm trying to seed an object that uses shadow properties using Entity Framework core 3.1 but when I try to create a migration I get:

The seed entity for entity type 'MyObject' cannot be added because there was no value provided for the required property 'Created'.

The property "Created" is a shadow property added in OnModelCreating so I don't have access to it in the model configuration. The Microsoft article Data Seeding says, "If the entity type has any properties in shadow state an anonymous class can be used to provide the values...." I tried that as well and got the same error.

Here is my configuration:

protected override void OnModelCreating( ModelBuilder modelBuilder )
{
    // Tried this before and after the shadow properties, same error
    modelBuilder.ApplyConfiguration( new MyObjectConfiguration() );

    // Tried to seed here too instead of in the MyObjectConfiguration
    //modelBuilder.Entity<Brokerage>().HasData( new
                            //{
                                //Id = 1,
                                //Name = "A Really Cool Object",
                                //PhoneNumber = "1234567891"
                            //} );

    // Create shadow properties
    foreach ( var entityType in modelBuilder.Model.GetEntityTypes()
          .Where( e => typeof( IAuditable ).IsAssignableFrom( e.ClrType ) ) )
    {
        modelBuilder.Entity( entityType.ClrType )
            .Property<DateTime>( "Created" );

        modelBuilder.Entity( entityType.ClrType )
            .Property<DateTime>( "Modified" );

        modelBuilder.Entity( entityType.ClrType )
            .Property<string>( "CreatedBy" );

        modelBuilder.Entity( entityType.ClrType )
            .Property<string>( "ModifiedBy" );
    }

    base.OnModelCreating( modelBuilder );
}


public class BrokerageConfiguration : IEntityTypeConfiguration<MyObject>
{
    public void Configure( EntityTypeBuilder<MyObject> builder )
    {
        ...

        //builder.Entity<MyObject>()
        //    .HasData( new { Id = 1, Name = "A Really Cool Object", PhoneNumber = "1234567891" } );

        builder.HasData
        (
            new MyObject
            {
                Id = 1,
                Name = "A Really Cool Object",
                PhoneNumber = "1234567891"
            }
        );
    }
}

How do I seed data that uses shadow properties?

like image 902
mack Avatar asked Jan 19 '26 15:01

mack


1 Answers

For the sake of completeness (as I was looking for a solution myself), using an anon class would be the solution as commented above:

modelBuilder.Entity<Brokerage>().HasData(new
{
    Id = 1,
    Name = "A Really Cool Object",
    PhoneNumber = "1234567891",
    CreatedBy = "sysdba",
    Created = DateTime.Now,
    ModifiedBy = "sysdba",
    Modified = DateTime.Now
});
like image 184
Mr.Fantastic Avatar answered Jan 22 '26 10:01

Mr.Fantastic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!