首页 > 解决方案 > 实体框架数据类

问题描述

我找到了解决问题的方法

   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; }

    }

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

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

标签: c#.netentity-framework

解决方案


为什么你想将 bool 字段添加到 people 类或者你打算从中实现什么没有明确说明,但我会告诉你我的想法。

  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 更新数据库

...无论是什么情况,只要明确说明即可。


推荐阅读