0

Here is my two model and i am getting this error on asp.net mvc5 codefirst when Update-Database

ERROR : Unable to determine the principal end of an association between the types 'ModulericaV1.Areas.Hr.Models.HrDepartment' and 'ModulericaV1.Areas.Hr.Models.HrPerson'. The principal end of this association must be explicitly configured using either the relationship fluent API or data annotations.

DEPARTMENT

 public class HrDepartment
    {
        [Key]
        public int Id { get; set; }

        [Display(Name = "Departman Adı")]
        public string Name { get; set; }

        public int? HrDepartmentId { get; set; }

        [ForeignKey("HrDepartmentId")]
        public virtual HrDepartment RelatedDepartment { get; set; }

        public int HrPersonId { get; set; }
        public virtual HrPerson HrPerson { get; set; }

    }

PERSON

public class HrPerson
    {
        public int Id { get; set; }
        [Display(Name = "Ad")]
        public string Name { get; set; }

        [Display(Name = "Departman")]
        public int HrDepartmentId { get; set; }
        public virtual HrDepartment HrDepartment { get; set; }

    }
umki
  • 769
  • 13
  • 31

1 Answers1

2

You have to override OnModelCreating method of your DbContext. Try something like this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<HrDepartment>().HasRequired(a=>a.HrPerson ).WithRequiredDependent(b=>b.HrDepartment);
}

or this:

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
    modelBuilder.Entity<HrDepartment>().HasRequired(a=>a.HrPerson ).WithRequiredPrincipal(b=>b.HrDepartment);
}

The difference among these resides in the in the principal end. I have to say that I didn't try, just writing what I remember so there can be few errors, however doing something like this you will be able to fix that error.

Moreover you can also set the cascade delete setting .WillCascadeOnDelete(false) (true is the default value).

To answer comment question: I think that EF does not create the DB contraint because if both are required then you can't insert A because you need B to be already created (otherwise check fails) and B can't be created yet because it needs A (for the same reason). So you can work without constraints, but you have to check programmatically before each SaveChanges() or redesign your model. In fact, I think you don't have a required-required relationship but a 1:N, because one department must have a responsible but a person can be responsible of zero to N departments.

Therefore what I'd do is:

1) keep HrDep as it is

2) remove

[Display(Name = "Departman")]
        public int HrDepartmentId { get; set; }
        public virtual HrDepartment HrDepartment { get; set; }

3) replace

modelBuilder.Entity<HrDepartment>().HasRequired(a=>a.HrPerson ).WithRequiredPrincipal(b=>b.HrDepartment);

with

modelBuilder.Entity<HrDepartment>().HasRequired(a => a.HrPerson ).WithMany().WillCascadeOnDelete(false);

Note that on db update there will be errors because of existing DB structure, so the quickest way is to delete DB and let EF recreate it.

Hope this helps, have a nice day,

Alberto

Alberto
  • 1,853
  • 1
  • 18
  • 22
  • 1
    Second one is the solution thanks :) But could you please explain more or how can learn this from a link or tutorial ? – umki Feb 18 '14 at 11:10
  • One more thing, with this options, scaffolding is not creating dropdown list for Person in Department and Department in Person... Only it is creating HrPersonId and HrDepartmentId – umki Feb 18 '14 at 11:14
  • Quick resource: http://msdn.microsoft.com/en-us/data/jj591620.aspx Complete resource: http://www.amazon.com/Programming-Entity-Framework-Julia-Lerman/dp/1449312969 and other books of Julia Lerman – Alberto Feb 18 '14 at 11:14
  • Thanks Alberto but it seems it is not working. There is no relation between the tables now. I can put personid=44 but there is no person with this id – umki Feb 18 '14 at 12:05
  • Alberto, i am working on this issue for 3 days but not resolved, could you please help me on this ? – umki Feb 20 '14 at 08:11
  • Updated! Still I don't use auto-scaffolding so i can't answer that part. – Alberto Feb 20 '14 at 09:52
  • Maybe you want also to read this http://stackoverflow.com/questions/7742131/ef-code-first-fluent-api-withrequireddependent-and-withrequiredprincipal and http://stackoverflow.com/questions/5368029/need-some-help-trying-to-understand-entity-framework-4-1s-code-first-fluent-api – Alberto Feb 20 '14 at 09:54