I'm tyring to add an entity to DbSet and save changes in context in Entity Framework Code First. I have University DbSet:
public DbSet<University> Universities { get; set; }
I have 5 class in use - Term; Faculty; Department; University; and Student. Their constructor methods are like:
public Student()
{
Universities = new List<University>();
User = new DATABASE.User();
Terms = new List<Term>();
}
public University()
{
UniversityFaculties = new List<Faculty>();
UniversityDepartments = new List<Department>();
Students = new List<Student>();
}
public Term()
{
Students = new List<Student>();
Lessons = new List<Lesson>();
university = new University();
}
public Faculty()
{
University = new University();
Students = new List<Student>();
}
public Department()
{
University = new University();
Students = new List<Student>();
}
I get a Student entity from previous forms,(student which logged in the program). When I'm trying to add entities that filled by comboboxes and textboxes, I realized that Universities DbSet is filling with the real entity that I meant to add but is also filling with an empty University entity. My Id values are not incrementing automatically so I'm incrementing them manually. My code:
Create Faculty Entity:
Faculty f = entities.Faculties.OrderByDescending(o => o.FacultyId).FirstOrDefault();
int newFacultyId = (f == null ? 1 : f.FacultyId + 1);
var faculty = new Faculty();
faculty.FacultyId = newFacultyId;
faculty.FacultyName = FacultyComboBox2.SelectedItem.ToString();
Create Department Entity:
Department d = entities.Departments.OrderByDescending(o =>o.DepartmentId).FirstOrDefault();
int newDepartmentId = (d == null ? 1 : d.DepartmentId + 1);
var department = new Department();
department.DepartmentId = newDepartmentId;
department.DepartmentName = DepartmentComboBox3.SelectedItem.ToString();
Create University Entity:
University uni = entities.Universities.OrderByDescending(o => o.UniversityId).FirstOrDefault();
int newUniId = (uni == null ? 1 : uni.UniversityId + 1);
var university = new University();
university.UniversityId = newUniId;
university.UniversityName = UniversityComboBox1.Text;
university.UniversityFaculties.Add(faculty);
university.UniversityDepartments.Add(department);
university.Students.Add(student);
student.Universities.Add(university);
faculty.University = university;
department.University = university;
faculty.Students.Add(student);
department.Students.Add(student);
Create Term Entity:
Term t = entities.Terms.OrderByDescending(o => o.TermId).FirstOrDefault();
int newTermId = (t == null ? 1 : t.TermId + 1);
var term = new Term();
term.TermId = newTermId;
term.TermTerm = int.Parse(TermComboBox1.Text);
term.TermYear = int.Parse(YearComboBox1.Text);
term.Students.Add(student);
student.Terms.Add(term);
term.university = university;
And my DbSet Additions:
entities.Faculties.Add(faculty);
entities.Departments.Add(department);
entities.Universities.Add(university);
entities.Terms.Add(term);
entities.SaveChanges();
But I got validation failed error so I used try catch blocks to analyze exception. I realized that my Universities DbSet contains an empty University entity. When I'm debugging, I realized that they're coming from entity creations that I make for the purpose of determining the ID of the entities. I tried to reach the null values in Universities DbSet and remove them but I couldn't do it in that way.The screenshot is below:
Other DbSet's don't have that problem.I don't know what to do to get rid of that problem.Any helps would be appreciate.Thanks.