[英]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_idProduct
: 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:如果我的假设是真的,我会提出两个选择:
context.Users.Include(c => c.Customer).GetById(x)
填写 session object 时,使用已加载的客户查询用户,如context.Users.Include(c => c.Customer).GetById(x)
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.