Dmitriy Gavrilenko
Dmitriy Gavrilenko

Reputation: 310

Entity Framework Core does not save related data

In continuation of yesterday's post

Two Entities

public class Realtor
{
    public Realtor()
    {
        Guid = Guid.NewGuid();
        Registration = DateTime.Now;
    }
    public int Id { get; set; }
    public Guid Guid { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime Registration { get; set; }
    public int SubdivId { get; set; }
    public Subdiv Subdiv { get; set; }
}

public class Subdiv
{
    public Subdiv()
    {
        Created = DateTime.Now;
    }
    public int Id { get; set; }
    public string Name { get; set; }
    public DateTime Created { get; set; }
    public List<Realtor> Realtors { get; set; }
}

I spend test

  1. I added one Subdiv (TOSTER TM) and received his ID
  2. Next, I add a Realtor and I push Subdiv property found on the ID, the newly created TOSTER TM Realtor.Subdiv is an object of type Subdiv. OK. enter image description here

  3. Then I try to select from the base the newly added Realtor. Realtor.Subdiv = null OMG!! enter image description here

  4. We get Subdiv object, which is lacking in Realtor above and see his List<Realtor> = null

enter image description here

Please help in solving this problem.

Upvotes: 6

Views: 12772

Answers (3)

Aamir
Aamir

Reputation: 695

Entity Framework core allows to save related entities, you need to define Fluent API settings for both Master and detail table

   public class Order     
   { 
        public int Id { get; set; }
        public int AddressId { get; set; }
        public string DeliveryNotes { get; set; }
        public int PurchaseOrderNo { get; set; }
        public virtual  ICollection<OrderItem> Items { get; set; }
     }

    public class OrderItem 
    {
        public int Id { get; set; }
        public string ProductName { get; set; }
        public int Quantity { get; set; }
        public decimal UserPrice { get; set; }
        public string Comment { get; set; }    
        [ForeignKey("OrderId ")]
        public int OrderId { get; set; }    
        public virtual Order Order { get; set; }
     }

Inside the DBConext OnModelCreating method , define the relation like, it will make sure when you have order object, its related or child objects i.e Enrolments will also be saved

modelBuilder.Entity<Order>()
                .HasMany(c => c.Items)
                .WithOne(e => e.Order);
modelBuilder.Entity<OrderItem>().Ignore(x => x.Order);

Now your code should look like this

 Order _order = new Order{ AddressId = 1, DeliveryNotes ="some notes", PurchaseOrderNo =1};
 _order.Items = new List< OrderItem>();
 _ordert.Items.add(new OrderItem{ ProductName =”Laptop”, Quantity =1, UserPrice =1500.00, Comment =”some testing comments”});
 repository.order.insert(_order); 
 repository.save();

Upvotes: 1

Glauco Cucchiar
Glauco Cucchiar

Reputation: 774

Try this:

Relator rl = Context.Relators.Include(r => r.Subdiv).First(s => s.Id == id);

Now you can access to Subdiv property

For more related date you can call Include Methods more times:

Relator rl = Context.Relators
                    .Include(r => r.Subdiv)
                    .Include(r => r.AnotherRel)
                    .First(s => s.Id == id);

For Entities with multiple levels in depth:

If Subdir is a collection

Relator rl = Context.Relators
                    .Include(r => r.Subdiv)
                    .ThenInclude(sub => sub.SecondLevelDepth)
                    .First(s => s.Id == id);

if Subdir is an Entity

Relator rl = Context.Relators
                    .Include(r => r.Subdiv.Select(s => s.SecondLevelDepth)
                    .First(s => s.Id == id);

Upvotes: 5

Ivan Stoev
Ivan Stoev

Reputation: 205849

The problem is not with saving the related data (it should be saved correctly, you could check that inside the database), but loading it.

EF Core currently does not support lazy loading, so in order to get the related data you need to explicitly request it (the so called eager loading):

Realtor rl = context.Realtors.Include(r => r.Subdiv).First(r => r.Id == id);

For more info, see EF Core: Loading Related Data.

Upvotes: 4

Related Questions