New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Seeding data: The seed entity for entity type 'X' cannot be added because there was no value provided for the required property 'Id'. #11776

Closed
Sigvaard opened this Issue Apr 23, 2018 · 9 comments

Comments

Projects
None yet
4 participants
@Sigvaard

Sigvaard commented Apr 23, 2018

I'm using EntityFrameworkCore 2.1.0-preview2-final

I try to seed data, this works:

modelBuilder.Entity<Dog>().HasData(new Dog[]
{
    new Dog() { Id = 1, Name = "Lucy", Age = 2 },
    new Dog() { Id = 2, Name = "Bella", Age = 3 },
    new Dog() { Id = 3, Name = "Daisy", Age = 5 },
});

this doesn't:

modelBuilder.Entity<Dog>().HasData(new Dog[]
{
    new Dog() { Name = "Lucy", Age = 2 },
    new Dog() { Name = "Bella", Age = 3 },
    new Dog() { Name = "Daisy", Age = 5 },
});

And I get:

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

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

public class Dog : BaseEntity
{
    public string Name { get; set; }
    public int Age { get; set; }
}

I expected it to work even when skipping Id. Is it auto increment by default?

I wanted to get it working with Bogus:

var dogsIds = 0;
var testDogs = new Faker<Dog>().RuleFor(p => p.Name, f => f.Name.FirstName())
    .RuleFor(p => p.Age, f => DateTime.Now.Year - f.Person.DateOfBirth.Year)
    .RuleFor(p => p.Id, f => dogsIds++);

var dogs = testDogs.Generate(100);

modelBuilder.Entity<Dog>().HasData(dogs.ToArray());

@Sigvaard Sigvaard changed the title from The seed entity for entity type 'X' cannot be added because there was no value provided for the required property 'Id'. to Seeding data: The seed entity for entity type 'X' cannot be added because there was no value provided for the required property 'Id'. Apr 23, 2018

@smitpatel

This comment has been minimized.

Contributor

smitpatel commented Apr 23, 2018

For data in HasData method, you need to provide values for auto-generated key properties.

@Sigvaard

This comment has been minimized.

Sigvaard commented Apr 23, 2018

@smitpatel I did exactly that, using Bogus but it doesn't seem to work with HasData. I had to use the workaround described in aspnet/AspNetCore#2188

I wanted to generate some random data to test my API with.

@smitpatel

This comment has been minimized.

Contributor

smitpatel commented Apr 23, 2018

@enemyofthedawn - Make sure you generate some random, unique values for key column too. 😀

@ajcvickers

This comment has been minimized.

Member

ajcvickers commented Apr 23, 2018

@enemyofthedawn Just to elaborate on why store-generated values are not supported here. The idea of having data in the model is that when the model is evolved, the seed data in the database is evolved along with it. But for that to work, each entity in the model needs to have a well-known key value such that it can be found and updated later. Feel free to use more traditional seeding mechanisms for, for example, tests that just need to initialize some data into an empty database.

@Sigvaard

This comment has been minimized.

Sigvaard commented Apr 24, 2018

@ajcvickers I'm not sure if I understood. Do you mean that generated objects like above won't work with HasData?

@ajcvickers

This comment has been minimized.

Member

ajcvickers commented Apr 24, 2018

@enemyofthedawn No, I'm just saying that the key values must be specified.

@Sigvaard

This comment has been minimized.

Sigvaard commented Apr 24, 2018

It worked with Bogus, I realized I had to start incrementing from 1, and Bogus did it from 0.

@ajcvickers It seems like HasData should be used more to fill database with some production data rather than test data. Right?

It seems like generating random data using HasData will modify/add data in each migration I add unless I add some check if the data exists before.

protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);

            int i = 1;
            var faker = new Faker<Order>()
                .RuleFor(o => o.Name, f => f.Commerce.ProductName())
                .RuleFor(o => o.Id, f => i++);

            var orders = faker.Generate(10);

            modelBuilder.Entity<Order>().HasData(orders.ToArray());
        }

first migration:

 migrationBuilder.InsertData(
                table: "Orders",
                columns: new[] { "Id", "Name" },
                values: new object[,]
                {
                    { 1, "Licensed Cotton Shirt" },
                    { 2, "Rustic Concrete Pizza" },
                    { 3, "Handcrafted Metal Keyboard" },
                    { 4, "Licensed Wooden Chicken" },
                    { 5, "Awesome Metal Pizza" },
                    { 6, "Practical Metal Chair" },
                    { 7, "Handmade Steel Gloves" },
                    { 8, "Refined Soft Chair" },
                    { 9, "Sleek Plastic Salad" },
                    { 10, "Ergonomic Steel Mouse" }
                });

2nd migration:

migrationBuilder.UpdateData(
                table: "Orders",
                keyColumn: "Id",
                keyValue: 1,
                column: "Name",
                value: "Small Steel Chips");

            migrationBuilder.UpdateData(
                table: "Orders",
                keyColumn: "Id",
                keyValue: 2,
                column: "Name",
                value: "Incredible Fresh Sausages");

            migrationBuilder.UpdateData(
                table: "Orders",
                keyColumn: "Id",
                keyValue: 3,
                column: "Name",
                value: "Awesome Plastic Fish");

            migrationBuilder.UpdateData(
                table: "Orders",
                keyColumn: "Id",
                keyValue: 4,
                column: "Name",
                value: "Awesome Metal Sausages");

            migrationBuilder.UpdateData(
                table: "Orders",
                keyColumn: "Id",
                keyValue: 5,
                column: "Name",
                value: "Incredible Soft Sausages");

            migrationBuilder.UpdateData(
                table: "Orders",
                keyColumn: "Id",
                keyValue: 6,
                column: "Name",
                value: "Refined Granite Chair");

            migrationBuilder.UpdateData(
                table: "Orders",
                keyColumn: "Id",
                keyValue: 7,
                column: "Name",
                value: "Generic Granite Tuna");

            migrationBuilder.UpdateData(
                table: "Orders",
                keyColumn: "Id",
                keyValue: 8,
                column: "Name",
                value: "Generic Wooden Fish");

            migrationBuilder.UpdateData(
                table: "Orders",
                keyColumn: "Id",
                keyValue: 9,
                column: "Name",
                value: "Sleek Soft Towels");

            migrationBuilder.UpdateData(
                table: "Orders",
                keyColumn: "Id",
                keyValue: 10,
                column: "Name",
                value: "Unbranded Wooden Pizza");
@TheBauwssss

This comment has been minimized.

TheBauwssss commented Jun 16, 2018

Can the error message

System.InvalidOperationException: The seed entity for entity type '' cannot be added because there was no value provided for the required property 'Id'.

be changed to

System.InvalidOperationException: The seed entity for entity type '' cannot be added because there was no value provided for the required property 'Id'. Please note that 0 is not a valid value for the Id field. Please start incrementing from 1.

I just wasted a whole day trying to figure out why it was not working only to discover that I needed to start incrementing from 1 instead of 0.

@ajcvickers

This comment has been minimized.

Member

ajcvickers commented Jun 18, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment