What is the best way to use data annotations for validation if I'm using an Entity Framework (v5.0) database first approach?
This is my partial class created by Entity Framework:
//------------------------------------------------------------------------------ // <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> //------------------------------------------------------------------------------ using System.ComponentModel.DataAnnotations; namespace ACore { using System; using System.Collections.Generic; public partial class PayrollMarkup_State { [UIHint("StatesEditor")] // <-- I added this line but it will be overwritten public string State { get; set; } public Nullable<float> MaintenancePercentage { get; set; } public Nullable<float> OfficePercentage { get; set; } } }
I tried this with no success....
Entity Framework generated file: 'PayrollMarkup_State.cs'
//------------------------------------------------------------------------------ // <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> //------------------------------------------------------------------------------ using System.ComponentModel.DataAnnotations; namespace ACore { using System; using System.Collections.Generic; public partial class PayrollMarkup_State { public string State { get; set; } public Nullable<float> MaintenancePercentage { get; set; } public Nullable<float> OfficePercentage { get; set; } } }
I then created this file in a different directory: 'PayrollMarkup_state.cs'
using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Web; namespace ACore.Models { [MetadataType(typeof(PayrollMarkupMetadata))] public partial class PayrollMarkup_State { } public class PayrollMarkupMetadata { [UIHint("StatesEditor")] public string State; // Has to have the same type and name as your model } }
Versioning databases is hard, but with code first and code first migrations, it's much more effective. Because your database schema is fully based on your code models, by version controlling your source code you're helping to version your database.
The fluent API is considered a more advanced feature and we would recommend using Data Annotations unless your requirements require you to use the fluent API.
DataAnnotations is used to configure the classes which will highlight the most commonly needed configurations. DataAnnotations are also understood by a number of . NET applications, such as ASP.NET MVC which allows these applications to leverage the same annotations for client-side validations.
Although it's somewhat painful, you need to create a class to use as the MetadataType
for your model class.
[MetadataType(typeof(PayrollMarkupMetadata)) public partial class PayrollMarkup_State { ... } public class PayrollMarkupMetadata { [UIHint("StatesEditor")] public string State; // Has to have the same type and name as your model // etc. }
You have a namespace problem - you have defined two different PayrollMarkup_State classes, one under the ACore namespace and one under the ACore.Models namespace. Change the namespace to ACore (from ACore.Models) in the file containing the metadata type definition.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With