How to create two way navigation property in entity framework?












0















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










share|improve this question

























  • You already doing two way navigation, you just need to apply foreign key annotation.

    – Just code
    Nov 15 '18 at 13:22











  • If you have enabled lazy loading this should work automatically when you ask cargo or containerIn

    – Cybercop
    Nov 15 '18 at 13:34











  • Did the posted answer solve your issue? If not, please expand on what's happening

    – GregH
    Nov 15 '18 at 14:18











  • In EF6 you have to use the Primary Key Property as the Foreign Key Property for a 1-1 relationship. In the model you currently have a Container can have multiple ContainerIn entities, as there is nothing preventing you from setting the same CargoId on two ContainerIn entities, and so would need a ICollection Navigation Property.

    – David Browne - Microsoft
    Nov 15 '18 at 14:30













  • @Justcode nable 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.

    – Arbaaz
    Nov 16 '18 at 5:24
















0















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










share|improve this question

























  • You already doing two way navigation, you just need to apply foreign key annotation.

    – Just code
    Nov 15 '18 at 13:22











  • If you have enabled lazy loading this should work automatically when you ask cargo or containerIn

    – Cybercop
    Nov 15 '18 at 13:34











  • Did the posted answer solve your issue? If not, please expand on what's happening

    – GregH
    Nov 15 '18 at 14:18











  • In EF6 you have to use the Primary Key Property as the Foreign Key Property for a 1-1 relationship. In the model you currently have a Container can have multiple ContainerIn entities, as there is nothing preventing you from setting the same CargoId on two ContainerIn entities, and so would need a ICollection Navigation Property.

    – David Browne - Microsoft
    Nov 15 '18 at 14:30













  • @Justcode nable 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.

    – Arbaaz
    Nov 16 '18 at 5:24














0












0








0








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










share|improve this 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







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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 16 '18 at 6:15







Arbaaz

















asked Nov 15 '18 at 13:14









ArbaazArbaaz

1,64384196




1,64384196













  • You already doing two way navigation, you just need to apply foreign key annotation.

    – Just code
    Nov 15 '18 at 13:22











  • If you have enabled lazy loading this should work automatically when you ask cargo or containerIn

    – Cybercop
    Nov 15 '18 at 13:34











  • Did the posted answer solve your issue? If not, please expand on what's happening

    – GregH
    Nov 15 '18 at 14:18











  • In EF6 you have to use the Primary Key Property as the Foreign Key Property for a 1-1 relationship. In the model you currently have a Container can have multiple ContainerIn entities, as there is nothing preventing you from setting the same CargoId on two ContainerIn entities, and so would need a ICollection Navigation Property.

    – David Browne - Microsoft
    Nov 15 '18 at 14:30













  • @Justcode nable 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.

    – Arbaaz
    Nov 16 '18 at 5:24



















  • You already doing two way navigation, you just need to apply foreign key annotation.

    – Just code
    Nov 15 '18 at 13:22











  • If you have enabled lazy loading this should work automatically when you ask cargo or containerIn

    – Cybercop
    Nov 15 '18 at 13:34











  • Did the posted answer solve your issue? If not, please expand on what's happening

    – GregH
    Nov 15 '18 at 14:18











  • In EF6 you have to use the Primary Key Property as the Foreign Key Property for a 1-1 relationship. In the model you currently have a Container can have multiple ContainerIn entities, as there is nothing preventing you from setting the same CargoId on two ContainerIn entities, and so would need a ICollection Navigation Property.

    – David Browne - Microsoft
    Nov 15 '18 at 14:30













  • @Justcode nable 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.

    – Arbaaz
    Nov 16 '18 at 5:24

















You already doing two way navigation, you just need to apply foreign key annotation.

– Just code
Nov 15 '18 at 13:22





You already doing two way navigation, you just need to apply foreign key annotation.

– Just code
Nov 15 '18 at 13:22













If you have enabled lazy loading this should work automatically when you ask cargo or containerIn

– Cybercop
Nov 15 '18 at 13:34





If you have enabled lazy loading this should work automatically when you ask cargo or containerIn

– Cybercop
Nov 15 '18 at 13:34













Did the posted answer solve your issue? If not, please expand on what's happening

– GregH
Nov 15 '18 at 14:18





Did the posted answer solve your issue? If not, please expand on what's happening

– GregH
Nov 15 '18 at 14:18













