Search code examples
entity-frameworkasp.net-mvc-5ef-code-firstentity-framework-migrationsef-code-first-mapping

Can't set the Relation between 2 DB Models


I Got This Issue:

I Have the Application User Class Like This

 
public class ApplicationUser : IdentityUser
    {
        public ROLES Role { get; set; }
        public int? CompanyId { get; set; }
        public int? AreaId { get; set; }
        public string Document { get; set; }
        public bool Enable { get; set; }

        [ForeignKey("CompanyId")]
        public virtual Company Company { get; set; }
        [ForeignKey("AreaId")]
        public virtual Area Area { get; set; }
        public virtual ICollection Measures { get; set; }

    }
 

And I Got this another Model:

 

public class Area
    {
        public int AreaId { get; set; }
        public string AreaName { get; set; }
        public int CompanyId { get; set; }
        public string UserId { get; set; }

        [ForeignKey("CompanyId")]
        public virtual Company Company { get; set; }
        [Key, ForeignKey("UserId")]
        public ApplicationUser ApplicationUser { get; set; }
    }
 

And when i try to: add-migration

the PM Console throws:

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

I have been trying all day but I can't find a way to tell the Entity Framework to recognize the relation.

Any ideas?

Thanks for reading


Solution

  • The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations

    This Post give me the Answer I Need!!! It's pretty hard to find...

    So I let you the post here... Thanks for all of your help!