简体   繁体   English

从登录用户获取产品时出错。 ASP.NET 核心MVC

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

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.我有一个包含一般注册数据的Users表和一个Customers表。

The relationship between the tables is as follows:表之间的关系如下:

  • User : userId, name, email, password User :userId、姓名、email、密码
  • Customer : customerId, status, section_id, user_id Customer :customerId、status、section_id、user_id
  • Product : productId, name, price, purchase_date, customer_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.那么, Products表与Customers表相关,而不是与Users表相关。 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.我尝试通过这种方式获取产品,但Customer的 id 值为 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 Project.Models.users.Customer.get 返回 null

How can I make the Client id accessible for comparison purposes?如何使Client ID 可访问以进行比较?

My GetUser method looks like this:我的GetUser方法如下所示:

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"注册“用户”时,“客户”在表中注册,“用户”的 fk

Class User.cs 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 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 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.如果需要,我可以发送更多代码。

Well I would assume that your session holds a JSON representation of user, queried at some point from the DB.好吧,我假设您的 session 拥有用户的 JSON 表示,在某个时候从数据库中查询。 When this happens, user is queried without the Customer navigational property (so it's null).发生这种情况时,将在没有 Customer 导航属性的情况下查询用户(因此它为空)。 When you access later User.Customer, User entity is not attached to DbContext anymore, therefore Customer is not loaded from the DB.当您稍后访问 User.Customer 时,用户实体不再附加到 DbContext,因此不会从数据库加载客户。 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)填写 session object 时,使用已加载的客户查询用户,如context.Users.Include(c => c.Customer).GetById(x)
  2. In your method query Customer from the DB using userId.在您的方法中,使用 userId 从数据库查询客户。 Ie you get User from session, but matching Customer from the DB.即您从 session 获得用户,但从数据库中匹配客户。

The choice depends on whether Cutomer is relevant for most of your calls or just for this single one.选择取决于 Cutomer 是与您的大多数呼叫相关还是仅与单个呼叫相关。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我无法访问已登录用户的用户名。 (ASP.NET Core Web API) - I can't access the UserName of the logged in user. (ASP.NET Core Web API) 检查用户是否在 ASP.NET Core 中登录 - Check if user is logged on in ASP.NET Core 使用 ASP.NET Core MVC 获取用户输入 - Getting User Input with ASP.NET Core MVC 在ASP.NET中获取已登录用户的userID - Getting the userID of a logged in User in ASP.NET ASP.NET Core 6:当使用不同的用户打开 Visual Studio 时,httpclient 调用 API 时出错。 AuthenticationException:远程证书无效 - ASP.NET Core 6 : httpclient error calling API when open Visual Studio with different User. AuthenticationException: The remote certificate is invalid 在 ASP.NET Core MVC Web 应用程序中出现构造函数错误? - Getting constructor error in ASP.NET Core MVC web app? 获取ASP.NET Core MVC中当前登录用户的角色 - Get role/s of current logged in user in ASP.NET Core MVC 使用 AddHttpContextAccessor 在 ASP.NET Core 3.1 MVC 中获取当前登录的用户 ID - Get currently logged on user id in ASP.NET Core 3.1 MVC with AddHttpContextAccessor 用户登录时如何显示 HTML 元素。(ASP.NET Core MVC,无 EF) - How to display HTML element when user is logged in. (ASP.NET Core MVC, without EF) ASP.NET 核心 MVC 身份 - 如何查看当前登录的用户? - ASP.NET Core MVC Identity - how to get current logged in user in view?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM