Hello, I'm trying to make a ToDo List on Asp.net 5.
There are two models with a one-to-many connection.

using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Threading.Tasks; namespace tdl_api.Models { public class Reminder { [Key] public long Id { get; set; } public string Title { get; set; } public DateTime? Create_date { get; set; } public string Body { get; set; } public bool Status { get; set; } public long? TypeId { get; set; } public virtual Type Type { get; set; } } } using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.ComponentModel.DataAnnotations; namespace tdl_api.Models { public class Type { [Key] public long Id { get; set; } public string Name { get; set; } public virtual ICollection<Reminder> Reminders { get; set; } public Type() { Reminders = new HashSet<Reminder>(); } } } 

Also data context

 using Microsoft.Data.Entity; using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; namespace tdl_api.Models { public class Contex: DbContext { public DbSet<Reminder> reminders { get; set; } public DbSet<Type> types { get; set; } protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity<Reminder>(e => { e.HasOne(r => r.Type).WithMany(t => t.Reminders); }); base.OnModelCreating(modelBuilder); } } } 

In the repository I get this:

 List<Reminder> data = await _db.reminders.ToListAsync(); 

But in Reminder objects, all Type is null (not pull-up). What missed? Did about how here.

  • Which approach was used: BaseFirst or ModelFirst? - R0manych
  • one
    I think CodeFirst, ef7 seems to support it exclusively? Well, I wrote classes (models) and made migration - Vladimir Zryachikh
  • And the base itself did not look? It seems to me that there is not enough data to understand what is happening. - R0manych
  • and if you do Include() i.e. obviously load the necessary entities? PS: Entity Framework 7 has not yet felt, but at 6 it works like this - Bald
  • If you use Include () server throws error 502.3 - Vladimir Zryachikh

1 answer 1

Try to set a clear connection.

 e.HasOne(r => r.Type).WithMany(t => t.Reminders) .HasForeignKey(r => r.TypeId).WillCascadeOnDelete(false); 

If cascade deletion is necessary, then instead of false we set true