Entity Framework retrieve objects based in condition - c#

I am developing an Web Api application using Entity Framework where I have these two models and my DbContext class:
public class Course
{
[Key]
public int CourseId { get; set; }
[Required]
public string CourseName{ get; set; }
public ICollection<Students> Students { get; set; }
}
public class Students
{
[Key]
public int StudentId { get; set; }
[Required]
public string StudentName{ get; set; }
[Required]
public int StudentAge{ get; set; }
}
//My CourseContext
public class CourseContext : DbContext
{
public CourseContext () : base("CourseDB") { }
public DbSet<Course> Courses { get; set; }
public DbSet<Students> Students { get; set; }
}
I am modeling a WebAPi with these models above and I need to list all students that are taking a course which means that are added to the ICollection Students property in Course Class. I have tried to develop a solution for that but I have no idea how to do that since I do not have a foreign key property between the models.
...
using (var course_db = new CourseContext ()){
/*but this return all students and repeated ones since one student
can be in more than one course*/
var students= course_db.Course.Include("Students").Select(x=> x.Students);
if (students== null){
return Json(new { success = false });
}
return Json(students, JsonRequestBehavior.AllowGet);
}
...
I have tried the code below but it did not work. I just want to retrieve the students that are enrolled in a course, since some of them are not. Can someone help me?

You need to further build relationships in your model. The student to course relationship should be a many-to-many relationship which you are missing the proper properties in each but also a joining table and appropriate model builder (context) configuration.
Override your dbcontext model creation method to add your fluent configuration:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
//Build many-to-many relationships
modelBuilder.Entity<Student>()
.HasMany<Course>(c => c.Courses)
.WithMany(s => s.Student)
.Map(pe =>
{
pe.MapLeftKey("Student_ID");
pe.MapRightKey("Course_ID");
pe.ToTable("StudentCoursesTable");
});
}
Add missing Courses from your Student class:
public class Students
{
public ICollection<Courses> Courses { get; set; }
}

Related

How do I create a review entity as a child of a parent entity in Entity Framework [duplicate]

I have two entities in my MVC application and I populated the database with Entity Framework 6 Code First approach. There are two city id in the Student entity; one of them for BirthCity, the other for WorkingCity. When I define the foreign keys as above an extra column is created named City_ID in the Student table after migration. Id there a mistake or how to define these FKs? Thanks in advance.
Student:
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int BirthCityID { get; set; }
public int LivingCityID { get; set; }
[ForeignKey("BirthCityID")]
public virtual City BirthCity { get; set; }
[ForeignKey("LivingCityID")]
public virtual City LivingCity { get; set; }
}
City:
public class City
{
public int ID { get; set; }
public string CityName { get; set; }
public virtual ICollection<Student> Students { get; set; }
}
To achieve what you want you need to provide some aditional configuration.Code First convention can identify bidirectional relationships, but not when there are
multiple bidirectional relationships between two entities.You can add configuration (using Data Annotations or the Fluent API) to present this
information to the model builder. With Data Annotations, you’ll use an annotation
called InverseProperty. With the Fluent API, you’ll use a combination of the Has/With methods to specify the correct ends of these relationships.
Using Data Annotations could be like this:
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int BirthCityID { get; set; }
public int LivingCityID { get; set; }
[ForeignKey("BirthCityID")]
[InverseProperty("Students")]
public virtual City BirthCity { get; set; }
[ForeignKey("LivingCityID")]
public virtual City LivingCity { get; set; }
}
This way you specifying explicitly that you want to relate the BirthCity navigation property with Students navigation property in the other end of the relationship.
Using Fluent Api could be like this:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().HasRequired(m => m.BirthCity)
.WithMany(m => m.Students).HasForeignKey(m=>m.BirthCityId);
modelBuilder.Entity<Student>().HasRequired(m => m.LivingCity)
.WithMany().HasForeignKey(m=>m.LivingCityId);
}
With this last solution you don't need to use any attibute.
Now, the suggestion of #ChristPratt in have a collection of Student in your City class for each relationship is really useful. If you do that, then the configurations using Data Annotations could be this way:
public class Student
{
public int ID { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public int BirthCityID { get; set; }
public int LivingCityID { get; set; }
[ForeignKey("BirthCityID")]
[InverseProperty("BirthCityStudents")]
public virtual City BirthCity { get; set; }
[ForeignKey("LivingCityID")]
[InverseProperty("LivingCityStudents")]
public virtual City LivingCity { get; set; }
}
Or using Fluent Api following the same idea:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Student>().HasRequired(m => m.BirthCity)
.WithMany(m => m.BirthCityStudents).HasForeignKey(m=>m.BirthCityId);
modelBuilder.Entity<Student>().HasRequired(m => m.LivingCity)
.WithMany(m => m.LivingCityStudents).HasForeignKey(m=>m.LivingCityId);
}
Sheesh. It's been a long day. There's actually a very big, glaring problem with your code, actually, that I completely missed when I commented.
The problem is that you're using a single collection of students on City. What's actually happening here is that EF can't decide which foreign key it should actually map that collection to, so it creates another foreign key specifically to track that relationship. Then, in effect you have no navigation properties for the collections of students derived from BirthCity and LivingCity.
For this, you have to drop down to fluent configuration, as there's no way to configure this properly using just data annotations. You'll also need an additional collection of students so you can track both relationships:
public class City
{
...
public virtual ICollection<Student> BirthCityStudents { get; set; }
public virtual ICollection<Student> LivingCityStudents { get; set; }
}
Then, for Student:
public class Student
{
...
public class StudentMapping : EntityTypeConfiguration<Student>
{
public StudentMapping()
{
HasRequired(m => m.BirthCity).WithMany(m => m.BirthCityStudents);
HasRequired(m => m.LivingCity).WithMany(m => m.LivingCityStudents);
}
}
}
And finally in your context:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new Student.StudentMapping());
}

Mapping entity framework model to multiple tables

How can I map an entity framework model to multiple tables?
How to perform insertion operation to specific table (by reference of string which stores the table name)?
I have not implemented this but a quick search provides many good examples of a practice known as Entity Splitting. The following should be useful:
http://www.c-sharpcorner.com/UploadFile/ff2f08/entity-splitting-in-entity-framework-6-code-first-approach/
public partial class Employee
{
// These fields come from the “Employee” table
public int EmployeeId { get; set; }
public string Code { get; set; }
public string Name { get; set; }
// These fields come from the “EmployeeDetails” table
public string PhoneNumber { get; set; }
public string EmailAddress { get; set; }
}
public partial class Model : DbContext
{
public Model() : base("name=EntityModel")
{
Database.Log = Console.WriteLine;
}
public virtual DbSet<Employee> Employees { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Employee>()
.Map(map =>
{
map.Properties(p => new
{
p.EmployeeId,
p.Name,
p.Code
});
map.ToTable("Employee");
})
// Map to the Users table
.Map(map =>
{
map.Properties(p => new
{
p.PhoneNumber,
p.EmailAddress
});
map.ToTable("EmployeeDetails");
});
}
}
All credit for the above code goes to linked post
In this case you can implement your own IModelCacheKeyFactory, which allow to hook into the model caching mechanism so EF is able to create different models based on some value right in runtime.
This article explains how

Multiple relationships to single table Entity Framework asp.net MVC

I've just started using Entity Framework for my next project and I'm struggling with the following. I have the following ApplicationUser class:
public class ApplicationUser : IdentityUser
{
public string Name { get; set; }
public int CompanyId { get; set; }
public virtual Company Company { get; set; }
}
I have two classes that inherent from this class:
public class TrainerUser : ApplicationUser
{
public virtual ICollection<ClientUser> Clients { get; set; }
}
public class ClientUser : ApplicationUser
{
public string TrainerId { get; set; }
public TrainerUser Trainer { get; set; }
}
The company class looks like this:
public class Company
{
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<TrainerUser> Trainers { get; set; }
public virtual ICollection<ClientUser> Clients { get; set; }
}
What I can't figure out is how I can use the fluent API to not include 3 different companyId columns in the ApplicationUsers table.
Currently I have the following fluent API configuration:
modelBuilder.Entity<TrainerUser>().HasRequired(c => c.Company).WithMany(t => t.Trainers).HasForeignKey(c => c.CompanyId);
modelBuilder.Entity<ClientUser>().HasRequired(c => c.Company).WithMany(c => c.Clients).HasForeignKey(c => c.CompanyId);
Can anyone point me in the right direction?
Try adding these to your code.
modelBuilder.Entity<TrainerUser>().ToTable("TrainerUser");
modelBuilder.Entity<ClientUser>().ToTable("ClientUser");
If I am getting you right. you are trying to create a structure representing Table Per Hierarchy (TPT). Read more about it at the link.
Basically what happens is when entity framework encounters inheritance in the entities. Its Default attempt to create tables is by creating column of the set of all properties of all the derived entities from a class with a discriminator column.
What you are trying to create is a separate table for every class in the hierarchy.

