Zero or One To Many in Entity Framework

c# entity-framework entity-framework-6

Popular Answer

Using Data Annotations, you can specify the relationship as follows:

public class Task
{
    public int TaskID { get; set; }

    public int? PeriodicTaskID { get; set; }
    [ForeignKey("PeriodicTaskID ")]
    public virtual PeriodicTask PeriodicTask { get; set; }
}

via Fluent API;

protected override void OnModelCreating(ModelBuilder modelBuilder)
{
    //..
    modelBuilder.Entity<Task>()
        .HasOne(t => t.PeriodicTask)
        .WithMany(pt => pt.Tasks);
}
0
9/5/2018 9:26:01 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow