使用AddRange的C#Seed Private成员

问题描述 投票:3回答:1

我有一个包含公共和私人成员的Product类。示例UnitPrice是私有的。当我尝试播种数据时,我需要一个很好的语法方法来设置UnitPrice和Addrange。我该怎么做?

public partial class Product
{
    public int ProductId { get; set; }
    public string ProductName { get; set; }
    public string ProductDescription { get; set; }
    private float UnitPrice { get; set; }

    public Product(int productid, string productname, string productdescription, float unitprice, string imagelocation)
    {
        ProductId = productid;
        ProductName = productname;
        ProductDescription = productdescription;
        UnitPrice = unitprice;
        ImageLocation = ImageLocation;
    }

我如何查看数据?我不能设置单价,因为它无法进入。

        if (!context.Product.Any())
        {
            context.Product.AddRange(
             new Product
             {
                 ProductName = "Samsung T3 Portable SSD - 500GB",
                 ProductDescription = "Superfast Read-Write Speeds of up to 450 MB/s; Shock Resistant & Secure Encryption",
                 UnitPrice = 5.50F,
                 ImageLocation = "Product1.jpg"
             },


Error Code:
    Severity    Code    Description Project File    Line    Suppression State
    Error   CS0122  'Product.UnitPrice' is inaccessible due to its protection level ElectronicsStore    
c# .net-core .net-core-2.0
1个回答
4
投票

你有一个接受这些参数的构造函数;用它:

if (!context.Product.Any()) { 
{
   context.Product.AddRange(
      new Product(id, "Samsung T3 Portable SSD - 500GB",  "Superfast Read-Write Speeds of up to 450 MB/s; Shock Resistant & Secure Encryption", 5.50F, "Product1.jpg"), 
      new Product(/* parameters */) 
) }
© www.soinside.com 2019 - 2024. All rights reserved.