Entity Framework Code First Foreign Key issue - c#

I have an EF Code First test app. I want to make one-to-many associations between 3 tables. I want to make a schema which looks like this http://gyazo.com/7a1800230a3838adecaafc5cb6676b25.png. When i launch my app VS says me:
The ForeignKeyAttribute on property 'EducationLevels' on type 'ConsoleApplication2.Employee' is not valid. The foreign key name 'EducationLevelId' was not found on the dependent type 'ConsoleApplication2.EducationLevel'. The Name value should be a comma separated list of foreign key property names.
Here it is my code:
class Program
{
static void Main(string[] args)
{
using (EmployeesContext context = new EmployeesContext())
{
Profession p = new Profession { Id = 0, NameOfProfession = "myprof" };
context.Profession.Add(p);
context.SaveChanges();
}
}
}
public enum Sex { Man = 0, Woman = 1 }
public class Employee
{
[Key]
public int Id { get; set; }
public string Name { get; set; }
public string Surname { get; set; }
public byte Age { get; set; }
public Sex Sex { get; set; }
public int EducationLevelId { get; set; }
public int ProfessionId { get; set; }
[ForeignKey("EducationLevelId")]
public virtual ICollection<EducationLevel> EducationLevels { get; set; }
[ForeignKey("ProfessionId")]
public virtual ICollection<Profession> Professions { get; set; }
}
public class EducationLevel
{
[Key]
public int Id { get; set; }
public string Level { get; set; }
public virtual Employee Employees { get; set; }
}
public class Profession
{
[Key]
public int Id { get; set; }
public string NameOfProfession { get; set; }
public virtual Employee Employees { get; set; }
}
public class EmployeesContext : DbContext
{
public DbSet<Employee> Employee { get; set; }
public DbSet<EducationLevel> EducationLevel { get; set; }
public DbSet<Profession> Profession { get; set; }
}

You need to swap collection and reference navigation properties (an Employee has one EducationLevel and one Profession, not many, and an EducationLevel has many Employees and not one, and a Profession has many Employees and not one):
public class Employee
{
// ...
public int EducationLevelId { get; set; }
public int ProfessionId { get; set; }
[ForeignKey("EducationLevelId")]
public virtual EducationLevel EducationLevel { get; set; }
[ForeignKey("ProfessionId")]
public virtual Profession Profession { get; set; }
}
public class EducationLevel
{
// ...
public virtual ICollection<Employee> Employees { get; set; }
}
public class Profession
{
// ...
public virtual ICollection<Employee> Employees { get; set; }
}

Related

Table Per Type(TPT) hierarchy vs Navigation in Entity Framework

I have been reading about Entity Framework during the pass few weeks. I came across TPT subject. I am a little bit confused and have difficulty to differentiate between TPT and Navigation. When should we choose one over another. Please take a look at the code below.
A. Navication
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public List<ContractEmployee> ContractEmployees { get; set; }
public List<PermanentEmployee> PermanentEmployees { get; set; }
}
public class ContractEmployee
{
public int HourlyWorked { get; set; }
public int HourlyPay { get; set; }
public Employee Employee { get; set; }
}
public class PermanentEmployee
{
public int AnnualSalary { get; set; }
public Employee Employee { get; set; }
}
B. TPT
[Table("Employee")]
public class Employee
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
}
[Table("ContractEmployee")]
public class ContractEmployee : Employee
{
public int HourlyWorked { get; set; }
public int HourlyPay { get; set; }
}
[Table("ContractEmployee")]
public class PermanentEmployee : Employee
{
public int AnnualSalary { get; set; }
}

navigation property is not loaded

I am using a objectDataSource to populate a gridview. I have 2 simple classes :
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public int Salary { get; set; }
public Department Department { get; set; }
}
and
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
public List<Employee> Employees { get; set; }
}
I also have
public class EmployeeDBContext : DbContext
{
public DbSet<Department> Departments { get; set; }
public DbSet<Employee> Employees { get; set; }
}
now in my EmployeeRepository class i have
public List<Department> GetDepartments()
{
EmployeeDBContext employeeDBContext = new EmployeeDBContext();
return employeeDBContext.Departments.Include("Employees").ToList();
}
Even though I have added .Include("Employees"), the employees are missing in gridview. what am I doing wrong here?
First of all, you will need to have a foreign key (DepartmentId) inside Employee class. I do not know how the video got away with that.
public class Employee
{
public int Id { get; set; }
public int DepartmentId { get; set; } <=====
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public int Salary { get; set; }
public virtual Department Department { get; set; }
^^^^^^^
}
public class Department
{
public int Id { get; set; }
public string Name { get; set; }
public string Location { get; set; }
public virtual ICollection<Employee> Employees { get; set; }
^^^^^^^^^^^^^^^^^^
}
public partial class EmployeeDBContext : DbContext
{
public virtual DbSet<Employee> Employee { get; set; }
public virtual DbSet<Department> Department { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
// Optional, but good practice to have the mapping here.
modelBuilder.Entity<Department>()
.HasMany(e => e.Employee)
.WithRequired(e => e.Department)
.HasForeignKey(e => e.DepartmentId);
}
}
- OR -
Add DepartmentId property and [ForeignKey] data annotation to Department.
public class Employee
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Gender { get; set; }
public int Salary { get; set; }
public int DepartmentId { get; set; } <===
// Optional, but good practice to have this data annotation attribute.
[ForeignKey("DepartmentId")] <===
public Department Department { get; set; }
}
FYI: You want to use virtual, in case someone wants to use lazy loading in the future.
have you tried something like this
return employeeDBContext.Departments.Include(x =>x.Employees ).ToList(); ?

How create Entity Framework with multiple foreign keys

I have an ASP.NET MVC project supporting multiple languages.
the tables (User,Gender,GenderTranslate) had nested inheritance
User inherit from Gender and Gender inherit from GenderTranslate
I have these tables
User table :
public partial class User
{
public int Id { get; set; }
public string Name { get; set; }
public Nullable<int> GenderID { get; set; }
public virtual Gender Gender { get; set; }
}
and Gender table:
public partial class Gender
{
public Gender()
{
this.GenderTranslates = new HashSet<GenderTranslate>();
this.Users = new HashSet<User>();
}
public int Id { get; set; }
public string Icon { get; set; }
public string Value { get; set; }
public virtual ICollection<GenderTranslate> GenderTranslates { get; set; }
public virtual ICollection<User> Users { get; set; }
}
and Language table:
public partial class Lang
{
public Lang()
{
this.GenderTranslates = new HashSet<GenderTranslate>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<GenderTranslate> GenderTranslates { get; set; }
}
and GenderTranslate table :
public partial class GenderTranslate
{
public int LangID { get; set; }
public int GenderID { get; set; }
public string Desc { get; set; }
public virtual Gender Gender { get; set; }
public virtual Lang Lang { get; set; }
}
When I get users I need to get GenderTranslate.Desc, not Gender.Value.
How can I do that using Entity Framework? Please help me

EF - Code First FOREIGN KEY constraint may cause cycles or multiple cascade paths

I have been up to trying to do some Entity Framework Code First, but I am stuck with the 'FOREIGN KEY constraint may cause cycles or multiple cascade paths.' problem.
Here are my classes:
public class Course
{
public Course()
{
this.Subject = new HashSet<Subject>();
this.Student = new HashSet<Student>();
}
public int Id { get; set; }
public string Name { get; set; }
public string Shift { get; set; }
public int Room { get; set; }
public virtual ICollection<Subject> Subject { get; set; }
public virtual ICollection<Student> Student { get; set; }
}
public class Subject
{
public Subject()
{
this.Deliverable = new HashSet<Deliverable>();
}
public int Id { get; set; }
public string Name { get; set; }
public int StartsAt { get; set; }
public int FinishesAt { get; set; }
public System.TimeSpan Weekdays { get; set; }
public int CourseId { get; set; }
public int TeacherId { get; set; }
public virtual Course Course { get; set; }
public virtual Teacher Teacher { get; set; }
public virtual ICollection<Deliverable> Deliverable { get; set; }
}
public class Student : Person
{
public Student()
{
this.Deliverable = new HashSet<Deliverable>();
}
public decimal Average { get; set; }
public int CourseId { get; set; }
public virtual Course Course { get; set; }
public virtual ICollection<Deliverable> Deliverable { get; set; }
}
public class Teacher : Person
{
public Teacher()
{
this.Subject = new HashSet<Subject>();
}
public virtual ICollection<Subject> Subject { get; set; }
}
public class Deliverable
{
public int Id { get; set; }
public string Name { get; set; }
public decimal Mark { get; set; }
public System.DateTime DeliveredDate { get; set; }
public bool Delivered { get; set; }
public System.DateTime AnnounceDate { get; set; }
public int SubjectId { get; set; }
public int StudentId { get; set; }
public virtual Subject Subject { get; set; }
public virtual Student Student { get; set; }
}
I think it's a reference looping error, but I can't realize the approach on how to resolve it. I'm using Web API and I'm able to change the model, so feel free to modify it please. Could this be resolved using FluentAPI?
Here is the exception. It is thrown the first time I have executed the application:
'Introducing FOREIGN KEY constraint 'FK_dbo.Students_dbo.Courses_CourseId' on table 'Students' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.'
You have to use Fluent-API to disable delete / update. To do this, modify the OnModelCreating method:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Course>().HasMany(c => c.Student).WillCascadeOnDelete(false);
}
I'm not sure if it's the Course class or the Student class who causes issues, if this is not working, try to do a "WillCascadeOnDelete(false)" on your Student class.

