1

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.

2
  • Not related to the question, but you should avoid using FirstOrDefault on your controller and use FirstOrDefaultAsync instead.
    – tvdias
    Nov 30, 2019 at 10:32
  • 1
    I could also get the user successfully.Could you share the data in your database?And what's the url that you send request?
    – Rena
    Dec 2, 2019 at 6:26

2 Answers 2

1

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.

2
  • 1
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - From Review
    – Tân
    Nov 30, 2019 at 12:30
  • 1
    Thanks @Tân. There's no answer on the link, but a way for anyone to replicate the issue. Since I was able to get it working with the VS template + the code on the question there's no code to add to the answer. I believe it's a data issue...
    – tvdias
    Nov 30, 2019 at 13:01
0

I get to resolve the problem by updating to .net Core 3.0 and Ef core3.0. I don't know exactly what was going wrong but the update fixed it.

Regards

1
  • If you believe it is the best answer, you can accept it.
    – tvdias
    Dec 2, 2019 at 22:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.