data-annotations

Entity Framework 4.1 InverseProperty Attribute

 ̄綄美尐妖づ 提交于 2019-11-27 00:26:32
Just wanted to know more about RelatedTo attribute and I found out it has been replaced by ForeignKey and InverseProperty attributes in EF 4.1 RC. Does anyone know any useful resources about the scenarios that this attribute becomes useful? Should I use this attribute on navigation properties? example: public class Book { public int ID {get; set;} public string Title {get; set;} [ForeignKey("FK_AuthorID")] public Author Author {get; set;} } public class Author { public int ID {get; set;} public string Name {get; set;} // Should I use InverseProperty on the following property? public virtual

Entity Framework code first unique column

淺唱寂寞╮ 提交于 2019-11-27 00:15:55
I am using Entity Framework 4.3 and using Code Fist. I have a class public class User { public int UserId{get;set;} public string UserName{get;set;} } How do I tell Entity Framework that UserName has to be unique when creating database table? I would prefer to use data anotations instead of configuration file if possible. In Entity Framework 6.1+ you can use this attribute on your model: [Index(IsUnique=true)] You can find it in this namespace: using System.ComponentModel.DataAnnotations.Schema; If your model field is a string, make sure it is not set to nvarchar(MAX) in SQL Server or you will

Validating DataAnnotations with Validator class

寵の児 提交于 2019-11-27 00:14:19
问题 I'm trying to validate a class decorated with data annotation with the Validator class. It works fine when the attributes are applied to the same class. But when I try to use a metadata class it doesn't work. Is there anything I should do with the Validator so it uses the metadata class? Here's some code.. this works: public class Persona { [Required(AllowEmptyStrings = false, ErrorMessage = "El nombre es obligatorio")] public string Nombre { get; set; } [Range(0, int.MaxValue, ErrorMessage=

“The Id field is required” validation message on Create; Id not set to [Required]

﹥>﹥吖頭↗ 提交于 2019-11-27 00:12:42
问题 This is happening when I try to create the entity using a Create style action in Asp.Net MVC 2. The POCO has the following properties: public int Id {get;set;} [Required] public string Message {get; set} On the creation of the entity, the Id is set automatically, so there is no need for it on the Create action. The ModelState says that "The Id field is required", but I haven't set that to be so. Is there something automatic going on here? EDIT - Reason Revealed The reason for the issue is

Conditionally required property using data annotations

余生长醉 提交于 2019-11-27 00:10:14
问题 I have a class like this: public class Document { public int DocumentType{get;set;} [Required] public string Name{get;set;} [Required] public string Name2{get;set;} } Now if I put a [Required] data annotation on the Name and Name2 properties, then everything is ok and if Name or Name2 are empty, validation will throw an error. But I want Name field only to be required if DocumentType is equal to 1 and Name2 only required if DocumentType is equal to 2 . public class Document { public int

DataAnnotations dynamically attaching attributes

我是研究僧i 提交于 2019-11-27 00:03:23
Apparently it is possible to dynamically attach DataAnnotation attributes to object properties at runtime and as such achieve dynamic validation. Can someone provide code sample on this? MVC has a hook to provide your own ModelValidatorProvider. By default MVC 2 uses a sub class of ModelValidatorProvider called DataAnnotationsModelValidatorProvider that is able to use System.DataAnnotations.ComponentModel.ValidationAttribute attributes for validation. The DataAnnotationsModelValidatorProvider uses reflection to find all the ValidationAttributes and simply loops through the collection to

MVC Model require true

时间秒杀一切 提交于 2019-11-27 00:01:46
Is there a way through data annotations to require that a boolean property be set to true? public class MyAwesomeObj{ public bool ThisMustBeTrue{get;set;} } You could create your own validator: public class IsTrueAttribute : ValidationAttribute { #region Overrides of ValidationAttribute /// <summary> /// Determines whether the specified value of the object is valid. /// </summary> /// <returns> /// true if the specified value is valid; otherwise, false. /// </returns> /// <param name="value">The value of the specified validation object on which the <see cref="T:System.ComponentModel

Int or Number DataType for DataAnnotation validation attribute

…衆ロ難τιáo~ 提交于 2019-11-26 23:59:45
问题 On my MVC3 project, I store score prediction for football/soccer/hockey/... sport game. So one of properties of my prediction class looks like this: [Range(0, 15, ErrorMessage = "Can only be between 0 .. 15")] [StringLength(2, ErrorMessage = "Max 2 digits")] [Remote("PredictionOK", "Predict", ErrorMessage = "Prediction can only be a number in range 0 .. 15")] public int? HomeTeamPrediction { get; set; } Now, I need also change error message for a data type, int in my case. There is some

Fluent Validation vs. Data Annotations [closed]

浪尽此生 提交于 2019-11-26 23:57:37
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 8 years ago . What are the operative differences between these two validation packages when used for ASP.NET MVC validatation? They seem to have

Creating entity relationship with renamed fields and non-primary key in primary table

久未见 提交于 2019-11-26 23:36:20
问题 The following are two partial tables in which I am trying to define a foreign key relationship. public class Form { [Key, Column("FormID")] public System.Guid FormGUID { get; set; } [Column("PatGUID")] public Nullable<System.Guid> PatientGUID { get; set; } } public class Patient { [Column("PatGUID")] public System.Guid PatientGUID { get; set; } [Key, Column("PatID")] public int PatientID { get; set; } } I've eliminated all but the relevant information, fields, navigations, etc. for this