1

I am trying to seed data into my database but I keep getting the error

 System.ArgumentNullException: Value cannot be null.
 Parameter name: set
 at System.Data.Entity.Utilities.Check.NotNull[T](T value, String parameterName)
at System.Data.Entity.Migrations.DbSetMigrationsExtensions.AddOrUpdate[TEntity](IDbSet`1 set, TEntity[] entities)
at kesws.Migrations.Configuration.Seed(kesws_context context) in C:\Users\JNyingi\source\repos\kesws\Migrations\Configuration.cs:line 83
at System.Data.Entity.Migrations.DbMigrationsConfiguration`1.OnSeed(DbContext context)
at System.Data.Entity.Migrations.DbMigrator.SeedDatabase()
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.SeedDatabase()
at System.Data.Entity.Migrations.DbMigrator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.Infrastructure.MigratorLoggingDecorator.Upgrade(IEnumerable`1 pendingMigrations, String targetMigrationId, String lastMigrationId)
at System.Data.Entity.Migrations.DbMigrator.UpdateInternal(String targetMigration)
at System.Data.Entity.Migrations.DbMigrator.<>c__DisplayClassc.<Update>b__b()
at System.Data.Entity.Migrations.DbMigrator.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.EnsureDatabaseExists(Action mustSucceedToKeepDatabase)
at System.Data.Entity.Migrations.DbMigrator.Update(String targetMigration)
at System.Data.Entity.Migrations.Infrastructure.MigratorBase.Update(String targetMigration)
at System.Data.Entity.Migrations.Design.ToolingFacade.UpdateRunner.Run()
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.AppDomain.DoCallBack(CrossAppDomainDelegate callBackDelegate)
at System.Data.Entity.Migrations.Design.ToolingFacade.Run(BaseRunner runner)
at System.Data.Entity.Migrations.Design.ToolingFacade.Update(String targetMigration, Boolean force)
at System.Data.Entity.Migrations.UpdateDatabaseCommand.<>c__DisplayClass2.<.ctor>b__0()
at System.Data.Entity.Migrations.MigrationsDomainCommand.Execute(Action command)
Value cannot be null.
Parameter name: set

Here is the class am trying to seed

public class MSG_Header
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public Guid Id { get; set; }

    public string MSGID { get; set; }

    public string RefNo { get; set; }

    public Int16 MSG_FUNC { get; set; }

    public string Sender { get; set; }

    public string Receiver { get; set; }

    public string Version_No { get; set; }

    public string Doc_No { get; set; }

    public string Doc_Group { get; set; }

    public DateTime MSG_Date { get; set; }

    public string Token { get; set; }

}

Here is my seed Initializer

   var headers = new MSG_Header
        {
            MSGID = "u64j515g5f5",
            RefNo = "f9b5f6b5fb6f52b",
            MSG_FUNC = 56,
            Sender = "Austin Evans",
            Receiver = "Linus Tech",
            Version_No = "h26h",
            Doc_No = "g6h1j6h1h6n1h",
            Doc_Group = "sssa",
            MSG_Date = DateTime.Now.AddMonths(-1),
            Token = "wtr6t2r6r2662j64yj5jmjju8i"
        };
        context.MSG_Header.AddOrUpdate(headers);
        context.SaveChanges();

Finally my context

  public class kesws_context: DbContext
{
    public kesws_context(): base("kesws")
    {

    }

    public DbSet<MSG_Header> MSG_Header { get; }


    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();

        modelBuilder.Entity<MSG_Header>()
            .HasKey(x => x.Id);
    }
}

I expect the data to be seeded with no issues. It creates the table correctly, but fails to seed the data. I am using EF Version 6.1.3. I would like to be assisted with this issue cause am stuck

5
  • Id seem to be null? AddOrUpdate Adds or updates entities by key.. Commented Mar 26, 2019 at 7:21
  • Still throws the same error even if i set the ID Commented Mar 26, 2019 at 7:24
  • Try setting DbSet as writable property: public DbSet<MSG_Header> MSG_Header { get; set;} Commented Mar 26, 2019 at 7:30
  • 1
    Now this was what I was missing, thanks Commented Mar 26, 2019 at 7:31
  • 1
    Good, I'll make an answer on that since this is not obvious and is hard to spot.. :) Commented Mar 26, 2019 at 7:33

1 Answer 1

3

You are missing the setter for the MSG_Header property in DbContext, it should look like this;

public class kesws_context: DbContext
{

    public DbSet<MSG_Header> MSG_Header { get; set; }
    {...}
}
Sign up to request clarification or add additional context in comments.

2 Comments

provide an example of implementation
Edited my answer based on question comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.