EF-code-first many-to-many related objects generates two different SQL tables (MVC3) - asp.net-mvc-3

I have read this great Q&A and I tried to make something similar. My Model classes are:
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<Course> CoursesAttending { get; set; }
public Person()
{
this.CoursesAttending = new List<Course>();
}
}
public class Course
{
public int CourseId { get; set; }
public string Title { get; set; }
public ICollection<Person> Students { get; set; }
}
public class PersonCourse
{
[Key, Column(Order = 0)]
public int PersonID { get; set; }
[Key, Column(Order = 1)]
public int CourseID { get; set; }
public virtual Person Student { get; set; }
public virtual Course StudentCourse { get; set; }
public int Mark { get; set; }
public string Comment { get; set; }
}
public class SchoolContext : DbContext
{
public DbSet<Course> Courses { get; set; }
public DbSet<Person> People { get; set; }
public DbSet<PersonCourse> PersonCourseLinks { get; set; }
public SchoolContext()
: base("ManyToManyTest")
{
}
}
Now, I try to add a new person and add a new course to his courses list:
[HttpPost]
public ActionResult Create(Person person)
{
if (ModelState.IsValid)
{
Course studentCourse;
try
{
studentCourse = db.Courses.ToList<Course>().First();
}
catch
{
studentCourse = new Course() { Title = "HTML" };
}
person.CoursesAttending.Add(studentCourse);
db.People.Add(person);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(person);
}
Everything goes well, but when I open my created database, I see there are 2 link tables for the Person and Course classes - PersonCourses(with fields: PersonID, CourseID, Mark, Comment) and PersonCourse1(with fields: PersonID, CourseID), and only PersonCourse1 has rows (actually one row). Why does it happen? Did I do something not correct? I expect to see only one link table - the PersonCourses table...

I expect to see only one link table - the PersonCourses table
Then you have to link PersonCourse entity with Person entity though CoursesAttending navigational property. Same thing has to be done to Course entity.
public class Person
{
public int PersonId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public ICollection<PersonCourse> CoursesAttending { get; set; }
public Person()
{
this.CoursesAttending = new List<PersonCourse>();
}
}
public class Course
{
public int CourseId { get; set; }
public string Title { get; set; }
public ICollection<PersonCourse> Students { get; set; }
}

Give foreign key associations to the foreign key properties in your gerund table:
public class PersonCourse
{
[Key, Column(Order = 0)]
public int PersonID { get; set; }
[Key, Column(Order = 1)]
public int CourseID { get; set; }
[ForeignKey("PersonID")]
public virtual Person Student { get; set; }
[ForeignKey("CourseID")]
public virtual Course StudentCourse { get; set; }
public int Mark { get; set; }
public string Comment { get; set; }
}

Just so you now why your code wasn't working as expected:
public class PersonCourse
{
[Key, Column(Order = 0)]
public int PersonID { get; set; }
[Key, Column(Order = 1)]
public int CourseID { get; set; }
public virtual Person Person { get; set; } // Follow built in conventions
public virtual Course Course { get; set; }
public int Mark { get; set; }
public string Comment { get; set; }
}
This now follows the built in Entity Framework convention of having
public int [ClassName][Id] or [Id] //Database Foreign Key column
and
public NavigationProperty [ClassName] // Navigation Property

Related

How do I define this relationship with code-first?

I'm trying to create a library of books.
I separated data and users context/database for security reasons (and clarity) but is it that useful?
Specially since BookOfUserEntity.Id should be really a composite key between UserId & BookId.
Something like :
public class BookOfUserEntity
{
[Key]
public Guid UserId { get; set; }
[Key]
public int BookId { get; set; }
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<BookOfUserEntity>()
.HasKey(b => new { b.UserId, b.BookId });
}
But then if I create a composite key what should be HistoryEntity.BookOfUserEntityId type & value?
I also separated BookEntity & BookOfUserEntity to avoid duplicated data and reuse what can be reused (MainCoverArtId etc...), but this added complexity - should I go back to a simpler model?
public class BookEntity
{
public int Id { get; set; }
public Guid? MainCoverArtId { get; set; }
...
public virtual List<BookOfUserEntity>? BookOfUserEntities { get; set; } // Only purpose is to know how many Users have this very book in their list.
}
public class BookOfUserEntity // Bad name here don't mention it
{
public int Id { get; set; }
public Guid UserId { get; set; }
public int BookId { get; set; }
public BookEntity Book { get; set; } // Useful? Should be virtual?
public List<HistoryEntity>? HistoryEntities { get; set; }
...
}
public class HistoryEntity
{
public int Id { get; set; }
public int BookOfUserEntityId { get; set; }
public BookOfUserEntity BookOfUserEntity { get; set; } // Same questions
public int Vol { get; set; }
public double Chapter { get; set; }
public DateTime? ReadDate { get; init; }
}

Passing more than one model to the view in ASP.NET MVC

I need to pass two models in the same view, however some elements have the same name.
I have two models Employee and HolidayRequestForm and I need to use both of these in the one view which will be a details page for each Employee.
Here is my Employee:
public partial class Employee
{
public int EmployeeID { get; set; }
public string FullName { get; set; }
public string EmailID { get; set; }
public string Password { get; set; }
public System.DateTime StartDate { get; set; }
public int RoleID { get; set; }
public int ShiftID { get; set; }
public int AreaID { get; set; }
public int DisciplineID { get; set; }
public int SiteID { get; set; }
public int ALCategory { get; set; }
public Nullable<int> HoursTaken { get; set; }
public Nullable<int> AwardedLeave { get; set; }
public Nullable<int> TotalHoursThisYear { get; set; }
public int HoursCarriedForward { get; set; }
public Nullable<int> EntitlementRemainingThisYear { get; set; }
public string Comments { get; set; }
}
Here is my HolidayRequestForm:
public partial class HolidayRequestForm
{
public int RequestID { get; set; }
public int EmployeeID { get; set; }
public System.DateTime StartDate { get; set; }
public System.DateTime FinishDate { get; set; }
public int HoursTaken { get; set; }
public string Comments { get; set; }
public int YearCreated { get; set; }
public int MonthCreated { get; set; }
public Nullable<int> DayCreated { get; set; }
public Nullable<int> YearOfHoliday { get; set; }
}
I have tried Creating a separate model that contains all elements to use in the view but I'm not sure how to differentiate elements with the same name eg. Comments Is it even possible to do so?
I would like to use both these models in my view as I'd like to create an Employee Profile page, with their info on the top displaying information about their profile and then holidays they have requested using the holidayrequestform in a table on the bottom of the page.
Write a ViewModel which will contain both Employee and HolidayRequestForm as follows and then pass the ViewModel to the view:
public class EmployeeViewModel
{
public Employee Employee {get; set;}
public HolidayRequestForm HolidayRequestForm {get; set;}
}
Then in your action method:
public ActionResult EmployeeDetails(int id)
{
Employee employee = _dbContext.Employees.FirstOrDefault(emp => emp.EmployeeID == id);
HolidayRequestForm holidayRequestForm = _dbContext.HolidayRequestForms.FirstOrDefault(hrf => hrf.EmployeeID == id);
EmployeeViewModel employeeViewModel = new EmployeeViewModel()
{
Employee = employee,
HolidayRequestForm = holidayRequestForm
}
return View(employeeViewModel);
}
Then in the view, access the model properties as follows:
#model EmployeeViewModel
<p>Full Name: #Model.Employee.FullName</p>

Code-First Referencing Table

I am currently struggling with some mapping properties on my models. Here are my two models.
What I am looking to do is only have unique PersonTypes (ie MD, Nurse) in my table and the person model reference these personTypes.
public partial class Person
{
public Person()
{
this.PersonTypes = new List<PersonType>();
this.Contacts = new List<Contact>();
}
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public int FacilityId { get; set; }
[DataType(DataType.Text), MaxLength(200), Required]
public string FirstName { get; set; }
[DataType(DataType.Text), MaxLength(200)]
public string MiddleName { get; set; }
[DataType(DataType.Text), MaxLength(200), Required]
public string LastName { get; set; }
public int? SpecialtyId { get; set; }
public bool IsEnabled { get; set; }
// Mapped Properties
[ForeignKey("FacilityId")]
public virtual Facility Facility { get; set; }
[ForeignKey("SpecialtyId")]
public virtual Specialty Specialty { get; set; }
public virtual ICollection<PersonType> PersonTypes { get; set; }
public virtual ICollection<Contact> Contacts { get; set; }
}
public partial class PersonType
{
public PersonType()
{
}
[Key, DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[DataType(DataType.Text), MaxLength(200), Required]
public string Name { get; set; }
public bool IsEnabled { get; set; }
}
Person1 = MD, Nurse - Person2 = MD - Person3 = Nurse, CNP
I don't want to have MD in my PersonType Table 2 times from above example. Is this possible. Thanks.
From your example I gather that you have many-to-many relationship between Persons and PersonTypes. For EF CodeFirst to understand this you have to create symmetric navigation property in you PersonType: public virtual ICollection<Person> Persons.

updating a record throws 'Multiplicity constraint violated.' in asp.net mvc

I am developing an application in asp.net mvc and using EF code first for my data access. Here is the model I use :
public class Culture
{
[Key()]
public int CultureID { get; set; }
[Required()]
[StringLength(250)]
public string CultureName { get; set; }
[Required()]
[StringLength(250)]
public string CultureDisplay { get; set; }
public virtual List<HomePage> HomePage { get; set; }
public virtual List<Person_Local> PersonLocal { get; set; }
}
public class Person
{
public int PersonID { get; set; }
[StringLength(250)]
public string PersonPicAddress { get; set; }
public virtual List<Person_Local> PersonLocal { get; set; }
}
public class Person_Local
{
//[NotMapped()]
[Key, Column(Order = 1)]
[ForeignKey("Person")]
public int PersonID { get; set; }
[Key, Column(Order = 2)]
[ForeignKey("Culture")]
public int CultureID { get; set; }
public string PersonName { get; set; }
public string PersonFamily { get; set; }
public string PersonAbout { get; set; }
public virtual Culture Culture { get; set; }
public virtual Person Person { get; set; }
}
I dont have any problem adding new person and personlocal to db. But when I want to update the person local, as below:
public ActionResult CreatePerson([Bind(Prefix = "Person")]Person obj,
[Bind(Prefix = "Person.PersonLocal")]IEnumerable<Person_Local> plocals)
{
string photo_guid = obj.PersonPicAddress;
if (obj.PersonID != 0)
{
Person p = da.Persons.FirstOrDefault(x => x.PersonID == obj.PersonID);
TryUpdateModel(p, "Person");
if (obj.PersonLocal[0].Person.PersonID != 0)
{
int cid = obj.PersonLocal[0].Culture.CultureID;
int pid = obj.PersonLocal[0].Person.PersonID;
Person_Local ploc =
da.Person_Locals.First(x => x.CultureID == cid && x.PersonID == pid);
//update ploc
}
da.SaveChanges();
}
}
I got following error :
Multiplicity constraint violated. The role 'Person_Local_Person_Target' of the relationship 'WebApp.Models.Person_Local_Person' has multiplicity 1 or 0..1.
Edited:
I commented
TryUpdateModel(p, "Person");
And the it seems the problem has been solved ?!! Why ?!

Entity Framework also Linq advice needed

i have got on my DB 3 tables
movies, workers, workermovies ( this is the Relationship table )
public class Movie
{
public Movie()
{
Genres = new List<Genre>();
Formats = new List<Format>();
ProductionCompanies = new List<ProductionCompany>();
Workers = new List<Worker>();
}
public int Id { get; set; }
public string Title { get; set; }
public DateTime ReleaseDate { get; set; }
public string StoryLine { get; set; }
public int RunTime { get; set; }
[ForeignKey("MPAARateId")]
public MPAARate MPAARate { get; set; }
public int MPAARateId { get; set; }
public byte[] ImageData { get; set; }
public string ImageMimeType { get; set; }
public DateTime CreatedDate { get; set; }
public string OfficialSite { get; set; }
public int Budget { get; set; }
public int StatusId { get; set; }
public virtual ICollection<Genre> Genres { get; set; }
public virtual ICollection<Format> Formats { get; set; }
public virtual ICollection<ProductionCompany> ProductionCompanies { get; set; }
public virtual ICollection<Worker> Workers { get; set; }
}
public class Worker
{
public int Id { get; set; }
public string FirstName { get; set; }
public string MiddleName { get; set; }
public string LastName { get; set; }
public DateTime Birthday { get; set; }
public string Biography { get; set; }
public string BornName { get; set; }
public double Height { get; set; }
public DateTime? Died { get; set; }
public byte[] ImageData { get; set; }
public string ImageMimeType { get; set; }
public bool IsActor { get; set; }
public bool IsDirector { get; set; }
public bool IsWriter { get; set; }
public bool IsProducer { get; set; }
public bool IsStar { get; set; }
public virtual ICollection<Movie> Movies { get; set; }
}
in this Relation i got the movieId and the workerId
but i also got some more fields if the person acted or writen or producer etc.
how do i define the relation entity class if needed
and when i want to get just the ppl that acted in the movie how do i wrote such a linq
query
You need to introduce an additional entity in your model WorkerMovie and convert the many-to-many relationship between Worker and Movie into two one-to-many relationships - one between Worker and WorkerMovie and the other between Movie and WorkerMovie. A sketch:
public class WorkerMovie
{
[Key, Column(Order = 0)]
public int WorkerId { get; set; }
[Key, Column(Order = 1)]
public int MovieId { get; set; }
public Worker Worker { get; set; }
public Movie Movie { get; set; }
public bool WorkedAsActor { get; set; }
public bool WorkedAsWriter { get; set; }
public bool WorkedAsProducer { get; set; }
// etc.
}
public class Movie
{
// ...
public virtual ICollection<WorkerMovie> WorkerMovies { get; set; }
// remove ICollection<Worker> Workers
}
public class Worker
{
// ...
public virtual ICollection<WorkerMovie> WorkerMovies { get; set; }
// remove ICollection<Movie> Movies
}
If you want only to find the workers who were actors in a particular movie with a movieId you can write:
var workersAsActors = context.WorkerMovies
.Where(wm => wm.MovieId == movieId && wm.WorkedAsActor)
.Select(wm => wm.Worker)
.ToList();
Here is another answer to a very similar question with many more examples of possible queries: Create code first, many to many, with additional fields in association table

Resources