简体   繁体   English

实体框架 6 多重性无效?

[英]Entity Framework 6 Multiplicity is not valid?

I have two tables where I want to implement a 1-1 relationship.I run into a issue described in this post: EntityFramework : Invalid column name *_ID1 I tried to implement the solution,to add the Foreign Key attribute and I run into another issue:我有两个表,我想在其中实现 1-1 关系。我遇到了这篇文章中描述的问题: EntityFramework : Invalid column name *_ID1我试图实现解决方案,添加外键属性,但我遇到了另一个问题:

Multiplicity is not valid in Role 'Account_Companies_Target' in relationship 'Account_Companies'.多重性在关系“Account_Companies”中的角色“Account_Companies_Target”中无效。 Because the Dependent Role properties are not the key properties, the upper bound of the multiplicity of the Dependent Role must be '*'.由于从属角色属性不是关键属性,因此从属角色的重数上限必须为“*”。

These are the classes:这些是类:

 public class Account
{
    [Key]
    public Guid Id { get; set; }
    public string CompanyName { get; set; }
    public long CNP { get; set; }
    public string Name { get; set; }
    public string Surname { get; set; }
    public long PhoneNumber { get; set; }
    public string Address { get; set; }
    [Required]
    public string UserName { get; set; }

    [Required(ErrorMessage = "Password is Required.")]
    public string Password { get; set; }
    public DateTime CreatedOn { get; set; }

    [NotMapped]
    [Required(ErrorMessage = "Confirmation Password is Required")]
    [Compare("Password", ErrorMessage = "Password Must Match")]
    public string ConfirmPassword { get; set; } 

    [Required]
    public AccountType AccountType { get; set; }

    public int CUI { get; set; }

    [ForeignKey("Id")]
    public virtual Company Companies { get; set; }

}

   public class Company
{

    [Key]
    public long CUI { get; set; }
    public Guid Id { get; set; }
    public string CompanyName { get; set; }
    public string Simbol { get; set; }
    public int SharesCount { get; set; }
    public decimal SharePrice { get; set; }
    public virtual Account Account { get; set; }

}

and this is the OnModelCreating overide:这是 OnModelCreating 覆盖:

  protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Account>()
           .HasOptional(account => account.Companies)
           .WithRequired(company => company.Account);            
    }

If Account.CUI, the foreign key property, is not a key, then the relationship is 1:many, not 1:1.如果外键属性 Account.CUI 不是键,则关系是 1:many,而不是 1:1。 And EF6 only allows an entity to have one key.而 EF6 只允许一个实体拥有一个密钥。

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

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