EF核心映射一个对象的一个字段到另一个对象的两个不同字段
我有一个货币对象和一个产品对象。我需要将货币与产品对象的批发货币以及零售场链接起来,
public class Currency
{
/// <summary>
/// Id is the 3 character ISO currency code
/// </summary>
[StringLength(3)]
public string Id { get; set; }
[StringLength(100)]
public string Name { get; set; }
}
我有另一个带有2个货币字段的对象:
public class Product
{
public int Id { get; set; }
public decimal WholesaleRate { get; set; }
public string WholesaleCurrencyId { get; set; }
public virtual Currency WholesaleCurrency { get; set; }
public decimal RetailRate { get; set; }
public string RetailCurrencyId { get; set; }
public virtual Currency RetailCurrency { get; set; }
}
我的查询是:
- 这两个对象之间有什么关系?一对多还是一对一?
- 我如何表达这些关系?上述正确吗?
I have a Currency object and a Product object. I need to link the Currency to WholesaeCurrency as well as RetailCurrency fields of the Product object
public class Currency
{
/// <summary>
/// Id is the 3 character ISO currency code
/// </summary>
[StringLength(3)]
public string Id { get; set; }
[StringLength(100)]
public string Name { get; set; }
}
I have another object with 2 currency fields:
public class Product
{
public int Id { get; set; }
public decimal WholesaleRate { get; set; }
public string WholesaleCurrencyId { get; set; }
public virtual Currency WholesaleCurrency { get; set; }
public decimal RetailRate { get; set; }
public string RetailCurrencyId { get; set; }
public virtual Currency RetailCurrency { get; set; }
}
My queries are:
- What is the relation between these 2 objects? one-to-many or one-to-one?
- How do I express these relations? Is the above correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以尝试以下演示:
以货币:
中的货币:
在您的上下文中添加下面的代码:
You can try the below demo:
In Currency:
In Product:
In your context add below code:
Result: