MVC EF code. First mutual mistake

I want to have a list of stands (at the exhibition) and a list of exhibitors.

The list of stands is separate from the list of exhibitors, but after registration I want the exhibitor to be able to book a stand.

When they select / order a stand, I would like to be able to have a list of stands in my opinion, as well as show the associated exhibitor who ordered it.

Similarly, I would like to list in a different form the participants, as well as the stand they booked.

So, I'm trying to establish a one-to-one relationship (using EF CodeFirst).

However, when I try to add a controller for the stand or exhibitor, I get the following error:

enter image description here

My models:

public class Stand { public int StandID { get; set; } public string Description { get; set; } public bool Booked { get; set; } public int ExhibitorID { get; set; } public virtual Exhibitor Exhibitor { get; set; } } public class Exhibitor { public int ExhibitorID { get; set; } public string Company { get; set; } public int StandID { get; set; } public virtual Stand Stand { get; set; } } 

I am sure that this is due to the "virtual" part of the models.

Can someone please indicate what needs to be updated in order to allow the connection?

Thanks,

Mark

+4
source share
2 answers

EF does not know which object is the main (parent) and which is dependent (child). You need to declare a foreign key for the item, which should be the first. You can do this using annotation or smooth display.

annotation

Add the following namespace:

 using System.ComponentModel.DataAnnotations.Schema; 

Annotate your Stand class with the following annotation:

 public class Stand { [ForeignKey("Exhibitor")] public int StandID { get; set; } public string Description { get; set; } public bool Booked { get; set; } public int ExhibitorID { get; set; } public virtual Exhibitor Exhibitor { get; set; } } 

Free match

Override the OnModelCreating method in the DbContext class to include:

 modelBuilder.Entity<Stand>() .HasOptional(s => s.Exhibitor) .WithRequired(e => e.Stand); 
+5
source

The model you created cannot work with relational databases. Stand requires ExibitorId , and Exibitor needs StandId . Looping does not allow you to insert rows in both tables.

Assuming that Exibitor can have more than one Stand , and one option is to convert the relationship to one to many.

 public class Stand { public int StandID { get; set; } public string Description { get; set; } public bool Booked { get; set; } public int? ExhibitorID { get; set; } public virtual Exhibitor Exhibitor { get; set; } } public class Exhibitor { public int ExhibitorID { get; set; } public string Company { get; set; } public virtual ICollection<Stand> Stands { get; set; } } 

Or you can use collaborative primary key mapping to make relationships one-to-one. Where Stand is the main object. Exibitor will use StandId as its PK.

 public class Stand { public int StandID { get; set; } public string Description { get; set; } public bool Booked { get; set; } public virtual Exhibitor Exhibitor { get; set; } } public class Exhibitor { public int ExhibitorID { get; set; } public string Company { get; set; } public virtual Stand Stand { get; set; } } 

Use the Fluent API to customize relationships.

 modelBuilder.Entity<Exibitor>().HasRequired(e => e.Stand) .WithOptional(s => s.Exibitor); 
+3
source

All Articles