How to create two way navigation property in entity framework?

asp.net-mvc c# entity-framework entity-framework-6 linq

Question

There is a Cargo class/table which has identity CargoID There is a ContainerIn class/table which containes CargoID

Every Cargo could have 1 or 0 corresponding container entries. I am trying to create navigation properties such that. Cargo.ContainerIn--->should give me associated ContainerIn entry ContainerIn.Cargo--->should give me associated Cargo entry

Cargo Class:

public class Cargo
{
    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int CargoID { get; set; }//SerialNo
    [Required]
    public DateTime DateOfPassage { get; set; }
    public string CompanyUserName { get; set; }
    public virtual ContainerIn ContainerIn { get; set; }
}

ContainerIn Subclass:

public class ContainerIn 
{
  [Key]
  [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
  public int ContainerInID { get; set; }
  public int CargoID { get; set; }
  public virtual Cargo Cargo { get; set; }
  public int LoadStatus { get; set; }
}

I have also tried adding public int ContainerInID { get; set; } inCargo` class. I am still getting :

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

EDIT: I have added OnModelCreating in ApplicationDbContext class.

 public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Entity<PisMark3.Models.Cargo.Cargo>()
                        .HasOptional(s => s.ContainerIn)
                        .WithRequired(ad => ad.Cargo);
        }
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
            //  Database.SetInitializer<ApplicationDbContext>(new DropCreateDatabaseIfModelChanges<ApplicationDbContext>());
        }
    .... 

Now I am getting: enter image description here

1
0
11/16/2018 6:15:22 AM

Popular Answer

you're pretty close. I think you want the following:

    public class Cargo
    {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int CargoID { get; set; }//SerialNo
      [Required]
      public DateTime DateOfPassage { get; set; }
      public string CompanyUserName { get; set; }
      public int ContainerInId { get; set; } //need to define a foreign key. This is happening by naming convention in this case as with your `ContainerIn.CargoId` foreign key 
      public virtual ContainerIn ContainerIn { get; set; }
    }

    public class ContainerIn 
    {
      [Key]
      [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
      public int ContainerInID { get; set; }
      public int CargoID { get; set; }
      public virtual Cargo Cargo { get; set; }
      public int LoadStatus { get; set; }
    }

Note this is a circular reference which should probably be avoided if possible however there are certainly some valid use cases. Just thought I'd give a shout out to that.

If you don't want to abide by naming conventions, you can use the ForeignKey data annotation as outlined here

2
11/15/2018 1:31:32 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow