实体框架中的验证 [英] Validations in entity framework
本文介绍了实体框架中的验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在做一个项目,我通过实体框架工作添加了数据库模型。现在我需要验证一些数据。我不知道在哪里添加验证。可以有人帮助我..... >
我试图在模型中添加验证 - > Model1.edmx - > Model1.tt - > Login.cs
但是没有用完
我尝试了什么:
Hi, I am doing one project in that i added database to model through entity frame work.Now i need to validate some data.I dont know where to add validations.Can anybody help me.....
I have tried to add validations in model -> Model1.edmx -> Model1.tt -> Login.cs
but it is not working out
What I have tried:
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated from a template.
//
// Manual changes to this file may cause unexpected behavior in your application.
// Manual changes to this file will be overwritten if the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace DOT_NET_COMMUNITY.Models
{
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
public partial class Login
{
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
public Login()
{
this.Articles = new HashSet<Article>();
this.ForumReplies = new HashSet<ForumReply>();
this.Forums = new HashSet<Forum>();
this.Projects = new HashSet<Project>();
}
public int LoginId { get; set; }
[Required(ErrorMessage = "Name Required")]
public string Name { get; set; }
[Required(ErrorMessage="Please enter User Name")]
public string Username { get; set; }
[Required(ErrorMessage = "Please enter Password")]
public string Password { get; set; }
[Required(ErrorMessage = "Please give security Que")]
public string SecQuestion { get; set; }
[Required(ErrorMessage = "Please enter Ans")]
public string Answer { get; set; }
[Required(ErrorMessage = "Please give the status")]
public string Status { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Article> Articles { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<ForumReply> ForumReplies { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Forum> Forums { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Project> Projects { get; set; }
}
}
推荐答案
您可以使用数据注释属性,自定义验证,这里有一个示例MSDN
实体框架验证 [ ^ ]
如何:在数据模型中自定义数据字段验证 [ ^ ]
如何:使用自定义属性在数据模型中自定义数据字段验证 [ ^ ]
代码优先的验证属性 [ ^ ]
You can use Data Annotations Attributes, custom validation, here is an example on MSDN
Entity Framework Validation[^]
How to: Customize Data Field Validation in the Data Model[^]
How to: Customize Data Field Validation in the Data Model Using Custom Attributes[^]
Validation Attributes in Code-First[^]
这篇关于实体框架中的验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文