How should I model my tables so they work well with Entify Framework?

So I'm trying to model the following using Entity Framework (code first).
public class School
{
public string Name { get; set; }
public IEnumerable<Class> Classes { get; set; }
}
public class Class
{
public string Name { get; set; }
public TypeOfClass Type { get; set; }
public IEnumerable<Student> Students { get; set; }
}
public enum TypeOfClass
{
Kindergarten,
HighSchool
}
public class Student
{
public string Name { get; set; }
}
public class Kid : Student
{
public string FavouriteToy { get; set; }
}
public class Teenager : Student
{
public int AmountOfAcne { get; set; }
}
I'm wondering how I should model my Entities (and tables) so that I can do something like this to select all Students in the School:
var school = new School();
var kindergartenClass = new Class
{
Name = "Kindergarten",
Type = TypeOfClass.Kindergarten,
Students = new List<Kid>()
};
var highschoolClass = new Class
{
Name = "Kindergarten",
Type = TypeOfClass.HighSchool,
Students = new List<Teenager>()
};
school.Classes = new List<Class> {kindergartenClass, highschoolClass};
IEnumerable<Student> students = school.Classes.SelectMany(x => x.Students);
I wan't to have separate tables for Kids and Teenagers since they have different properties and will only share a subset och common properties.
Anyone with good advice? :)
First off, your classes should have properties that can be recognized by EF as primary keys. Normally these would be non-nullable ints with a name of Id or [ClassName]Id:
public class School
{
public int Id { get; set; } // or SchoolId
}
public class Class
{
public int Id { get; set; } // or ClassId
}
public class Student
{
public int Id { get; set; } // or StudentId
}
You have two options for mapping Kids and Teenagers to separate tables:
Table per Type (TPT) - base class (in this case Student) gets its own table while Kids and Teenagers get one table each with foreign key associations back to the Student table.
Table per Concrete Type (TPC) - Kid and Teenager get their own tables and all the properties from the base class (in this case the Name property from the Student class) get duplicated across all child tables.
I think what you're after is TPC. Also, I think you only want Kids and Teenagers to have tables (not Students). If so, you would need to make the Student class abstract:
public abstract class Student
{
// ...
}
And you would need to override the OnModelCreating method in your context:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Kid>().Map(m =>
{
m.ToTable("Kids");
m.MapInheritedProperties();
});
modelBuilder.Entity<Teenager>().Map(m =>
{
m.ToTable("Teenagers");
m.MapInheritedProperties();
});
}
You will need an Id (Key) column. And the navigation properties for collections should be ILIst<>.
public class School
{
// Id and SchoolId are automatically recognized
public int SchoolId { get; set; }
public string Name { get; set; }
// use an IList so that you can Add()
public IList<Class> Classes { get; set; }
}
for the inherited classes you will have to pick a TPC/TPT/TPH model

How do I create a Many-to-Many relationship with only one entity?

