Optional One to Many relationships in Entity Framework

Mohammadmk
 
on Sep 08, 2018 03:59 AM
5085 Views

Hello every one;

can we have optional one to many relationship in entityframework

 Note: a person can be realate to a department or it might no and a Department can have many person

public class Person
{
    public int Id{ get; set; }
    
    public Department Department { get; set; }
    public int DepartmentId
}

public class Department
{
    public int Id{ get; set; }
    
    public List<Person> Members { get; set; }
}

Person to Department

.HasOptional(m => m.Department)
.WithOptional( d => d.Members)
.HasForeignKey( m=> m.DepartmentId);

 For Example

Id                     Name                    DepartmentId

1                       John                      x

2                       Ahmad                   y

3                       Persony                NULL

4                       Personz                  x

thanks

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Andrea
 
on Sep 08, 2018 03:59 AM
dharmendr
 
on Sep 10, 2018 04:49 AM