Quantcast
Channel: ADO.NET, Entity Framework, LINQ to SQL, NHibernate
Viewing all articles
Browse latest Browse all 1698

Entity Framework one to many relationship table design help

$
0
0

I am trying to come to grips with using EF6 in my solution. I have done lots of reading and tutorials. Now I'm trying to implement into a real world app that I have. 

I have the following tables in my data model. They all inherit from a base class that adds an "Id" PK column to each table below.

 public class Company
    {

        [Required]
        [MinLength(2)]
        [MaxLength(48)]
        public virtual string CompanyName { get; set; }

        [Required]
        [MinLength(2)]
        [MaxLength(48)]
        public virtual string CompanyLegalName { get; set; }

        [Required]
        public virtual bool ActiveYesNo { get; set; }


    }

I have this table, which holds ALL addresses. This can hold the address for any type of entity, company or any other.

    public class Address
    {
        [Required]
        [MaxLength(10)]
        public virtual string AddressType { get; set; }

        [Required]
        [MinLength(5)]
        [MaxLength(100)]
        public virtual string AddressLine1 { get; set; }

        [MaxLength(100)]
        public virtual string AddressLine2 { get; set; }

        [MaxLength(50)]
        public virtual string AddressLine3 { get; set; }

        [Required]
        [MinLength(3)]
        [MaxLength(100)]
        public virtual string City { get; set; }

        [Required]
        public virtual Country Country { get; set; }

        public virtual State State { get; set; }

        public virtual string County { get; set; }

        [Required]
        [MaxLength(10)]
        public virtual string PostalCode { get; set; }

        [Required]
        public virtual bool DefaultYN { get; set; }

    }

In order to for a company to have multiple addresses, I define the table below.

    public class CompanyAddress
    {
        public virtual Company Company { get; set; }
        public virtual Address Address { get; set; }
    }


My issue comes in when I add the following line into the Company table:

public virtual CompanyAddress Addresses { get; set; }

After I add this line and I do "Add-Migration" I get the following error message

Unable to determine the principal end of an association between the types 'CompanyAddress' and 'Company'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.	

So then I found this via google: http://stackoverflow.com/questions/6531671/what-does-principal-end-of-an-association-means-in-11-relationship-in-entity-fr

I tried the first two answers from that thread in my project and neither of them worked.

Can someone help me setup this relationship for the above tables and let me know what I am missing and/or doing wrong here?

Thanks to all that reply!

Blue


Viewing all articles
Browse latest Browse all 1698

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>