如何防止 DataAnnotations 属性在 DataBase First Model 中被删除
我有带有实体框架的 asp.net mvc 3 应用程序,我使用数据库优先模型来设置它。
我的步骤如下: 1. 生成带有表的数据库 2.创建ADO.NET实体数据模型文件(.edmx)并导入表 3.在设计中我添加了一个代码生成项并使用了ADO.NET DbContext Generator 4. 使用所有表模型制作的 Model1.tt 支架
我已经编辑了模型并使用 DataAnnotations 属性更新了它们(仅作为众所周知的示例)
public class LogOnModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
现在,当我对数据库进行更改并将其更新为edmx 文件中的所有模型都将被覆盖,并且 DataAnnotations 属性将消失。
我的问题: 我如何使用数据库优先模型并仍然编辑模型以进行特定验证,就像我可以自由地使用代码优先模型一样? (请不要使用第三方工具解决方案) 谢谢
I have my asp.net mvc 3 application with entity framework and i used the Database First model to set it up.
My Steps below:
1. Genarated a database with tables
2. Created ADO.NET Entity Data Model file (.edmx) and imported the tables
3. inside the design i added a Code Generation item and used ADO.NET DbContext Generator
4. a Model1.tt holder as been made with all of the tables Models
I have edited the models and updated them with DataAnnotations Attributes (just for the example a well known one)
public class LogOnModel
{
[Required]
[Display(Name = "User name")]
public string UserName { get; set; }
[Required]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[Display(Name = "Remember me?")]
public bool RememberMe { get; set; }
}
Now when im making changes to the database and updating it to the edmx file all the models will be overwritten and the DataAnnotations Attributes will disapear.
my question:
how can i use database first model and still edit the models for speciific validation like im free to do with code first model?
(please no third party tools solution)
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你需要使用伙伴类。请参阅我的过时但仍然有用的文章 http://msdn.microsoft.com /en-us/library/ee256141(v=vs.98).aspx
You need to use buddy classes. See my dated but still useful article http://msdn.microsoft.com/en-us/library/ee256141(v=vs.98).aspx
在视图中使用 ViewModel。这会将 EF 实体与 UI 逻辑分离。
Use ViewModels in your Views. This will decouple your EF entities from your UI logic.