EF核心映射一个对象的一个​​字段到另一个对象的两个不同字段

发布于 2025-02-06 19:14:48 字数 889 浏览 2 评论 0原文

我有一个货币对象和一个产品对象。我需要将货币与产品对象的批发货币以及零售场链接起来,

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; }
    }

我的查询是:

  1. 这两个对象之间有什么关系?一对多还是一对一?
  2. 我如何表达这些关系?上述正确吗?

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:

  1. What is the relation between these 2 objects? one-to-many or one-to-one?
  2. How do I express these relations? Is the above correct?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

2025-02-13 19:14:48

您可以尝试以下演示:

以货币:

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; }

        public virtual ICollection<Product> ProductWholesale { get; set; }
        public virtual ICollection<Product> ProductRetail { get; set; }
    }

中的货币:

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; }
    }

在您的上下文中添加下面的代码:

 protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Product>()
                .HasOne(p => p.WholesaleCurrency)
                .WithMany(t => t.ProductWholesale)
                .HasForeignKey(m => m.WholesaleCurrencyId )
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<Product>()
                .HasOne(p => p.RetailCurrency)
                .WithMany(t => t.ProductRetail)
                .HasForeignKey(m => m.RetailCurrencyId)
                .OnDelete(DeleteBehavior.Restrict);
        }

“在此处输入图像说明”

You can try the below demo:

In Currency:

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; }

        public virtual ICollection<Product> ProductWholesale { get; set; }
        public virtual ICollection<Product> ProductRetail { get; set; }
    }

In Product:

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; }
    }

In your context add below code:

 protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<Product>()
                .HasOne(p => p.WholesaleCurrency)
                .WithMany(t => t.ProductWholesale)
                .HasForeignKey(m => m.WholesaleCurrencyId )
                .OnDelete(DeleteBehavior.Restrict);

            modelBuilder.Entity<Product>()
                .HasOne(p => p.RetailCurrency)
                .WithMany(t => t.ProductRetail)
                .HasForeignKey(m => m.RetailCurrencyId)
                .OnDelete(DeleteBehavior.Restrict);
        }

Result:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文