C#实体框架应我们设置使用POCO.Id或只是POCO有关系? [英] C# Entity Framework should we set a relationship by using the POCO.Id or just the POCO?

查看:309
本文介绍了C#实体框架应我们设置使用POCO.Id或只是POCO有关系?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务方法,其中分配POCO如预期的另一个POCO的子对象无法正常工作的情况。我使用实体框架4。

 公共无效ChangeOrderCurrency(货币汇率)
{
order.CurrencyId = currency.Id;
order.Currency =货币;与

汇率等
}



哪个更
//其他的东西正确的使用设置的关系? order.CurrencyId = currency.Id order.Currency =货币



在其通过了所有的单元测试,偶尔行 order.Currency =货币同时设置order.CurrencyId和order.Currency为NULL <这个当前的代码/ p>

解决方案

这使得使用的货币对象,而不仅仅是ID,因为在检索数据时,你很可能会希望有更多的意义货币属性而不仅仅是同上。当你创建/更新,你将有标识在这两种情况下可用。


I have a situation in a service method where assigning a POCO as a child object of another POCO does not work as expected. I am using Entity Framework 4.

public void ChangeOrderCurrency(Currency currency)  
{
    order.CurrencyId = currency.Id;
    order.Currency = currency;
    // other stuff related to exchange rates etc
}

Which is more correct to use to set the relationship? order.CurrencyId = currency.Id or order.Currency = currency?

In this current code which passes all unit tests, occasionally the line order.Currency = currency will set both order.CurrencyId and order.Currency to NULL

解决方案

It makes more sense to use the currency object, not just the Id because when you retrieve data you will most likely want to have the Currency property and not just the Id. When you create / update you will have the Id available in both scenarios.

这篇关于C#实体框架应我们设置使用POCO.Id或只是POCO有关系?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