如何解决“多重性在“角色”中无效错误? [英] How to resolve " Multiplicity is not valid in Role" error?

查看:213
本文介绍了如何解决“多重性在“角色”中无效错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下模型:

public class Retailer : Entity
{
    public string Name { get; set; }
    public string Address { get; set; }
    public virtual ICollection<Product> Products { get; set; }
    public virtual ICollection<Customer> Customers { get; set; }
}

public class Product : Entity
{
    public string Name { get; set; }
    public string Description { get; set; }
    public string ECommerceUrl { get; set; }
    public string ImageUrl { get; set; }
    public Retailer Retailer { get; set; }

    public ICollection<Category> Categories { get; set; }
}

我试图定义1到(0或许多)关系零售商可以拥有以下产品0或多个产品:

I'm trying to define a 1 to (0 or many) relationship where a retailer can have 0 or multiple products with the following:

modelBuilder.Entity<Product>()
    .HasRequired(r => r.Retailer)
    .WithMany(p => p.Products)
    .HasForeignKey(p => p.Id); // Id is defined in the base class

我收到错误


Product_Retailer_Source::多重性在关系Product_Retailer中的角色Product_Retailer_Source中无效。因为依赖角色是指关键属性,从属角色的多重性的上限必须为1。

Product_Retailer_Source: : Multiplicity is not valid in Role 'Product_Retailer_Source' in relationship 'Product_Retailer'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'.

我如何界定关系是错误的?如何解决这个问题?

What is wrong with how I'm defining the relationship? How can I fix this?

推荐答案

尝试这个

public class Product : Entity
{
    public Retailer Retailer { get; set; }
    public int RetailerId { get; set; }
}

使用此配置(您没有定义外部存储零售商针对产品)

With this configuration (you didn't have a foreign key defined to store the RetailerId against the Product)

modelBuilder.Entity<Product>()
    .HasRequired(r => r.Retailer)
    .WithMany(p => p.Products)
    .HasForeignKey(p => p.RetailerId);

这篇关于如何解决“多重性在“角色”中无效错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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