开发者

EF Code First Query and assign without savechanges

开发者 https://www.devze.com 2023-03-31 22:19 出处:网络
new ProductReadModel { Nr = (nr++).ToString(), Abbreviation = \"Ewb\", Name = \"Some product开发者_运维技巧\",
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).

0

精彩评论

暂无评论...
验证码 换一张
取 消