EF不更新相关实体

时间:2017-05-29 06:33:32

标签: entity-framework entity-framework-6

我有以下代码:

var result = new koper_huurder
{
    guid = koperHuurder.Id.ToString(),
    persoon_1_guid = koperHuurder.KH1Id,
    persoon_2_guid = koperHuurder.KH2Id
};

Mapper.Map(koperHuurder, result);

result.persoon1 = new persoon
{
    guid = koperHuurder.KH1Id,
    mobiel = koperHuurder.KH1Mobiel,
    email_prive = koperHuurder.KH1EmailPrive,
    telefoon = koperHuurder.KH1TelefoonPrive
};

result.persoon2 = new persoon
{
    guid = koperHuurder.KH2Id,
    mobiel = koperHuurder.KH2Mobiel,
    email_prive = koperHuurder.KH2EmailPrive,
    telefoon = koperHuurder.KH2TelefoonPrive
};

context.Entry(result).State = EntityState.Modified;
context.SaveChanges();

然而,在SaveChanges()之后,persoon表中没有任何内容更新。它只是更新了Koper_huurder表。我用SQL Server Profiler检查过这个。延迟加载已禁用。

我不知道该怎么做!

修改

如果我执行以下操作,则会收到验证错误消息。它告诉我persoon记录中的某些列不能为空。这很奇怪,因为我没有对它们做任何事情。

--the same code as above and then--

context.Entry(result).State = EntityState.Modified;
context.Entry(result.persoon1).State = EntityState.Modified;
context.Entry(result.persoon2).State = EntityState.Modified;
context.SaveChanges();

0 个答案:

没有答案