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; } in
Cargo` 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>());
}
....