Error getting products from the logged in user. ASP.NET Core MVC

Edrian Biagi

I'm having trouble getting only products purchased by the logged in user, that is, the user who made the purchase.

I have a Users table that contains general registration data, and a Customers table.

The relationship between the tables is as follows:

  • User: userId, name, email, password
  • Customer: customerId, status, section_id, user_id
  • Product: productId, name, price, purchase_date, customer_id

Well, the Products table is related to the Customers table and not to the Users table. And I can't change that because there are other applications consulted in the database.

I try to get the products this way, but the id value of Customer is null.

public IActionResult View(int id)
{
    User user = _loginUser.GetUser();
    Product product = _pedidoRepository.GetProduct(id);

    if (product.customer_id != user.Customer.customerId)
    {
        return new ContentResult() { Content = "Access denied" };
    }

    return View(product);
}

I get the error:

Project.Models.users.Customer.get returned null

How can I make the Client id accessible for comparison purposes?

My GetUser method looks like this:

public user GetUser()
{
    // Deserializar
    if( _session.Exists(Key)) 
    {
        string user JSONString = _session.Consult(Key);
        return JsonConvert.DeserializeObject<user>(userJSONString);
    }
    else
    {
        return null;
    }
}

It is important to say that the relationship between "User" and "Customer" is one by one. When registering "User", "Customer" is registered in the table with a fk for "User"

Class User.cs

public class User
{
    /* PK */
    [Key]
    public int userId { get; set; }
    public string name { get; set; }
    public string email{ get; set; }
    public string passwor{ get; set; }

    [ForeignKey("user_id")]
    [JsonIgnore]
    public Customer Customer { get; set; }
}

Class Customer.cs

public class Customer
{
    /* PK */
    [Key]
    public int customerId{ get; set; }
    public string Status { get; set; }
   
    [Column("user_id")]
    [ForeignKey("User")]
    public int? user_id { get; set; }
    public virtual User User { get; set; }
    .
    .
}

Class Product.cs

public class Product
{
    /* PK */
    [Key]
    public int productId{ get; set; }
    public string name { get; set; }
    public decimal price { get; set; }
    public DateTime  purchase_date { get; set; }

    [Column("customer_id")]
    [ForeignKey("Customer")]
    public int? customer_id{ get; set; }
    public virtual Customer Customer { get; set; }
}

I appreciate any comments. I'm starting with language. If I need to, I can send some more codes.

Maxim Zabolotskikh

