2

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

NivekJump
  • 31
  • 2
  • I think it's choking on you Company navigation properties. It looks like it's required in the Area class. Try using the Required attribute there or use fluent API to specify that one is required and the other optional. – Kirby Oct 12 '16 at 00:58

2 Answers2

0

Add Attribute for AreaId in Area class

[Key]
public int AreaId { get; set; }

and if you want 1-1 relationship for ApplicationUser and Area update your code like

[Unique]
[ForeignKey("UserId")]
public ApplicationUser ApplicationUser { get; set; }
erdi yılmaz
  • 352
  • 1
  • 4
  • 15
  • I want to AreaId in ApplicationUser its a Optional Key And also Application user its a OptionalKey in Area. – NivekJump Oct 12 '16 at 14:07
  • No... I want a 1-1 Optional relationship. The ApplicationUser on Area Its Optional and can be just 1 per Area. And the Area on ApplicationUser can be just 2 per User. – NivekJump Oct 12 '16 at 22:01
  • Can you give me an example with data – erdi yılmaz Oct 13 '16 at 05:14
  • Okey i Want something like this: ApplicationUser... Id= hf9d8fm-as9d (Or something like that...) CompanyId = 9997654(But this can be null too) (ForeignKey) AreaId = 665 (This can be null too) (ForeignKey) Document = 10098264926 Enable = true In the other side AreaId can be: AreaId = 7789 (PrimaryKey) AreaName = IT Deparment CompanyId = 88998752 (This Can't be null an Area Always be part of a Company) UserId = ajd9d7kas-djdj0as8sydh (This is the boss of the area, and can be null too) – NivekJump Oct 13 '16 at 15:03
  • In that order... ApplicationUsers store me all the users of different Companies And Areas Store me all the areas of different companies... An User can have an Area or Not So Can Have a Company or Not. And an Area can have a Boss Area or Not But always have a Company. – NivekJump Oct 13 '16 at 15:03
0

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!

Community
  • 1
  • 1
NivekJump
  • 31
  • 2