Custom Validation with MVC2 and EF4 - asp.net-mvc

on ScottGu's Blog is an Example how to use MVC2 Custom Validation with EF4:
http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
So here the Problem:
When the Designer in VS2010 creates the Objects for the DB, along to the example you have to add [MetadataType(typeof(Person_validation))] Annotation to that class.
But when i change anything in the Designer all these Annotations are lost.
Is it possible to keep self made changes to the edmx file, or is there any better way of applying System.ComponentModel.DataAnnotations to the generated Entities?
Thanks.

You do it with a pattern loosely called "buddy classes". Basically what you do is create a separate class with your metadata, and create a partial class that couples the generated entities to your buddy class.
For a simple example, let's say you have a Person entity, and you want to set the FirstName property to be required. This is what you'd do outside of your generated files:
[MedadataType(typeof(PersonMetadata))]
partial class Person { } // the other part is generated by EF4
public class PersonMetadata
{
// All attributes here will be merged into the generated class,
// thanks to the partial class above. Just apply attributes as usual.
[Required]
public string FirstName { get; set; }
}
You can find more details on this approach here. And ScottGu actually talks about it too, in the article you linked to. Look under the headline "Step 5: Persisting to a database" ;)

Related

Dynamically change models and controllers after publishing website in ASP.NET Core MVC