Well I would assume that your session holds a JSON representation of user, queried at some point from the DB. When this happens, user is queried without the Customer navigational property (so it's null). When you access later User.Customer, User entity is not attached to DbContext anymore, therefore Customer is not loaded from the DB. If my assumtion is true, I'd propose two options:

  1. When filling the session object, query user with loaded customer, like context.Users.Include(c => c.Customer).GetById(x)
  2. In your method query Customer from the DB using userId. I.e. you get User from session, but matching Customer from the DB.

The choice depends on whether Cutomer is relevant for most of your calls or just for this single one.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

ASP.NET MVC: Get user id of currently logged in user

分類Dev

How to authorize user who logged in using external login in Asp.Net MVC

分類Dev

ASP.NET MVC4 "Getting Started" Configuration Error

分類Dev

ASP.NET - MVC - Getting data back from another Model

分類Dev

Getting value from HTML code in ASP.NET MVC

分類Dev

ASP.NET MVC CurrentCulture, from user or server?

分類Dev

Redirect user from startup.cs asp.net core

分類Dev

In ASP.NET Core 2.1 how do I add menu items after the user has logged in based on Role?

分類Dev

Weird Error Upgrading ASP.NET MVC from 4 to 5

分類Dev

Tow types of user logged in the same login form - ASP.NET

分類Dev

Display logged in user data only in MVC 4 error

分類Dev

ASP.NET MVC 5 Web API not accepting View Model on POST from MVC ASP.NET Core solution

分類Dev

Is it possible to reference .net framework 4.7.2 class library from an ASP.NET Core MVC project?

分類Dev

I'm getting an DirectoryNotFoundException error after published an blazor assembly application ASP.NET CORE hosted in IIS

分類Dev

ASP.NET Core MVC MySQL Identity

分類Dev

User management with ASP.NET MVC 4

分類Dev

asp.net core not binding current User

分類Dev

How can I stop ASP.NET Core 2.0 MVC minification from modifying a specific file?

分類Dev

Store / Retrieve ConnectionString from appSettings.json in ASP.net Core 2 MVC app

分類Dev

Change authentication from no authentication to individual authentication in existing project of Asp.Net Core (MVC)

分類Dev

Set Page from Area Folder as default page in asp.net core 2.2 MVC

分類Dev

How to get data from 2 (or more) independent dropdown asp.net core mvc?

分類Dev

How to get user profile data from ASP.NET MVC custom IdentityUser Model?

分類Dev

Call User.Identity.GetUserId() from Helpers with ASP.NET MVC

分類Dev

Can I change asp.net mvc 5 identity user ID from string(GUID) to int

分類Dev

ASP.NET Core MVC と EF Core 1.1

分類Dev

Error using ExpandoObject in ASP.NET MVC

分類Dev

500 error getting object from web service by AJAX in ASP.NET

分類Dev

Getting taggable id of a logged user by facebook id

Related 関連記事

  1. 1

    ASP.NET MVC: Get user id of currently logged in user

  2. 2

    How to authorize user who logged in using external login in Asp.Net MVC

  3. 3

    ASP.NET MVC4 "Getting Started" Configuration Error

  4. 4

    ASP.NET - MVC - Getting data back from another Model

  5. 5

    Getting value from HTML code in ASP.NET MVC

  6. 6

    ASP.NET MVC CurrentCulture, from user or server?

  7. 7

    Redirect user from startup.cs asp.net core

  8. 8

    In ASP.NET Core 2.1 how do I add menu items after the user has logged in based on Role?

  9. 9

    Weird Error Upgrading ASP.NET MVC from 4 to 5

  10. 10

    Tow types of user logged in the same login form - ASP.NET

  11. 11

    Display logged in user data only in MVC 4 error

  12. 12

    ASP.NET MVC 5 Web API not accepting View Model on POST from MVC ASP.NET Core solution

  13. 13

    Is it possible to reference .net framework 4.7.2 class library from an ASP.NET Core MVC project?

  14. 14

    I'm getting an DirectoryNotFoundException error after published an blazor assembly application ASP.NET CORE hosted in IIS

  15. 15

    ASP.NET Core MVC MySQL Identity

  16. 16

    User management with ASP.NET MVC 4

  17. 17

    asp.net core not binding current User

  18. 18

    How can I stop ASP.NET Core 2.0 MVC minification from modifying a specific file?

  19. 19

    Store / Retrieve ConnectionString from appSettings.json in ASP.net Core 2 MVC app

  20. 20

    Change authentication from no authentication to individual authentication in existing project of Asp.Net Core (MVC)

  21. 21

    Set Page from Area Folder as default page in asp.net core 2.2 MVC

  22. 22

    How to get data from 2 (or more) independent dropdown asp.net core mvc?

  23. 23

    How to get user profile data from ASP.NET MVC custom IdentityUser Model?

  24. 24

    Call User.Identity.GetUserId() from Helpers with ASP.NET MVC

  25. 25

    Can I change asp.net mvc 5 identity user ID from string(GUID) to int

  26. 26

    ASP.NET Core MVC と EF Core 1.1

  27. 27

    Error using ExpandoObject in ASP.NET MVC

  28. 28

    500 error getting object from web service by AJAX in ASP.NET

  29. 29

    Getting taggable id of a logged user by facebook id

ホットタグ

アーカイブ