Marco
Marco

Reputation: 5109

EF Code First Query and assign without savechanges

new ProductReadModel
{
    Nr = (nr++).ToString(),
    Abbreviation = "Ewb",
    Name = "Some product",
    BodyMaterial = context.BodyMaterialReadModels.FirstOrDefault(b => b.Abbreviation == "EDTA")
}

During database initialization I try to fill my tables with the EF code first seeder. In one of my seeders I add all the bodymaterials. In this one I try to relate these bodymaterials to products. When I debug I see my bodymaterial collections counts 14 items. However after going through this code the BodyMaterial property is null.

Any suggestions?

Upvotes: 0

Views: 131

Answers (1)

Ladislav Mrnka
Ladislav Mrnka

Reputation: 364279

You are executing database query against empty database. Try this instead:

BodyMaterial = context.BodyMaterialReadModels.Local.FirstOrDefault(...);

or use your BodyMaterialCollection directly. The set exposed on the context is entry point to the database not to your not persisted entities (except Local property).

Upvotes: 3

Related Questions