How to Map many one-to-many relationship in ASP.NET MVC?

I have few Domain Models - Address, Customer, Employee, StoreLocation. Address has many to one relationship with Customerand Employee and one to one relationship with StoreLocation.
public class Address
{
public int Id;
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
}
public class Customer
{
public int Id { get; set; }
public string Name { get; set; }
public IList<Address> Addresses { get; set; }
}
public class StoreLocation
{
public int Id { get; set; }
public string ShortCode { get; set; }
public string Description { get; set; }
public Address Address { get; set; }
}
public class Employee
{
public int Id { get; set; }
public string Name { get; set; }
public DateTime Dob { get; set; }
public IList<Address> Addresses { get; set; }
}
How to Map this relationship?. I am using ASP.NET MVC 3.0 and Entity Framework 4.1.
If you are using code-first (I think you want this, else, you have to edit your Q), the first way is the way explained below:
Entities:
public class Address {
[Key]
public int Id { get; set; }
public string Line1 { get; set; }
public string Line2 { get; set; }
public string Line3 { get; set; }
public virtual Customer Customer { get; set; }
public virtual StoreLocation StoreLocation { get; set; }
public virtual Employee Employee { get; set; }
public int? CustomerId { get; set; }
public int? EmployeeId { get; set; }
}
public class Customer {
[Key]
public int Id { get; set; }
public string Name { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
}
public class StoreLocation {
[Key]
public int Id { get; set; }
public string ShortCode { get; set; }
public string Description { get; set; }
public virtual Address Address { get; set; }
}
public class Employee {
[Key]
public int Id { get; set; }
public string Name { get; set; }
public DateTime Dob { get; set; }
public virtual ICollection<Address> Addresses { get; set; }
}
DbContext inherited class:
public class ManyOneToManyContext : DbContext {
static ManyOneToManyContext() {
Database.SetInitializer<ManyOneToManyContext>(new ManyOneToManyInitializer());
}
public DbSet<Address> Addresses { get; set; }
public DbSet<Customer> Customers { get; set; }
public DbSet<StoreLocation> StoreLocations { get; set; }
public DbSet<Employee> Employees { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder) {
modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
modelBuilder.Entity<Customer>().HasMany(c => c.Addresses).WithOptional(a => a.Customer).HasForeignKey(a => a.CustomerId);
modelBuilder.Entity<StoreLocation>().HasRequired(s => s.Address).WithOptional(a => a.StoreLocation).Map(t => t.MapKey("AddressId"));
modelBuilder.Entity<Employee>().HasMany(e => e.Addresses).WithOptional(a => a.Employee).HasForeignKey(e => e.EmployeeId);
}
}
Context Initializer:
public class ManyOneToManyInitializer : DropCreateDatabaseAlways<ManyOneToManyContext> {
protected override void Seed(ManyOneToManyContext context) {
}
}
That will create the db-schema below:
Let me know if you have any questions or need clarifications on any part.

Categories

Resources