In EF6 you have to use the Primary Key Property as the Foreign Key Property for a 1-1 relationship. In the model you currently have a Container can have multiple ContainerIn entities, as there is nothing preventing you from setting the same CargoId on two ContainerIn entities, and so would need a ICollection Navigation Property.

– David Browne - Microsoft
Nov 15 '18 at 14:30







In EF6 you have to use the Primary Key Property as the Foreign Key Property for a 1-1 relationship. In the model you currently have a Container can have multiple ContainerIn entities, as there is nothing preventing you from setting the same CargoId on two ContainerIn entities, and so would need a ICollection Navigation Property.

– David Browne - Microsoft
Nov 15 '18 at 14:30















@Justcode nable 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.

– Arbaaz
Nov 16 '18 at 5:24





@Justcode nable 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.

– Arbaaz
Nov 16 '18 at 5:24












2 Answers
2






active

oldest

votes


















2














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






share|improve this answer


























  • I am getting nable 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.

    – Arbaaz
    Nov 16 '18 at 5:23











  • Then you need to configure teh relationship via the fluent api or data annotations as shown here: stackoverflow.com/questions/17254724/… please see the link i referenced for more info regarding data annotations

    – GregH
    Nov 16 '18 at 13:06



















0















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




This is telling you to define your relationship, because it can not understand the relationship.



What you are looking for is
One-to-Zero-or-One relationship here is the link



This is your model,



 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 CompanyUserNameContainIn { get; set; }
}

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


This is your flent api code,



protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Cargo>()
.HasOptional(s => s.CompanyUserNameContainIn)
.WithRequired(ad => ad.Cargo);
}


This tells entityframework that it has optional companyusername in cargo model and required model cargo in ContainerIn



You can read more in detail in the link I provided, it has nice example of student and address.



EDIT:



As you want to use identityDBContext you can modify your code as below



// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{

public virtual DbSet<Cargo> Cargo { get; set; }
public virtual DbSet<ContainerIn> ContainerIn { get; set; }

public ApplicationDbContext()
: base("DefaultConnection")
{
}

public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Cargo>()
.HasOptional(s => s.CompanyUserNameContainIn)
.WithRequired(ad => ad.Cargo);

modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });

}

}





share|improve this answer


























  • Please see my edit

    – Arbaaz
    Nov 16 '18 at 6:16











  • Did you update your database? I see no errors trying in my project.

    – Just code
    Nov 16 '18 at 6:32













  • That's strange because I am getting the following error even when I am trying to update the database: One or more validation errors were detected during model generation: PisMark3.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. PisMark3.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.....

    – Arbaaz
    Nov 16 '18 at 6:51











  • just call the command update-database it should work

    – Just code
    Nov 16 '18 at 6:52











  • I did. That is exactly when I am getting the above error in the console.

    – Arbaaz
    Nov 16 '18 at 6:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320333%2fhow-to-create-two-way-navigation-property-in-entity-framework%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














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






share|improve this answer


























  • I am getting nable 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.

    – Arbaaz
    Nov 16 '18 at 5:23











  • Then you need to configure teh relationship via the fluent api or data annotations as shown here: stackoverflow.com/questions/17254724/… please see the link i referenced for more info regarding data annotations

    – GregH
    Nov 16 '18 at 13:06
















2














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






share|improve this answer


























  • I am getting nable 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.

    – Arbaaz
    Nov 16 '18 at 5:23











  • Then you need to configure teh relationship via the fluent api or data annotations as shown here: stackoverflow.com/questions/17254724/… please see the link i referenced for more info regarding data annotations

    – GregH
    Nov 16 '18 at 13:06














2












2








2







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






share|improve this 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







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 13:31

























answered Nov 15 '18 at 13:25









GregHGregH

2,82611752




2,82611752













  • I am getting nable 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.

    – Arbaaz
    Nov 16 '18 at 5:23











  • Then you need to configure teh relationship via the fluent api or data annotations as shown here: stackoverflow.com/questions/17254724/… please see the link i referenced for more info regarding data annotations

    – GregH
    Nov 16 '18 at 13:06



















  • I am getting nable 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.

    – Arbaaz
    Nov 16 '18 at 5:23











  • Then you need to configure teh relationship via the fluent api or data annotations as shown here: stackoverflow.com/questions/17254724/… please see the link i referenced for more info regarding data annotations

    – GregH
    Nov 16 '18 at 13:06

















I am getting nable 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.

– Arbaaz
Nov 16 '18 at 5:23





I am getting nable 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.

– Arbaaz
Nov 16 '18 at 5:23













