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?
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).
精彩评论