I'm using ASP.NET Core MVC 2. I need to operator can change some elements of Models or view codes. How I can code or design for it.
For example: I have a "news" model and I want to operator (final user of website, who can't code or access to visual studio) can add this to "news" model:
public string ImageUrl { get; set; }
and also can change the database without coding.
Thanks
If you want to design a completely extensible model, you could use something called Entity–attribute–value model (EAV).
Your model might have a couple common attributes like Title and Summary. Then you might have a list of Custom Fields, the first of which could be ImageUrl. You could create your own class called CustomField or something similar, which would have properties such as FieldName, and DataType.
public string Title { get; set; }
public string Summary { get; set; }
public List<CustomField> CustomFields { get; set; }
You would then have a table full of custom field values and the tables they belong to. It gets pretty complex.
When you want to automatically reflect your model changes to the database, you will need an ORM framework like EF (Entity Framework). You can check more here.
In order for your case to happen is to build your own configuration platform that may use several tools and mechanincs that will allow you to generate code and then compile it. Such as T4 and more.
In general, this is a very hard task to accomplish and even big experienced teams would have troubles to build something similar.
I can not post any code, as this would only seem a desperate approach.

Adding validation messages to MVC classes generated by Entity Framework

I am starting an MVC project and designing my DB in EF, which means I design the tables, and VS creates the classes I need to access them.
The problem is, I want to make use of attributes like DisplayName, Required and generating validation error messages ( including specifying rules to validate ).
As far as I can see, the classes are recreated every time I change my DB, so I can't really add them to the classes. Is there another way to do this once and have it persist ?
So you would use the MetadataType attribute and link your entity to a type where you'll set the validation attributes.
Something like this for an Entity Person:
[MetadataType(typeof(Person_Validation))]//<<link to metadata class
public partial class Person//<<<Your real entity class
{//this is in a separate file.
//note =>partial. There's nothing in this class
}
public class Person_Validation//the validations go here.
{
[StringLength(255, ErrorMessage="Name is required"), Required]
[DisplayName("Name")]
public string Name { get; set; }
}

MVC 3 - Entity Framework - Scaffolding - Validation issue

Im developing an MVC 3 application with Entity Framework and Im tring to use Scaffolding.
To solve "Type not mappedd issue" I've done the procedure found here. Everything now works fine.
Default validation is not working, required field are firing an exception instead of write something on ValidationSummary, so I want to add my custom validations using attributes.
The problem is that the solution about "type not mapped issue" has added 2 .tt files and a .cs file for each of my entities, these files are recreated each time my model (.edmx) is changed and saved so I cant put my Data Annotation Validator Attributes in those classes and either I cant create a new partial class with some properties because thay are already defined.
How can I do? May I have to move validation client-side using jquery? Or maybe there a workaround to add Data Annotation Validator Attributes to my entities, I prefer this way to have more visibility of my validations.
Thanks in advance
I've not used the DbContext generator, but have had similar issues with the POCO Generator. Assuming that the solution is similar:
Modify the T4 template that creates the entity classes to add an extra attribute to the class:
[MetadataType(typeof(CustomerMetaData))]
where "Customer" is the name of the entity.
Then, manually create MetaData classes for each of your entities. You can actually use a T4 template for that, too, if you want, but not have it run all the time.
The Metadata classes look like this...
public class CustomerMetaData
{
[StringLength(150, ErrorMessage="Maximum length is 150 characters.")]
[Required(ErrorMessage="CustomerName is required.")]
public virtual string CustomerName
{
get;
set;
}
public virtual Nullable<int> Type
{
get;
set;
}
// ... etc ...
}
As you can see, you attach the rules to the MetaData class, thus abstracting it from the generated entity class.

ASP MVC3 Database-first

I use the entity framework for application ASP MVC3. At first I using code-first approach. I created the classes and used attributes to validate the data field
public class Person
{
public int ID { get; set; }
[Required(ErrorMessage = "Name can not be empty")]
public string Name { get; set; }
}
But when using database-fitst, I do not know how to validate the datafields.
In this case class Person is automatically created. How to do validate of its data fields?
Here's my $0.02 worth. If you want to validate your model which has been generated by entity framework using the Database first approach then you have to make use of a concept called 'Buddy' class. I believe Scottgu has a great article on that. As you can see the model classes generated by Entity Framework are partial classes meaning to say you can also create your own partial class to hold the so called attributes or to describe the metadata of the generated model. These partial classes will then be combined to form one class at runtime. Please do check out ScottGu's blog
here: http://weblogs.asp.net/scottgu/archive/2010/01/15/asp-net-mvc-2-model-validation.aspx
Hope this answers your question.
Hard to understand what exactly you mean, but I can recommend reading this.
The concept of Code First is simple:
You create the classes. In your classes you can use the Required attribute just like you would with the normal Entity Framework
EFCodeFirst creates the database tables for you.

Using EF POCO classes as MVC 2 models (with data annotations)

I have a 4 layered web application programmed in C#... .Net 4.0:
UI Layer
Business Layer
Data access Layer
Entities layer
My data layer contains an edmx
My entities layer contains my POCO objects (generated by a t4 script), and that layer is referenced in all other layers.
When creating an MVC form to create a new customer, for example.... I already have the customer class with fields for first name, last name, etc in my entities layer, but that auto-generated POCO class does not have data annotations for validation... I.E. [Required], etc. for when the form is submitted
My solution right now is to create new model classes that are pretty much the same as my poco classes but also have these additional validation annotations.
What I want to know is if theres an easy way to use certain POCO objects in the MVC model (in the UI layer) without having to almost rewrite the class... and also without modifying the t4 that generates these POCO classes (since I'm not up to speed on t4).
I saw this from another post on stackoverflow http://automapper.codeplex.com/ ... not sure if this will do it or is the best solution.
If your POCO class is declared as such:
public class Person {
public string FirstName { get; set; }
public string LastName { get; set; }
}
then if you just change the T4 to make it a partial class, you can then define in a separate file:
[MetadataType(typeof(PersonMetadata))]
public partial class Person {
internal class PersonMetadata {
[Required]
// insert other metadata here
public string FirstName { get; set; }
// and if you don't want metadata for lastname, you can leave it out
}
}
Two extra points - the metadata class doesn't have to be nested in the partial you define, I think it's neater though. Also, the types don't have to match in the metadata class, so you could make them all object if you wanted to (and you might see some examples on the web with it like this)
Modifying a T4 template is not very hard at all. I recently faced the same issue and decided to read up on T4 a bit and then modify the template to create the generated properties the way I need them (annotations, and in my case with NotifyPropertyChange etc. as I use the same POCO objects in an MVC UI and in a Silverlight UI).
Even though you're looking for a solution that doesn't require modifying T4, I hope this is useful.

Resources