data-annotations

How do I specify that a property should generate a TEXT column rather than an nvarchar(4000)

与世无争的帅哥 提交于 2019-11-26 13:56:01
问题 I'm working with the Code First feature of Entity Framework and I'm trying to figure out how I can specify the column data types that should be created when the database is auto-generated. I have a simple model: public class Article { public int ArticleID { get; set; } public string Title { get; set; } public string Author { get; set; } public string Summary { get; set; } public string SummaryHtml { get; set; } public string Body { get; set; } public string BodyHtml { get; set; } public

Annotating properties on a model with default values

我怕爱的太早我们不能终老 提交于 2019-11-26 13:08:56
问题 I created an EF4.1 code-first model (may or may not be important), and I\'m trying to get default values for my Create scaffold template. My model looks like: class Person { [DefaultValue (18)] public int Age { get; set; } } And then my Create view looks like: <div class=\"editor-label\"> @Html.LabelFor(model => model.Age) </div> <div class=\"editor-field\"> @Html.EditorFor(model => model.Age) @Html.ValidationMessageFor(model => model.Age) </div> I would expect at runtime, that the EditorFor

How does DataAnnotations really work in MVC?

流过昼夜 提交于 2019-11-26 12:57:17
问题 This is more of a theoretical question. I\'m currently examining the MVC 3 validation by using ComponentModel.DataAnnotations, and everything works automagically, especially on client side. Somehow something checks for those attributes, and generates javascript for the validation (or html5 attributes, if using unobtrusive mode), and it works. My question is that what generates the client side javascript and how can I access and modify it? For example I want to handle the given dataannotation

Entity Framework Code First Fluent Api: Adding Indexes to columns

纵然是瞬间 提交于 2019-11-26 12:50:06
问题 I\'m running EF 4.2 CF and want to create indexes on certain columns in my POCO objects. As an example lets say we have this employee class: public class Employee { public int EmployeeID { get; set; } public string EmployeeCode { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public DateTime HireDate { get; set; } } We often do searches for employees by their EmployeeCode and since there are a lot of employees it would be nice to have that indexed for

Why is DisplayFormat DataFormatString not working?

|▌冷眼眸甩不掉的悲伤 提交于 2019-11-26 12:44:23
问题 I have a property in my view model as follows: [Editable(false)] [Display(Name = \"Date\")] [DisplayFormat(DataFormatString = \"{0:yyyy/MM/dd}\", ApplyFormatInEditMode = true)] public DateTime MovementDate { get; set; } Yet the markup <td> @Html.DisplayFor(modelItem => item.MovementDate) </td> renders the date value as 2013/05/15 12:00:00 AM . What am I doing wrong? My model: public class WithDateModel { [DisplayFormat(DataFormatString = \"{0:yyyy/MM/dd}\")] public DateTime TheDate { get; set

How can I tell the Data Annotations validator to also validate complex child properties?

ぐ巨炮叔叔 提交于 2019-11-26 12:17:18
问题 Can I automatically validate complex child objects when validating a parent object and include the results in the populated ICollection<ValidationResult> ? If I run the following code: using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; namespace ConsoleApplication1 { public class Person { [Required] public string Name { get; set; } public Address Address { get; set; } } public class Address { [Required] public string Street { get; set; } [Required]

Why can&#39;t I reference System.ComponentModel.DataAnnotations?

拟墨画扇 提交于 2019-11-26 10:59:44
问题 I\'m trying to use DataAnnotations in my WPF project to specify a maximum length of strings, with the following: using System.ComponentModel.DataAnnotations; However, I get the error The type or namespace name \'DataAnnotations\' does not exist in the namespace \'System.ComponentModel\' (are you missing an assembly reference?) I\'ve seen other examples where DataAnnotations does exist in this namespace. I\'m using C#4. Is there any reason why I can\'t use this? What can I do to fix it? 回答1:

DataAnnotations: Recursively validating an entire object graph

自作多情 提交于 2019-11-26 10:57:03
问题 I have an object graph sprinkled with DataAnnotation attributes, where some properties of objects are classes which themselves have validation attributes, and so on. In the following scenario: public class Employee { [Required] public string Name { get; set; } [Required] public Address Address { get; set; } } public class Address { [Required] public string Line1 { get; set; } public string Line2 { get; set; } [Required] public string Town { get; set; } [Required] public string PostalCode {

Mixing Fluent API and DataAnnotations in EF code first

不打扰是莪最后的温柔 提交于 2019-11-26 09:59:29
问题 While we mostly use fluent configuration for our code-first POCOs, we have found it useful to use data annotations for things like the table name, PKs, etc. since it makes it easier for non-EF components that don\'t have a reference to the ObjectContext to interact with these entities. In our experience, it seems that the two configuration styles can be mixed freely, with fluent configuration overriding DataAnnotations. Is this documented anywhere? Is there any risk to doing this mixed

DateTime (date and hour) validation with Data Annotation

被刻印的时光 ゝ 提交于 2019-11-26 09:52:51
问题 I have the following code: [DisplayName(\"58.Date and hour of birth\")] [DataType(DataType.DateTime, ErrorMessage = \"Please enter a valid date in the format dd/mm/yyyy hh:mm\")] [Range(typeof(DateTime), \"1/1/2011\", \"1/1/2016\")] [RequiredToClose] public object V_58 { get; set; } I want to force the inclusion of time (in format hh:mm) and not only the date. This code considers 1/1/2011 as valid when it shouldn\'t as it does not containt the hour , Any clue about how to express the correct