简体   繁体   English

实体框架更新sql请求错误

[英]entity framework update sql request error

I got a weird thing with EF 6: 我对EF 6感到很奇怪:

I try to update an entity with this code: 我尝试使用以下代码更新实体:

public void Update(ref CommandeVente vente)
{
    Ctx.Etat.Attach(vente.etat);
    Ctx.Entry<Dossier>(vente).State = EntityState.Modified;
}

CommandeVente Class is inherited from a DossierVente Class which is inherited too from a Dossier Class. CommandeVente Class继承自DossierVente类,该类也从Dossier类继承。

When I save the context the SQL request generated take very few properties of the entity, and the one modified is not in: 当我保存上下文时,生成的SQL请求只占用实体的很少属性,而修改后的属性不在:

UPDATE [dbo].[Dossiers]
SET [verrouile] = @0, [numDossier] = @1, [dateCreation] = @2, [dateCloture] = NULL, [tva] = @3, [PrixHT] = @4, [prixTTC] = @5
WHERE ([id] = @6)

Do you know this issue and how to solve it? 你知道这个问题以及如何解决它吗?

Thanks. 谢谢。

The solution to this issue was to separate foreignkey and related entity. 该问题的解决方案是将外键和相关实体分开。

In my exemple I use in my Vente Class 在我的例子中,我在Vente Class中使用

public virtual Etat etat { get; set; }

In this case Entity Framework must manages the foreignkey, but in my case it doesn't. 在这种情况下,实体框架必须管理外键,但在我的情况下它不会。

So I modified the entity to: 所以我将实体修改为:

public int? etatId { get; set; }
public  Etat etat { get; set; }

And it's done. 它已经完成了。 Now the foreignkey is correctly updated. 现在外键已正确更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM