c# - 实体框架数据类
问题描述
我找到了解决问题的方法
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; }
}
我无法向其中添加布尔字段,因为它破坏了逻辑并且实体核心不允许我这样做。
我该如何解决这个问题?谢谢。
解决方案
为什么你想将 bool 字段添加到 people 类或者你打算从中实现什么没有明确说明,但我会告诉你我的想法。
如果要为 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!! }
我猜你想复制你在人员课上为员工所做的事情,就这样做吧!
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); }
如果“My class generate with entity”表示您正在生成实体,则表示您很可能首先使用数据库,并且您正在使用 ADO-EDMX 文件根据数据库中的表生成实体。这意味着您应该将这些属性添加为列是“人员”表,然后重新生成实体,并且您将拥有添加的属性。
如果代码优先,请添加属性并运行迁移命令
add-migration addedBoolProps 更新数据库
...无论是什么情况,只要明确说明即可。
推荐阅读
- typescript - 向 Parse.User 扩展/添加方法
- xcode - react-native clang:错误:链接器命令失败,退出代码为 1
- vb6 - vb6中的双面打印问题
- python - 保存递归函数的多个值
- android-studio - 插件与发现的新版本不兼容 .ignore
- clickonce - 有没有办法跟踪一次应用程序的点击
- spring - 如何验证 Spring Cloud Gateway 中传入服务的标头?我需要调用 2 个其他 REST API 调用(GET/POST)来验证标头
- react-native - 如何通过反应原生地图框获取我们正在查看的位置?
- machine-learning - 如何确定 RMSLE 的准确度?
- symfony - Symfony 3.4 到 4.2 模拟 @ParamConverter 类?