如何防止 DataAnnotations 属性在 DataBase First Model 中被删除

发布于 2025-01-04 05:01:07 字数 747 浏览 4 评论 0原文

我有带有实体框架的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原来分手还会想你 2025-01-11 05:01:07

你需要使用伙伴类。请参阅我的过时但仍然有用的文章 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

淡墨 2025-01-11 05:01:07

在视图中使用 ViewModel。这会将 EF 实体与 UI 逻辑分离。

Use ViewModels in your Views. This will decouple your EF entities from your UI logic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文