0

I have an EF model to is connected 1 to 1 for many tables but I am having trouble connecting a 1 to many table. Currently using EF6 on .Net 4.8. The error I am recieving is "Multiplicity is not valid in Role '' in relationship 'Login_Users'. Because the Dependent Role refers to the key properties, the upper bound of the multiplicity of the Dependent Role must be '1'"

The main table is Users

public partial class Users
{
    public Users()
    {
        this.Login = new HashSet<Login>();
        this.Phone = new HashSet<Phone>();
    }
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Name { get; set; }
    public string Email { get; set; }
    public System.DateTime BirthDate { get; set; }
    public int LastFourSSN { get; set; }
    public Nullable<int> AddressId { get; set; }


    public virtual Address Address { get; set; }
    public virtual ICollection<Login> Login { get; set; }
    public virtual ICollection<Phone> Phone { get; set; }
}

One to one tables

public partial class Login
{
    public Login()
    {
        this.Login_Track = new HashSet<Login_Track>();
        this.Policy = new HashSet<Policy>();
    }

    public string Username { get; set; }
    public string Password { get; set; }
    [Key, ForeignKey("Users")]
    public Nullable<int> UserId { get; set; }
    public System.DateTime CreationDate { get; set; }
    public Nullable<System.DateTime> LastLoginDate { get; set; }

    public virtual Users Users { get; set; }
}

Address

public partial class Address
{
    public Address()
    {
        this.Users = new HashSet<Users>();
    }
    [Key, ForeignKey("Users")]
    public int Id { get; set; }
    public string AddressLine1 { get; set; }
    public string AddressLine2 { get; set; }
    public string City { get; set; }
    public string County { get; set; }
    public string State { get; set; }
    public string PostalCode { get; set; }

    public virtual ICollection<Users> Users { get; set; }
}

1 to many table

public partial class Phone
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    public string Number { get; set; }
    public string Type { get; set; }
    [ForeignKey("Users")]
    public Nullable<int> UserId { get; set; }
    public virtual Users Users { get; set; }
}
Airn5475
  • 2,452
  • 29
  • 51
  • 1
    "*I have an EF model to is connected 1 to 1 for many tables*" - this is the problem to begin with. Why can't person have many addresses? Make it 1-to-many. Take Amazon. You can have many addresses where you ship stuff. 1-to-1 has serious implications, Not only in EF but for DB as well – T.S. Mar 16 '20 at 19:17
  • @T.S. any guidance to implementing this with what I presented? – traveldigm Mar 17 '20 at 16:53
  • No. One time I had to setup EF for the existing DB that was constructed in somewhat erratic way. I've spend days to setup EF to work correctly. I don't have time for doing this here. But I can say this - make good design of your DB and you will not have problems you running into – T.S. Mar 17 '20 at 16:57

0 Answers0