Base solution for your next web application
Open Closed

EF Core not seeding my data #3708


User avatar
0
bilalhaidar created

Hello, I added a migration, then requested to update the database. Tables were created successfully, but the seed data was not executed.

Then I noticed that the tables were created but missing my columns (i.e. properties).

In my case I have around 10 classes that share the same structure. Each class represents an entity that is used as reference data.

So I created a base class having those 2 properties and made those classes inheirt from that class. Something as this:

[Serializable]
    public class HostEntityBase<TPrimaryKey> : FullAuditedEntity<TPrimaryKey>
    {
        [Required]
        [StringLength(HostEntityConsts.MaxLabelLength)]
        public string Label { get; private set; }

        [StringLength(HostEntityConsts.MaxLabelDescriptionLength)]
        public virtual string Notes { get; private set; }

        public void UpdateLabel(string newLabel)
        {
            //Validate newLabel
            Check.NotNull(newLabel, nameof(newLabel));

            Label = newLabel;
        }

        public virtual void UpdateNotes(string newNotes)
        {
            Notes = newNotes;
        }

        public HostEntityBase()
        {
        }

        public HostEntityBase(string label, string notes)
        {
            UpdateLabel(label);
            UpdateNotes(notes);
        }
    }

Then each class inherits from this base and calls its constructor.

In the previous version (MVC 5/Angular js) it was working fine. But now with EF Core seems not to work. No columns were added on the tables?

Also, when I ran the app, the Seed ran. Where is that configured to do so? Usually, with update-database the Seed would run.

Thanks


3 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @bilalhaidar,

    EF Core does not have seed functon yet. I don't know if 2.0 has it or not because it is just released and I couldn't follow it. So, we moved seed method to application startup.

    I don't have any clue why your migration didn't work. Have you checked EF Core's github repository for that ?

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    I did some clean up for the migrations. I noticed something that when I run the application the data is seeded, is that by design?

  • User Avatar
    0
    alper created
    Support Team

    yes