0

User and Admin is a 1 to 1 relationship where there's only 1 admin role for 1 user.

Previously I fixed a ModelValidationException by adding [Key] on the first line of my administrator class following this link Click here but now I got this error:

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

I tried following this solution but it still gave me the same error.Click Here

enter image description here

User.cs

public class User
    {
        public int UserID { get; set; }
        [StringLength(50, MinimumLength = 1)]
        public string LastName { get; set; }
        [StringLength(50, MinimumLength = 1, ErrorMessage = "First name cannot be longer than 50 characters.")]
        [Column("FirstName")]
        public string FirstMidName { get; set; }
        [DataType(DataType.Date)]
        [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)]
        public DateTime EnrollmentDate { get; set; }

        public int DepotID { get; set; }
        public int DepartmentID { get; set; }
        public int TicketID { get; set; }


        public string FullName
        {
            get { return LastName + ", " + FirstMidName; }
        }

        //Setting up relationships A use can apply for any number of tickets, so Tickets is defined as a collection of Ticket entities.
        public virtual ICollection<Ticket> Tickets { get; set; }

        //Setting up relationships Users can only have ONE adminstrator so we use public virtual Administrator Administrator { get; set; }
        public virtual Administrator Administrator { get; set; }
        public virtual Department Department { get; set; }

        public virtual Depot Depot { get; set; }
    }

Administrator.cs

public class Administrator
{
    [Key]
    public int AdminID { get; set; }
    public int TicketID { get; set; }   
    public int UserID { get; set; }
    [StringLength(50)]
    public string AdminRole { get; set; }
    public virtual ICollection<Ticket> Tickets { get; set; }
    public virtual User User { get; set; }
}

This is in the configuration.cs where I do the migration for user and administrator.

User

 var users = new List<User>
            {
                new User { FirstMidName = "lala",   LastName = "la", 
                    EnrollmentDate = DateTime.Parse("2016-02-18") },
                new User { FirstMidName = "baba", LastName = "baba",    
                    EnrollmentDate = DateTime.Parse("2016-02-18") },
                new User { FirstMidName = "dada",   LastName = "dada",     
                    EnrollmentDate = DateTime.Parse("2016-02-18") },
                new User { FirstMidName = "Christine",   LastName = "West",     
                    EnrollmentDate = DateTime.Parse("2016-02-18") },

            };

Administrator

    var administrator = new List<Administrator>
    {
        new Administrator {AdminID = 1, AdminRole = "Administrator LVL1", User = users.Single ( s => s.UserID == 1),
        Tickets = new List<Ticket>() },
        new Administrator {AdminID = 2, AdminRole = "Administrator LVL2", User = users.Single ( s => s.UserID == 2),
        Tickets = new List<Ticket>() },
        new Administrator {AdminID = 3, AdminRole = "Administrator LVL3", User = users.Single ( s => s.UserID == 3),
        Tickets = new List<Ticket>() }



    };
    administrator.ForEach(s => context.Administrators.AddOrUpdate(p => p.AdminID, s));
    context.SaveChanges();
}
Community
  • 1
  • 1
TykiMikk
  • 1,058
  • 3
  • 15
  • 31
  • In a one to one relation the entities must share the same primary key. So remove the `AdminId` and make the `UserId` the primary key. Further, you should configure the principal end of the relation, i.e. the direction the cascading delete should take. It most likely that when the user is deleted the admin should be deleted too, so the User should be the principal. See http://www.entityframeworktutorial.net/code-first/configure-one-to-one-relationship-in-code-first.aspx – Dabblernl Feb 28 '16 at 21:08
  • *I tried following this solution* I don't see how you used that proposed mapping. – Gert Arnold Feb 28 '16 at 23:03
  • @GertArnold I tried it but deleted it since it didn't solve my problem – TykiMikk Feb 29 '16 at 00:00
  • @Dabblernl But I need adminID because adminID identifies whether the user is a Administrator LVL1 or Administrator LVL2 etc? I put [Key, ForeignKey("User")] on top of public int UserID { get; set; } – TykiMikk Feb 29 '16 at 02:32
  • @GertArnold That didn't make any sense. – TykiMikk Feb 29 '16 at 07:56
  • Look, it's all about the mapping here. You tried something in a link that, from what I see there, should be the correct mapping. If that doesn't work, you should show how you applied that mapping. – Gert Arnold Feb 29 '16 at 08:00
  • @GertArnold I know I'm currently reading the link kindly provided by Dabblernl and editing my code as of right now. – TykiMikk Feb 29 '16 at 08:03
  • @GertArnold I was warned once that I can't edit my question into a different question so I created a new one. I've done the mapping and need someone to see if it is correct : http://stackoverflow.com/questions/35695056/relational-database-mapping-in-mvc – TykiMikk Feb 29 '16 at 08:41

0 Answers0