data-annotations

EF CodeFirst: Rails-style created and modified columns

拜拜、爱过 提交于 2019-12-04 13:43:53
Using Entity Framework CodeFirst, how do I create a created datetime column that gets populated with the current timestamp everytime a record is inserted for that table, and a modified datetime column that has a timestamp generated evertime a row is updated? Rails does this by default and I was hoping the EF generated database would have this as well, but it doesn't. Is this something that can be done with data annotations? If so, how? Thanks! Ladislav Mrnka It is not supported in EF. EF will not create these columns for you automatically. You must do it yourselves by either: Have Created and

Has the behavior for DataAnnotations in asp.net mvc 3 changed?

走远了吗. 提交于 2019-12-04 11:37:35
I have a Model with a property [ReadOnly(true)] public decimal BodyMassIndex { get; private set; } In my View when I call @Html.EditorForModel() I still get a standard editable textbox for that property Why is this? if the textbox is still editable whats the point of this DataAnnotation Attibute? Brad Wilson's post That is not a DataAnnotation attribute. Notice it's in the System.ComponentModel namespace. Data Annotations are in the System.ComponentModel.DataAnnotations namespace. However, this is a case where we could consider supporting it. But what exactly did you expect to happen and why

Allow empty strings for fields marked with PhoneAttribute or UrlAttribute

大憨熊 提交于 2019-12-04 09:57:28
问题 I'm using CodeFirst Entitty framework 5. I have a class representing a user. public class User { [Key] public int UserId { get; set; } [Url] [DataType(DataType.Url)] [Required(AllowEmptyStrings= true)] public string WebSite { get; set; } [Phone] [DataType(DataType.PhoneNumber)] [Required(AllowEmptyStrings = true)] public string Phone { get; set; } [Phone] [DataType(DataType.PhoneNumber)] [Required(AllowEmptyStrings = true)] public string Fax { get; set; } } I like the validation mechanics for

DataAnnotations MaxLength and StringLength for string[] or List<string>?

泄露秘密 提交于 2019-12-04 09:48:14
I am trying to figure out if there is a proper way to achieve this with DataAnnotations: Have an array or List of strings where the maximum number of elements in the array or List is 2 items and where each string may only be, say 255 characters long. Will this work: [MaxLength(2)] [StringLength(255)] public string[] StreetAddress { get; set; } I would rather not have to make a new class just to hold a string Value property to constrain each string to 255 characters. You can create your own validation attribute by inheriting from Validation Attribute like described here: How to: Customize Data

Error message for Index data annotation in EF

我们两清 提交于 2019-12-04 09:19:27
Hi I am using Entity Framework 6.1.1 which supports the Index data annotation feature in it. I have a field defined in my entity class as: [Index("scoreIndex", IsUnique=true)] public int score{ get; set; } This is working fine. However, I am trying to figure out how to display a message when the score is not unique. Right now it just throws an exception. I tried the following [Index("scoreIndex", IsUnique=true, ErrorMessage="Score must be unique")] But it does not contain the definition for ErrorMessage for this Index annotation class. Can you please tell me how to handle the exception message

How can I create a generic UniqueValidationAttribute in C# and DataAnnotation?

本小妞迷上赌 提交于 2019-12-04 08:23:05
问题 I'm trying to create a UniqueAttribute using the System.ComponentModel.DataAnnotations.ValidationAttribute I want this to be generic as in I could pass the Linq DataContext, the table name, the field and validate if the incoming value is unique or not. Here is a non-compilable code fragment that I'm stuck at right now: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.ComponentModel.DataAnnotations; using System.Data.Linq; using System

Error messages for model validation using data annotations

旧街凉风 提交于 2019-12-04 08:19:09
问题 Given the following classes: using System.ComponentModel.DataAnnotations; public class Book{ public Contact PrimaryContact{get; set;} public Contact SecondaryContact{get; set;} [Required(ErrorMessage="Book name is required")] public string Name{get; set;} } public class Contact{ [Required(ErrorMessage="Name is required")] public string Name{get; set;} } Is there a clean way I can give a distinct error message for each instance of Contact in Book using DataAnnotations ? For example, if the

data annotations hide property/field

给你一囗甜甜゛ 提交于 2019-12-04 08:10:16
I have a model class Address { public int AddressID {get;set;} public string Street {get;set;} public string City {get;set;} public string State {get;set;} public int ZipCode {get;set;} } in my view, when I have @Html.LabelFor(model => model.Address) (assuming Address is a complex property inside another model) I get a label for every one of Address properties, so I get: AddressID: Street: City: State: ZipCode: problem is, I don't want the ID property to show up, I tried these two annotations: [Display(AutoGenerateField = false)] [HiddenInput(DisplayValue = false)] but the first one doesn't do

ASP.NET MVC Model/ViewModel Validation

℡╲_俬逩灬. 提交于 2019-12-04 08:09:10
I have model classes in Linq-to-Sql with partial classes marked with data annotation attributes and a reference to xVal. When I bind a view directly to a model everything works great, both the JS generated by xVal and server side double check. Many of my views don't take input to one specific model, so I am setting up view model classes. Instead of exposing an entire model instance I expose properties into the model that I allow/need to be set by the view. // foo model public class Foo { public string FooField { ... } public Bar Bar { ... } } // bar model, where bar is a parent relationship of

ASP.NET MVC4 Multi-lingual Data Annotations

≡放荡痞女 提交于 2019-12-04 08:01:06
问题 In a standard application I have the following: [Required] [DisplayName("Email Address")] public string EmailAddress { get; set; } ...this in turn generates a label for this form field automatically in English. Now, if I need my app to support 5 languages, what is the best approach from a ASP.NET MVC application to handle this? Scope of application is about 400 - 600 data fields. UPDATE: I will also require support for updating small sections of text in the application like page names and