I can't load Identity User using EF core 2.2

asp.net-core asp.net-identity c# entity-framework-core

Question

I'm building a system where I have the Order entity in which each one has a Sender. I'm using

  • ASP.Net core 2.2 (with Identity for managing the membership);
  • EF core 2.2.

The problem I'm facing is that I can't add the user entity using .Include(x => x.Sender). It is always returned as null.

This is the Order code:

public class Order
{
    public int Id { get; set; }
    public OrderStatusType CurrentOrderStatus { get; set; }
    public string PublicOrderId { get; set; }
    public Address PickupAddress { get; set; }
    public Address DropAddress { get; set; }

    public User Sender { get; set; }
    public User Receiver { get; set; }
    public User Creator { get; set; }

    public double ExpectedDistance { get; set; }
    public int ExpectedDuration { get; set; }
}

This is the User code:

public class User : IdentityUser
{
    public string FirastName { get; set; }
    public string LastName { get; set; }
    public string LastNumber { get; set; }
    public string Gander { get; set; }
    public Language PrefairdLanguage { get; set; }
    public string Themapnail { get; set; }
    public Address DefaultAddress { get; set; }
    public Store Store { get; set; }
    public int Imei { get; set; }
} 

This is the DbContext code:

public class MandoobContext : IdentityDbContext<User>
{
   // private readonly string connectionString;

    public MandoobContext(DbContextOptions<MandoobContext> options) : base(options)
    {

    }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        base.OnModelCreating(modelBuilder);

    }


    public DbSet<Order> Orders { get; set; }
    public DbSet<Address> Addresses { get; set; }    
}

This is the controller where I try to retrieve the data from the DB:

[HttpGet]
[Route("Order/{id:int}")]
public async Task<IActionResult> Order(int id)
{
    Order order = _context.Orders
        .Include(x => x.DropAddress)
        .Include(x => x.PickupAddress)
        .Include(x => x.Sender)
        .FirstOrDefault(x => x.Id == id);

    return View(order);
}

Thanks in advance for the help if anyone has an idea.

Regards.

1
1
11/30/2019 4:38:04 PM

Popular Answer

Are you sure the Sender is set on the database on the given Order? I've created a working application with the VS .net template and added your model to it and it just works.

You can find it on GitHub: https://github.com/tvdias/SO59114688.

1
11/30/2019 10:26:32 AM


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