我想使用 Asp.Net Core MVC 将这些功能构建到项目中。
有人可以指导我完成这些步骤吗:
我创建了一个带有身份验证的 ASP.NET Core MVC 项目,用户可以在其中注册和登录。 我还创建了这些模型。
namespace Company.Models
{
public class ProductType
{
public ProductType()
{
Products = new List<Product>();
}
public long ProductTypeId { get; set; }
public string ProductName { get; set; }
public string ProductInfo { get; set; }
public string Location { get; set; }
public ProductTypeStatus Status { get; set; }
public string ImageUrl { get; set; }
public string Manufacturer { get; set; }
public string AdminComment { get; set; }
public Category Categories { get; set; }
public ICollection<Product> Products { get; protected set; }
}
public enum ProductTypeStatus
{
Available,
ReservedAdmin
}
public enum ProductStatus
{
Available,
ReservedLoaner,
ReservedAdmin,
Loaned,
Defect,
Trashed,
Lost,
NeverReturned
}
namespace Company.Models
{
public class Product
{
public long ProductId { get; set; }
public long ProductTypeId { get; set; }
public int ProductNumber { get; set; }
public string SerialNo { get; set; }
public ProductStatus Status { get; set; }
public string AdminComment { get; set; }
public string UserComment { get; set; }
public long? CurrentLoanInformationId { get; set; }
}
}
namespace Company.Models
{
public class Category
{
public Category()
{
ProductTypes = new List<ProductType>();
}
public int CategoryId { get; set; }
public string Name { get; set; }
public ICollection<ProductType> ProductTypes
{
get; protected set;
}
}
我最近转向了 Asp.Net Core MVC。所以这对我来说是一个新的开始环境。不过,我确实遵循了有关 asp.net mvc 的 msdn 教程。 我感谢任何帮助!
我看到了你的模型设计,我认为你错过了一件小事,即产品和类别之间的关系。
1 个产品将属于 1 个类别
因此,要添加一对一关系,您需要像这样调整模型。您可以查看更多这里
namespace Company.Models
{
public class Product
{
public long ProductId { get; set; }
public long ProductTypeId { get; set; }
public int ProductNumber { get; set; }
public string SerialNo { get; set; }
public ProductStatus Status { get; set; }
public string AdminComment { get; set; }
public string UserComment { get; set; }
public long? CurrentLoanInformationId { get; set; }
public Category Category { get;set; }
}
}
namespace Company.Models
{
public class Category
{
public Category()
{
ProductTypes = new List<ProductType>();
}
public int CategoryId { get; set; }
public string Name { get; set; }
public ICollection<ProductType> ProductTypes
{
get; protected set;
}
}
}
因此,当您更新模型时,您将需要运行 ef 迁移以将更改应用到数据库。详情可以看这里
最后你需要编写代码来查询类似的东西
var query = _db.Product.Where(x => x.Category == "Book");
您可以阅读如何在 C# 中编写 ef 查询这里
我现在修复了它,问题没有列出,因为添加了类别状态 IsActive false 并且有一个小错字,谢谢。