Courses have many prerequisites, and simultaneously a particular course can be a prerequisite for many courses. I've tried to establish the many-to-many relationship (in OnModelBCreating) using EF code-first with the following:
modelBuilder.Entity<Course>()
.HasMany(e => e.Prerequisites)
.WithMany(e => e.Postrequisites)
.Map(m => m.ToTable("CourseRequisiteMappings")
.MapLeftKey("CourseId").MapRightKey("CourseId")); // EDIT: THIS LINE IS THE PROBLEM. SEE MARKED ANSWER AND MY COMMENT ON IT.
Also, here is the Course class:
public class Course
{
public int CourseId { get; set; }
public string Name { get; set; }
public string InstitutionCode { get; set; }
public string Description { get; set; }
public bool IsElective { get; set; }
public virtual ICollection<Instructor> Instructors { get; set; }
public virtual ICollection<Student> Students { get; set; }
public virtual ICollection<Module> Modules { get; set; }
public virtual ICollection<Course> Prerequisites { get; set; }
public virtual ICollection<Course> Postrequisites { get; set; }
}
When I implemented this and went to update the database, it gave me the following errors:
CourseId: Name: Each property name in a type must be unique. Property
name 'CourseId' is already defined.
ModuleId: Name: Each property name in a type must be unique. Property
name 'ModuleId' is already defined.
CourseCourse: EntityType: EntitySet 'CourseCourse' is based on type
'CourseCourse' that has no keys defined.
ModuleModule: EntityType: EntitySet 'ModuleModule' is based on type
'ModuleModule' that has no keys defined.
I could not find an example of doing this which leads me to believe one of the following three are true:
There's a different way of accomplishing this that I don't see
I'm on the right track but overlooking something due to my lack of knowledge with EF
I'm the first one to try and EF doesn't support this (very unlikely)
First, does anyone know how I can set up this relationship, i.e., what do these errors mean (responding to #2)? For bonus points, is there another way of doing this that might be better or worse (kinda #1)? Thanks in advance.
Your mapping is nearly correct. But you have to understand that under the hood the Entity Framework wil create a so callled junction table that stores the many to many relationship.
This junction table wil just have two fields, containing the foreign keys which together make up the primary key. Obviously these foreign keys cannot have the same name.EF is smart enough to all figure it out by itself and no maping is necessary. Below a working example:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
namespace ManyToManyUnderTheHoodSpike
{
class Program
{
static void Main(string[] args)
{
Database.SetInitializer(new DropCreateDatabaseAlways<CourseContext>());
using (CourseContext context=new CourseContext())
{
context.Courses.Add(new Course("Top of the bill")
{
PrerequisiteCourses = new List<Course>()
{
new Course("My two cents"),
new Course("Counting to two")
}
});
context.SaveChanges();
}
}
}
public class CourseContext : DbContext
{
public DbSet<Course> Courses { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
}
}
public class Course
{
public Course() { }
public Course(string name)
{
Name = name;
}
public string Name {get;set;}
public int CourseId{get;set;}
public ICollection<Course> PrerequisiteCourses{get;set;}
public ICollection<Course> FollowUpCourses{get;set;}
}
}
If you run this code you get a database with two tables: Courses and CourseCourses with as the only fields Course_Id and Course_Id1.
But that is not very readable, so let's make the mapping to make it more readable:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Course>().HasMany(course => course.PrerequisiteCourses)
.WithMany(course => course.FollowUpCourses)
.Map(data => data.ToTable("Prerequisites")
.MapLeftKey("FollowUpId")
.MapRightKey("PrerequisiteId"));
}
Presto!
I would model like this. I know you wanted only 1 table. But Ef will create the many to many table if you dont. Not sure what you didnt get right without testing. So anyway, here is another option.
public class Course
{
public int CourseId { get; set; }
public string Name { get; set; }
public string InstitutionCode { get; set; }
public string Description { get; set; }
public bool IsElective { get; set; }
//nav elements
public virtual ICollection<Instructor> Instructors { get; set; }
public virtual ICollection<Student> Students { get; set; }
public virtual ICollection<Module> Modules { get; set; }
public virtual ICollection<PreReqCourse> Prerequisites { get; set; }
// You can Find follow on courses, by accessing PreReqCourse table, but if you felt this navigation offered enough value, create a post req table too. Using same approach.
// public virtual ICollection<Course> Postrequisites { get; set; }
}
public class PreReqCourse
{
public virtual int Id {get; set;}
public virtual int CourseId { get; set; }
public virtual Course PreReqForCourse { get; set; } //Nav prop
}
modelBuilder.Entity<Course>()
.HasMany(e => e.Prerequisites)
.WithMany();
// Leave WithMany empty. You can define in PreReqCourse Table model, you dont need to model from both directions.
modelBuilder.Entity<PreReqCourse>()
.HasRequired(e => e.PreReqForCourse)
.HasForeignKey(f => f.CourseId)
.WithMany(p=>p.PreRequisites);

Categories