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, passwordCustomer
: customerId, status, section_id, user_idProduct
: productId, name, price, purchase_date, customer_idWell, 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.
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:
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.
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加