【问题标题】:entity framework data class实体框架数据类
【发布时间】:2020-04-04 21:35:40
【问题描述】:

我找到了解决问题的方法

   public class Employee
    {
        public int? Id { get; set; }
        public bool IsIdSelected { get; set; }

        public string Name { get; set; }
        public bool IsNameSelected { get; set; }

        public string Address { get; set; }
        public bool IsAddressSelected { get; set; }
    }

        private void OnExportButtonClick(object sender, RoutedEventArgs e)
        {
            // Now, concatinate all the selected cells
            var str = string.Empty;
            foreach (var emp in _collection)
            {
                if (emp.IsIdSelected)
                    str += string.Format("{0}, ", emp.Id);

                if (emp.IsNameSelected)
                    str += string.Format("{0}, ", emp.Name);

                if (emp.IsAddressSelected)
                    str += string.Format("{0}", emp.Address);

                str += "\n";
            }
            // Instead of displaying this message you could export these cells to Excel
            // in the format you need.
            MessageBox.Show(str);
        }
    }
}

但是……

我的类生成实体包含这个:

public partial class People
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string Phone { get; set; }

    }

我无法向其中添加布尔字段,因为它破坏了逻辑并且实体核心不允许我这样做。

我该如何解决这个问题? 谢谢。

【问题讨论】:

  • 永远不要在循环中连接string。你应该在这里使用StringBuilder。此外,您的问题并不清楚您的问题是什么以及您正在寻求什么样的解决方案。
  • 请把问题说清楚

标签: c# .net entity-framework


【解决方案1】:

我建议使用 ViewModel + automapper 您不想将 IsSelected 字段保存到数据存储中,因此请使用 ViewModel 并将其映射到 DataModel,反之亦然。

【讨论】:

    【解决方案2】:

    你可以为你的实体添加任何你想要的属性,如果你想让实体框架完全忽略它们,那么用[NotMapped]标记它们。

    这已经支持很久了。

    【讨论】:

      【解决方案3】:

      没有明确说明为什么要将 bool 字段添加到人员类或您打算从中实现的目标,但我会告诉您我的想法。

      1. 如果要为 people 类添加额外的属性,那就去做吧!

        public partial class People
        
        {
        
        public int Id { get; set; }
        
        public int IsIdSelected { get; set; }
        
        public string Name { get; set; }
        
        public int IsNameSelected { get; set; }
        
        public string Phone { get; set; }
        
        public int IsPhoneSelected { get; set; }
        
        //you can let me know what error you are getting from adding this properties!!
        
        }
        
      2. 我猜你想复制你在人员课程中为员工所做的事情,就这样做!

         private void OnExportButtonClick(object sender, RoutedEventArgs e)
         {
             // Now, concatinate all the selected cells
             var str = string.Empty;
             foreach (var ppl in _collection)
             {
                 if (ppl.IsIdSelected)
                     str += string.Format("{0}, ", ppl.Id);
        
                 if (ppl.IsNameSelected)
                     str += string.Format("{0}, ", ppl.Name);
        
                 if (ppl.IsPhoneSelected)
                     str += string.Format("{0}", ppl.Phone);
        
                 str += "\n";
             }
             // Instead of displaying this message you could export these cells to Excel
             // in the format you need.
             MessageBox.Show(str);
         }
        
      3. 如果“My class generate with entity”表示您正在生成实体,则表示您很可能首先使用数据库,并且您正在使用 ADO-EDMX 文件根据数据库中的表生成实体。这意味着您应该将这些属性添加为列是“人员”表,然后重新生成实体,您将拥有添加的属性。

      4. 如果代码优先,添加属性并运行迁移命令

      add-migration addedBoolProps 更新数据库

      ...不管是什么情况,只要明确说明即可。

      【讨论】:

        猜你喜欢
        • 2012-03-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-11-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-12-25
        相关资源
        最近更新 更多