I am considering using xVal for validation of Entity Framework classes in a MVC application. This involves writing metadata classes as explained in details by Graham O'Neale (http://goneale.com/2009/03/04/using-metadatatype-attribute-with-aspnet-mvc-xval-validation-framework).
I am wondering if there's a way to auto generate such metadata classes using the metadata from the SQL database (for example: not null fields will have [Required] class attribute.
You could use Code Smith tool www.codesmithtools.com (there is a free version if I remember correctly)
I wrote an application that will read an existing database and then generate a Data Annotation Class for every table (excluding aspnet_* and VersionInfo).
http://pfsolutions-mi.com/Product/MetaDataClassGenerator
In EF 4, you can easily customize the code generation via a T4 template.
Related
I am currently developing project using C# MVC and entity framework, I want to use the entity framework in other modules ,
i.e security module , Utility module ,
i want to call the db using the entity frame work, how do i do this ?
i am new to this are please explain in detail, idea is to break the project into presentation layer, business layer and data access layer..
i don't know how to archive this.
Try this way,
There are three ways to work around entity framework, Database First, Model First & Code First.
Database First: If you already have database, then entity framework can generate a data model that consists of classes & properties that correspond to existing database objects such as tables & columns.
The information of database structure, conceptual data model & mapping between them is store in the xml in an .edmx file.
Model First: If you don't have database, you can start creating model using vs entity framework designer. This approach also use the .edmx file.
Code First: In this approach, we don't need .edmx file. Mapping between store schema & conceptual data model is represented by code, handled by code convention & special mapping API.
Here I have used the Database First approach.
In order to use the Dal class lib, add the reference in the business logic layer and initialize the entities class. For example
Find the entity framework object.
Initialize the entity framework object in other class lib.
FrameworkEntities entities = new FrameworkEntities();
Please let me know, if you want to use model first or code first approach.
Get started from the below link for the Entity Frame Work
http://msdn.microsoft.com/en-US/data/ee712907?
I have generated xxxModel.Context and xxxModel template (in Models) from an existing database using EF 4.1 on ASP.Net MVC 4 application. After that when I build the project it gives the error for all the Model classes (POCO) saying "The type file name already contains a definition for memeber variable name". Where am I going wrong?
Thanks for help.
Clearly the file DESE.cs (and others) already contains identically named classes with properties - which are clashing with the types generated by EF. CC_Names.cs, for example, is being output by a text template - so I'm assuming you've got more than one code-generation strategy going on here from the same database.
And then you also have issues where you've re-declared the partial class CorpCostEntities again in another file with a different base to the one set by the EF code generator.
I think you might need to decide whether you want to use edmx code generation or the text-templating approach and stick to it :)
I'm building an application using MVC 3 and Entity Framework 4.
I've created my Entity Data Model and generated a database from it.
Now I know the validation attributes such as [Required] or [StringLength(5)] can be used on the model properties to provide validation both clientside and serverside.
I would like to know if these attributes can also be generated dynamically instead of having to add them to the model explicitly? I saw that in EF 4.1 RC you can make use of the Fluent API to further configure your model in the OnModelCreating method by using the DbModelBuilder class.
As shown here
I'm working with a framework however that still uses ObjectContext instead of DbContext so I would like to know if the above solution can be used in combination with ObjectContext?
As a final note, since I've been trying to figure out how to generate and use data annotations it seems using view models would increase the complexity of validation. From what I read here it seems that just passing the models directly to the view would remove the need to add annotations to the models as well as the view models. However that means that you can no longer use strongly typed views when you do joins on the models and pass those to the view directly?
No it can't. Fluent API is different approach to describe mapping. You can use fluent API or EDMX (Entity Data Model). Not both. Fluent API also works only with DbContext API. If you want to have annotations generated you can try to modify T4 template generating your classes.
I have come across a disturbing issue when using poco classes that are extending base classes.
For example, let say you have a Person poco class that has a strongly typed Car property. You also have a Spouse poco that also uses the Car Property.
Now you want to display "Person Car" and "Spouses Car" in the view using the Display("Name = xxx") attribute. You cant!!! Becareful of this issue if you are not using flat View Models
I have a load of ADO.NET Entities in my MVC project. I was going to use these entities directly from my views/controllers... however, I think it's probably best to use ViewModels which more accurately reflect what the View requires.
I'm looking for a way that I can auto-generate a ViewModel from an existing Entity, i.e., auto-generate the wrapper or adapter pattern from an existing member... Or a T4 template that would loop through the public properties of an Entity, and output properties for the ViewModel... then I can delete the properties I don't need or create aggregate view models etc.
I cannot seem to find anywhere a way to auto-gen a wrapper or adapter pattern class from an existing type?
The idea is then at runtime, use AutoMapper to map between the ViewModel and the Entity.
thanks
You could use AutoMapper to convert from your domain model to a view model. There's a great post from Jimmy Bogard explaining how you could integrate this within your controller actions.
http://weblogs.asp.net/rajbk/archive/2010/05/04/a-basic-t4-template-for-generating-model-metadata-in-asp-net-mvc2.aspx
That can help. It is actually for metadata generation for existing entity types. But you can use it to generate clean view models with data annotations as well. Maybe with a little modification.
I'm reading several docs on validation in ASP.NET MVC. Ignoring those that suggest to reinvent the wheel writing your own validation logic, most articles advocate the use of xVal or Data Annotation Validators, both of which allow declarative validation through decorating models' properties(*).
I think I'll go for xVal, as it seems to be the most suggested (and thus, I hope, supported). What puzzles me is that I'm using Linq to SQL, and my models are declared in an automatically generated file (dbml + partial.cs), containing several classes and almost 5000 lines long, so I'm not very eager to edit it, as if I change the db structure I'll have to regenerate it, loosing my changes.
Is there a way to avoid editing that file?
(*) I've also found a Validator Toolkit which also provides declarative validation and doesn't request to decorate models' properties, but the page shows 57 downloads, so I can't tell if the project is mature or not.
You can create a partial class and add the Data Annotation Validators to the partial class that way it doesn't matter if your dbml file is regenerated.
Try this asp.net article and this article
Check out buddy classes -- really the only way to fly with generated models and DataAnnotation-based validation.
Yes it can (xVal). See here and here.