Then you need to configure teh relationship via the fluent api or data annotations as shown here: stackoverflow.com/questions/17254724/… please see the link i referenced for more info regarding data annotations

– GregH
Nov 16 '18 at 13:06





Then you need to configure teh relationship via the fluent api or data annotations as shown here: stackoverflow.com/questions/17254724/… please see the link i referenced for more info regarding data annotations

– GregH
Nov 16 '18 at 13:06













0















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




This is telling you to define your relationship, because it can not understand the relationship.



What you are looking for is
One-to-Zero-or-One relationship here is the link



This is your model,



 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 CompanyUserNameContainIn { get; set; }
}

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


This is your flent api code,



protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Cargo>()
.HasOptional(s => s.CompanyUserNameContainIn)
.WithRequired(ad => ad.Cargo);
}


This tells entityframework that it has optional companyusername in cargo model and required model cargo in ContainerIn



You can read more in detail in the link I provided, it has nice example of student and address.



EDIT:



As you want to use identityDBContext you can modify your code as below



// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{

public virtual DbSet<Cargo> Cargo { get; set; }
public virtual DbSet<ContainerIn> ContainerIn { get; set; }

public ApplicationDbContext()
: base("DefaultConnection")
{
}

public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Cargo>()
.HasOptional(s => s.CompanyUserNameContainIn)
.WithRequired(ad => ad.Cargo);

modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });

}

}





share|improve this answer


























  • Please see my edit

    – Arbaaz
    Nov 16 '18 at 6:16











  • Did you update your database? I see no errors trying in my project.

    – Just code
    Nov 16 '18 at 6:32













  • That's strange because I am getting the following error even when I am trying to update the database: One or more validation errors were detected during model generation: PisMark3.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. PisMark3.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.....

    – Arbaaz
    Nov 16 '18 at 6:51











  • just call the command update-database it should work

    – Just code
    Nov 16 '18 at 6:52











  • I did. That is exactly when I am getting the above error in the console.

    – Arbaaz
    Nov 16 '18 at 6:57
















0















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




This is telling you to define your relationship, because it can not understand the relationship.



What you are looking for is
One-to-Zero-or-One relationship here is the link



This is your model,



 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 CompanyUserNameContainIn { get; set; }
}

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


This is your flent api code,



protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Cargo>()
.HasOptional(s => s.CompanyUserNameContainIn)
.WithRequired(ad => ad.Cargo);
}


This tells entityframework that it has optional companyusername in cargo model and required model cargo in ContainerIn



You can read more in detail in the link I provided, it has nice example of student and address.



EDIT:



As you want to use identityDBContext you can modify your code as below



// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{

public virtual DbSet<Cargo> Cargo { get; set; }
public virtual DbSet<ContainerIn> ContainerIn { get; set; }

public ApplicationDbContext()
: base("DefaultConnection")
{
}

public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Cargo>()
.HasOptional(s => s.CompanyUserNameContainIn)
.WithRequired(ad => ad.Cargo);

modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });

}

}





share|improve this answer


























  • Please see my edit

    – Arbaaz
    Nov 16 '18 at 6:16











  • Did you update your database? I see no errors trying in my project.

    – Just code
    Nov 16 '18 at 6:32













  • That's strange because I am getting the following error even when I am trying to update the database: One or more validation errors were detected during model generation: PisMark3.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. PisMark3.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.....

    – Arbaaz
    Nov 16 '18 at 6:51











  • just call the command update-database it should work

    – Just code
    Nov 16 '18 at 6:52











  • I did. That is exactly when I am getting the above error in the console.

    – Arbaaz
    Nov 16 '18 at 6:57














0












0








0








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




This is telling you to define your relationship, because it can not understand the relationship.



What you are looking for is
One-to-Zero-or-One relationship here is the link



This is your model,



 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 CompanyUserNameContainIn { get; set; }
}

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


This is your flent api code,



protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Cargo>()
.HasOptional(s => s.CompanyUserNameContainIn)
.WithRequired(ad => ad.Cargo);
}


This tells entityframework that it has optional companyusername in cargo model and required model cargo in ContainerIn



You can read more in detail in the link I provided, it has nice example of student and address.



EDIT:



As you want to use identityDBContext you can modify your code as below



// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{

public virtual DbSet<Cargo> Cargo { get; set; }
public virtual DbSet<ContainerIn> ContainerIn { get; set; }

public ApplicationDbContext()
: base("DefaultConnection")
{
}

public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Cargo>()
.HasOptional(s => s.CompanyUserNameContainIn)
.WithRequired(ad => ad.Cargo);

modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });

}

}





share|improve this answer
















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




This is telling you to define your relationship, because it can not understand the relationship.



What you are looking for is
One-to-Zero-or-One relationship here is the link



This is your model,



 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 CompanyUserNameContainIn { get; set; }
}

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


This is your flent api code,



protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Cargo>()
.HasOptional(s => s.CompanyUserNameContainIn)
.WithRequired(ad => ad.Cargo);
}


This tells entityframework that it has optional companyusername in cargo model and required model cargo in ContainerIn



You can read more in detail in the link I provided, it has nice example of student and address.



EDIT:



As you want to use identityDBContext you can modify your code as below



// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
// Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// Add custom user claims here
return userIdentity;
}
}

public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
{

public virtual DbSet<Cargo> Cargo { get; set; }
public virtual DbSet<ContainerIn> ContainerIn { get; set; }

public ApplicationDbContext()
: base("DefaultConnection")
{
}

public static ApplicationDbContext Create()
{
return new ApplicationDbContext();
}

protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Cargo>()
.HasOptional(s => s.CompanyUserNameContainIn)
.WithRequired(ad => ad.Cargo);

modelBuilder.Entity<IdentityUserLogin>().HasKey<string>(l => l.UserId);
modelBuilder.Entity<IdentityRole>().HasKey<string>(r => r.Id);
modelBuilder.Entity<IdentityUserRole>().HasKey(r => new { r.RoleId, r.UserId });

}

}






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 7:19

























answered Nov 16 '18 at 5:59









Just codeJust code

10.4k53067




10.4k53067













  • Please see my edit

    – Arbaaz
    Nov 16 '18 at 6:16











  • Did you update your database? I see no errors trying in my project.

    – Just code
    Nov 16 '18 at 6:32













  • That's strange because I am getting the following error even when I am trying to update the database: One or more validation errors were detected during model generation: PisMark3.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. PisMark3.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.....

    – Arbaaz
    Nov 16 '18 at 6:51











  • just call the command update-database it should work

    – Just code
    Nov 16 '18 at 6:52











  • I did. That is exactly when I am getting the above error in the console.

    – Arbaaz
    Nov 16 '18 at 6:57



















  • Please see my edit

    – Arbaaz
    Nov 16 '18 at 6:16











  • Did you update your database? I see no errors trying in my project.

    – Just code
    Nov 16 '18 at 6:32













  • That's strange because I am getting the following error even when I am trying to update the database: One or more validation errors were detected during model generation: PisMark3.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. PisMark3.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.....

    – Arbaaz
    Nov 16 '18 at 6:51











  • just call the command update-database it should work

    – Just code
    Nov 16 '18 at 6:52











  • I did. That is exactly when I am getting the above error in the console.

    – Arbaaz
    Nov 16 '18 at 6:57

















Please see my edit

– Arbaaz
Nov 16 '18 at 6:16





Please see my edit

– Arbaaz
Nov 16 '18 at 6:16













Did you update your database? I see no errors trying in my project.

– Just code
Nov 16 '18 at 6:32







Did you update your database? I see no errors trying in my project.

– Just code
Nov 16 '18 at 6:32















That's strange because I am getting the following error even when I am trying to update the database: One or more validation errors were detected during model generation: PisMark3.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. PisMark3.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.....

– Arbaaz
Nov 16 '18 at 6:51





That's strange because I am getting the following error even when I am trying to update the database: One or more validation errors were detected during model generation: PisMark3.Models.IdentityUserRole: : EntityType 'IdentityUserRole' has no key defined. Define the key for this EntityType. PisMark3.Models.IdentityUserLogin: : EntityType 'IdentityUserLogin' has no key defined. Define the key for this EntityType. IdentityUserRoles: EntityType: EntitySet 'IdentityUserRoles' is based on type 'IdentityUserRole' that has no keys defined.....

– Arbaaz
Nov 16 '18 at 6:51













just call the command update-database it should work

– Just code
Nov 16 '18 at 6:52





just call the command update-database it should work

– Just code
Nov 16 '18 at 6:52













I did. That is exactly when I am getting the above error in the console.

– Arbaaz
Nov 16 '18 at 6:57





I did. That is exactly when I am getting the above error in the console.

– Arbaaz
Nov 16 '18 at 6:57


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53320333%2fhow-to-create-two-way-navigation-property-in-entity-framework%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

List item for chat from Array inside array React Native

Thiostrepton

Caerphilly