this is my dbcontext
public partial class TestModel : DbContext
{
public TestModel()
: base("name=TestModel")
{
}
public virtual DbSet<Department> Departments { get; set; }
public virtual DbSet<Employee> Employees { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Department>()
.Property(e => e.DepartmentName)
.IsUnicode(false);
modelBuilder.Entity<Department>()
.HasMany(e => e.Employees)
.WithOptional(e => e.Department)
.HasForeignKey(e => e.DeptID);
modelBuilder.Entity<Employee>()
.Property(e => e.Name)
.IsUnicode(false);
}
}Department and Employee has one-to-many relationship.
1) see this code
modelBuilder.Entity<Department>()
.Property(e => e.DepartmentName)
.IsUnicode(false);a) what is the meaning of this line .Property(e => e.DepartmentName) and this line .IsUnicode(false);
2) see this code
modelBuilder.Entity<Department>()
.HasMany(e => e.Employees)
.WithOptional(e => e.Department)
.HasForeignKey(e => e.DeptID);a) what hasmany does .HasMany(e => e.Employees)
b) what withoptional does
c) department has no FK then why EF code generator use this .HasForeignKey(e => e.DeptID); ? actually employees table has FK.
please discuss about the above points in details.
thanks