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.
FirstOrDefault
on your controller and useFirstOrDefaultAsync
instead.