user1973285
user1973285

Reputation:

MVC Model Poco class, design issue,

i have a little issue.

I am trying to make a Address class were i save all my application's addresses.

The thing is that i want to be able to link several addresses to both customer and company.

Can someone please show me how i should design it?

I use MVC 4 with entityFramework code first.

 public class Address
    {
        [Key]
        public int AddressId { get; set; }

        public string Street { get; set; }

        public string Number { get; set; }

        public string ZipCode { get; set; }

        public int CountyId { get; set; }
        public virtual County County { get; set; }

        public int StateId { get; set; }
        public virtual State State { get; set; }

        public int CountryId { get; set; }
        public virtual Country Country { get; set; }
    }

public class Customer
    {
        [Key]
        public int CustomerId { get; set; }
        public int CompanyId { get; set; }

        [Display(Name = "Kund")]
        public string Name { get; set; }

        public virtual Company Company { get; set; }

        // wan't to display a ICollection of addresses.
        //public virtual ICollection<Address> Addresses { get; set; }
    }

 public class Company
    {
        [Key]
        public int CompanyId { get; set; }
        [Display(Name = "Organisationsnummer")]
        public string OrganisationNumber { get; set; }
        [Display(Name = "Företag")]
        public string Name { get; set; }
        [Display(Name = "Företag skapat")]
        public DateTime CreationDate { get; set; }

        public virtual ICollection<Customer> Customers { get; set; }
        public virtual ICollection<Employee> Employees { get; set; }
        // wan't to display a ICollection of addresses.
        //public virtual ICollection<Address> Addresses { get; set; }
    }

Upvotes: 1

Views: 295

Answers (1)

Admir Tuzović
Admir Tuzović

Reputation: 11177

At the following properties and annotations to your classes, it should help Entity Framework understand your relationships:

 public class Address
{
    [Key]
    public int AddressId { get; set; }

    public string Street { get; set; }

    public string Number { get; set; }

    public string ZipCode { get; set; }

    public int CustomerId {get;set;}
    [ForeignKey("CustomerId")]
    public virtual Customer Customer {get;set;}

    public int CompanyId {get;set;}
    [ForeignKey("CompanyId")]
    public virtual Company Company {get;set;}

    public int CountyId { get; set; }
    [ForeignKey("CountyId")]
    public virtual County County { get; set; }

    public int StateId { get; set; }
    [ForeignKey("StateId")]
    public virtual State State { get; set; }

    public int CountryId { get; set; }
    [ForeignKey("CountryId")]
    public virtual Country Country { get; set; }
}

public class Customer
{
    [Key]
    public int CustomerId { get; set; }

    public int CompanyId { get; set; }

    [Display(Name = "Kund")]
    public string Name { get; set; }

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

    [InverseProperty("Customer")]
    public virtual ICollection<Address> Addresses { get; set; }
}



public class Company
{
    [Key]
    public int CompanyId { get; set; }
    [Display(Name = "Organisationsnummer")]
    public string OrganisationNumber { get; set; }
    [Display(Name = "Företag")]
    public string Name { get; set; }
    [Display(Name = "Företag skapat")]
    public DateTime CreationDate { get; set; }

    [InverseProperty("Company")]
    public virtual ICollection<Customer> Customers { get; set; }

    [InverseProperty("Company")]
    public virtual ICollection<Address> Addresses { get; set; }

    [InverseProperty("Company")]
    public virtual ICollection<Employee> Employees { get; set; }

}

public class Employee
{
[Key]
public int EmployeeId {get;set;}

public int CompanyId {get;set;}

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

EDIT: You now have issue of another type. Your DELETE/UPDATE rules are causing the error you're seeing right now. You've most likely set CASCADE delete on multiple paths that lead to same primary key table. For start set all your relationships that look like this:

enter image description here enter image description here

Then assume this scenario: You have entities A, B and C. Entity B has FK to entity A. Entity C has FK to entity A and entity B.

You're allowed to set cascade delete/update only on one dependency path. This means that you can only do:

Cascade delete between A and B and cascade delete between B and C.

If you however add cascade delete between A and C as well along with above, you'll get an error you have now.

Upvotes: 2

Related Questions