实体框架:编写自定义数据注释来更改值的 CASE
class DemoUser
{
[TitleCase]
public string FirstName { get; set; }
[TitleCase]
public string LastName { get; set; }
[UpperCase]
public string Salutation { get; set; }
[LowerCase]
public string Email { get; set; }
}
假设我有上面写的演示类,我想创建一些自定义注释,如 LowerCase、UpperCase 等,以便其值自动转换。这样做将使我能够在其他类中使用这些注释。
class DemoUser
{
[TitleCase]
public string FirstName { get; set; }
[TitleCase]
public string LastName { get; set; }
[UpperCase]
public string Salutation { get; set; }
[LowerCase]
public string Email { get; set; }
}
Suppose i have demo-class as written above, i want to create some custom annotations like LowerCase,UpperCase etc so that its value gets converted automatically. Doing this will enable me to use these annotations in other classes too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如拉迪斯拉夫所暗示的,这是两个问题合二为一的。
假设您遵循 Jefim 链接中创建属性的方法,并假设您调用这些创建的属性类“UpperCaseAttribute”、“LowerCaseAttribute”和“TitleCaseAttribute”,则以下 SaveChanges() 重写应该在 EF 4.3(当前版本)中工作截至本回答发布时)。
As Ladislav implied, this is two questions in one.
Assuming you follow the recipe for creating attributes in Jefim's link, and assuming you're calling those created attribute classes "UpperCaseAttribute", "LowerCaseAttribute", and "TitleCaseAttribute", the following SaveChanges() override should work in EF 4.3 (the current version as of the time of this answer post).
您可以在 EF 上下文中重写 SaveChanges 方法(如果您使用默认代码生成,只需编写一个分部类)。像下面这样:
You can override the
SaveChanges
method in your EF context (if you use default code-generation just write a partial class). Something like the following: