如果存在子视图,如何使用产品实体
How to use product entity if child view exists
ASP.NET 5 MVC 购物车应用程序使用 EF Core 和 Npgsql 数据提供程序。
数据库包含产品 table
create table Product (
Product char(20) primary key;
Description char(50);
)
它被 EF Core Scaffold 映射到 Product 实体
public class Product {
public string Product1 { get; set; }
public string Description { get; set; }
}
只读产品视图实体具有数据库和产品中不存在的特殊阴影属性 class:
public class ShopToode: Product {
public decimal CartPrice { get; set; }
}
public DbSet<ShopToode> ShopToodes { get; set; }
public DbSet<Product> Products { get; set; }
ShopToode 仅用于使用 FromSqlRaw 查看数据:
var tooteinfo = await ctx.ShopToodes.FromSqlRaw(@"select *,
1.2 as CartPrice
from Product").AsNoTracking().ToListAsync();
正在尝试获得类似的产品
var t = ctx.Products.First();
抛出错误
Npgsql.PostgresException (0x80004005): 42703: column t.Discriminator
does not exist
如果视图也存在,如何使用产品实体?
不确定 ShopToode 是什么,但听起来像是购物车商品。除非 ShopToode 需要实际保存到数据库中,否则我不会将其声明为实体,并且绝对不会 继承自 Product。您可以声明一个视图模型或 DTO,您打算序列化或插入会话变量并将产品的项目详细信息添加到它:
var tooteinfo = await ctx.Products
.Select(x => new TooteInfo
{
ProductId = x.ProductId,
// ...
CartPrice = 1.2
}).ToListAsync();
对于持久保存到数据的结构,其中我有一个购物车(对于用户),CartProduct 将产品链接到购物车,以及一个产品,这更适合多对多关系:
public class Cart
{
[Key]
public int CartId { get; set; }
public int UserId {get; set; }
// ...
public virtual ICollection<CartProduct> CartProducts { get; set; } = new List<CartProduct>();
}
public class CartProduct
{
[Key, Column(Order=0), ForeignKey("Cart")]
public int CartId { get; set; }
[Key, Column(Order=1), ForeignKey("Product")]
public int ProductId { get; set; }
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
[NotMapped]
public decimal CartPrice
{
get { return UnitPrice * Quantity; }
}
public virtual Cart Cart { get; set; }
public virtual Product Product { get; set; }
}
public class Product
{
[Key]
public int ProductId { get; set; }
// ...
}
从那里您可以从 CartProducts 查询购物车和 select 详细信息,并通过它到 Product 以构建视图模型。
ASP.NET 5 MVC 购物车应用程序使用 EF Core 和 Npgsql 数据提供程序。 数据库包含产品 table
create table Product (
Product char(20) primary key;
Description char(50);
)
它被 EF Core Scaffold 映射到 Product 实体
public class Product {
public string Product1 { get; set; }
public string Description { get; set; }
}
只读产品视图实体具有数据库和产品中不存在的特殊阴影属性 class:
public class ShopToode: Product {
public decimal CartPrice { get; set; }
}
public DbSet<ShopToode> ShopToodes { get; set; }
public DbSet<Product> Products { get; set; }
ShopToode 仅用于使用 FromSqlRaw 查看数据:
var tooteinfo = await ctx.ShopToodes.FromSqlRaw(@"select *,
1.2 as CartPrice
from Product").AsNoTracking().ToListAsync();
正在尝试获得类似的产品
var t = ctx.Products.First();
抛出错误
Npgsql.PostgresException (0x80004005): 42703: column t.Discriminator does not exist
如果视图也存在,如何使用产品实体?
不确定 ShopToode 是什么,但听起来像是购物车商品。除非 ShopToode 需要实际保存到数据库中,否则我不会将其声明为实体,并且绝对不会 继承自 Product。您可以声明一个视图模型或 DTO,您打算序列化或插入会话变量并将产品的项目详细信息添加到它:
var tooteinfo = await ctx.Products
.Select(x => new TooteInfo
{
ProductId = x.ProductId,
// ...
CartPrice = 1.2
}).ToListAsync();
对于持久保存到数据的结构,其中我有一个购物车(对于用户),CartProduct 将产品链接到购物车,以及一个产品,这更适合多对多关系:
public class Cart
{
[Key]
public int CartId { get; set; }
public int UserId {get; set; }
// ...
public virtual ICollection<CartProduct> CartProducts { get; set; } = new List<CartProduct>();
}
public class CartProduct
{
[Key, Column(Order=0), ForeignKey("Cart")]
public int CartId { get; set; }
[Key, Column(Order=1), ForeignKey("Product")]
public int ProductId { get; set; }
public int Quantity { get; set; }
public decimal UnitPrice { get; set; }
[NotMapped]
public decimal CartPrice
{
get { return UnitPrice * Quantity; }
}
public virtual Cart Cart { get; set; }
public virtual Product Product { get; set; }
}
public class Product
{
[Key]
public int ProductId { get; set; }
// ...
}
从那里您可以从 CartProducts 查询购物车和 select 详细信息,并通过它到 Product 以构建视图模型。