Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Annotations with Entity Framework 5.0 (database first)

Tags:

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     } } 
like image 401
Mithrilhall Avatar asked Mar 25 '13 18:03

Mithrilhall


People also ask

Which is better code first or database first in Entity Framework?

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.

Should I use Fluent API or data annotations?

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.

What are data annotations in Entity Framework?

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.


2 Answers

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. } 
like image 112
GalacticCowboy Avatar answered Sep 28 '22 20:09

GalacticCowboy


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.

like image 28
Moho Avatar answered Sep 28 '22 22:09

Moho