How do I use System.ComponentModel.DataAnnotations.AssociationAttribute - asp.net-mvc-3

Some time ago I asked this question: What is the purpose of each of the System.ComponentModel.DataAnnotations attributes?
However, I had no luck with getting replies. This question was a bit broad in the sense that it asked for documentation about every dataannotation attribute. At this moment, I am mostly interested in the Association attribute.
I am using ASP.NET MVC3 with Entity Framework 4 and would like to annotate my POCOs. I am using foreign keys in my POCOs (somehow feels wrong, but it seems to be generally accepeted). How do I annotate my POCO with the Association attribute? On which properties do I put it (Association property and/or foreign key property)? What are the thisKey and otherKey parameters. Is thisKey this POCOs key, or the foreign key in this POCO?
Then lastly, what will use this attribute? Is there something in ASP.NET MVC?
Thanks in advance!

Be aware that not all attributes provided in DataAnnotations namespace are related to Entity framework. I thought that AssociationAttribute is used in Linq-to-sql but it is actually different class with the same name from System.Data.Linq assembly. I just checked usage of AssociationAttribute by Reflector and it looks like neither Entity Framework (including code first), ASP.NET MVC, ASP.NET Dynamic data or WPF use it.

The AssociationAttribute (in conjuction with the ExternalReferenceAttribute) is used by WCF RIA Services in Silverlight. With this attribute the client can resolve associations between entities from diffrent Domains.
I think it is specific to RIA Services. HereĀ“s a walkthrough how it gets applied in WCF RIA Services.
A simple Example looks like
public class Customer{
public int Id {get;set;}
}
public class Order{
public int Id {get;set;}
public int CustomerId {get;set;} //ForeignKey of the Customer
[ExternalReference]
[Association("Customer", "CustomerId", "Id")]
public Customer {get;set;}
}

Related

Add attributes to Entity Framework database first generated classes

I am using Entity Framework 4.1 and generating my classes using the database first approach. I have a an EDMX file in my solution.
I am trying to add attributes to my classes using the MetadataTypeAttribute approach which seems to be the recommended way of doing this outside of editing the T4 template, however, I cannot seem to get it to work because I keep getting this compile error:
'Patient' is an ambiguous reference between 'PatientManagementSystem.Patient' and 'PatientManagementSystem.Models.Patient'
Here is the code I am using:
[MetadataTypeAttribute(typeof(PatientMetadata))]
public partial class Patient
{
}
public class PatientMetadata
{
[Required]
public string LastName {get; set;}
}
Is this error happening because I don't actually have classes for this because I am doing Database first and everything in the EDMX?
Thanks!
Flea
Make sure both are in the same namespace. Since it's a partial class, the class you are modifying must also be in the same namespace or it isn't the same class.

Validate attributes through edmx

I'm working on an MVC3 project I have created my database on SQL server 2008 then I add it using entity data model, now I have the .edmx I don't want to generate the code of the classes because till now it's not stable and I don't feel the need to do that except to make the validations
I'm wondering if there is a way to make validation directly using the .edmx without generating the code of classes
Need server and client side validation
Cheers
You can't do it from edmx. You can however have classes that add the metadata to the edmx generated classes.
This adds a metadata containing class to the class that was generated by the edmx:
[MetadataTypeAttribute(typeof(CustomerMetadata))]
public partial class Customer
{
}
and then you have the metadata class which has the metadata attributes:
internal sealed class CustomerMetadata
{
public int ID;
[Required]
[StringLength(60)]
public string Name;
}
Edit:
EF generates all it's classes as partial. That means you can add functionality in a different file to the same class. We use this feature to add an attribute telling .net that there's a class that has the metadata information. In this case the class with the metadata for the Customer class is CustomerMetadata.
It has all the properties that you want decorated with metadata. In this case ID is not actually required to be there.
(I would have added this as a comment but I don't have enough reputation points yet)
Be sure that the namespace in your partial class exactly matches the EF-generated class's namespace, including the correct case.

Defining data annotation using DbContext versus Objectcontext in the database first approach

I am using the database first approach with entity framework, when i used to work on the default template the database tables were mapped using the ObjectContext, so i used to create #partial classes & [MetadataType(typeof ) to apply the data annotation ,, but when i start using the Dbcontext code generation template to map the database tables i found that it will create .tt folder in my Model area were i find that i can apply the data annotation directly to the .cs classes themselves without the need to create partial classes as in objectcontext case.
Currently the data annotations are working fine,, but would my approach cause me problems i am not aware of and i should create partial classes as i used to do with the Objectcontext ?
BR
In general, you shouldn't edit generated code because changes you make will be overwritten on re-generation. This is why most generators emit partial classes.
The best practice for your situation would be to create a new file in your solution with another partial class declaration. In that file, add the MetadataType attribute to the class, and add your property-level validation attributes to the "buddy" class (the one referenced in the attribute). This allows you to use validation attributes on the generated properties and, should your model/database change, you can still re-generate your model classes without losing them.
For example, your new file might look something like:
[MetadataType(typeof(PersonMetadata))]
partial class Person
{
// Add logic to the generated class in here.
public string FullName
{
get { return FirstName + " " + LastName; }
}
}
class PersonMetadata
{
// Add attributes to the generated properties in here.
[Required]
public string FirstName { get; set; }
}
Create the same partial classes to define your metadata OR you can simply reverse engineer your existing database using the Entity Framework Power Tools so you have POCO classes. Then you can use the fluent API (you'll see the validations it adds for you) instead of data annotations to give you server side validation.
If you want client side, then you can still apply them to your models, as they wont be regenerated every time you compile .
however - I would recommend you create ViewModels and use AutoMapper to map between your EF objects and viewmodels. Then you can apply your annotations directly to your ViewModels.
My understanding of your situation is that you reverse-engineered your Database-First style to a Code-First style. If your context class inherits from DbContext, you are in Code-First style now (unless there is some strange hybrid possible, which I don't know about!).
In code-first, there is really no point of creating partial classes for data annotations, IMO.

ASP.Net MVC3, DataAnnotations and Dynamic Validation

I'm creating an MVC 3 application that needs to establish data validation rules at runtime based on external data (e.g. Required, MinimumLength, MaximumLength). It seems natural to use Data Annotations in MVC 3, however the property attributes that provide validation metadata are set at compile-time.
Is there a pattern to use Data Annotations with metadata provided at runtime?
Example:
public string Text { get; set; }
public void SetIsRequired(string propertyName, bool required)
{
// Somehow find the property 'propertyName' and create/remove a RequiredAttribute
// on that property
}
...
SetIsRequired("Text", true);
I'm aware of TypeDescriptor, but don't see an option to modify attributes of a property of an instance (only class level attributes on an instance, or property level attributes for a type).
It seems natural to use Data Annotations in MVC 3
Not for me. I never really liked data annotations due to their declarative nature. And doing validation in a declarative way limits capabilities. I have always liked and use FluentValidation.NET.
You could probably use the IDataErrorInfo interface (which MVC can consume) to write your custom, dynamic, validation rules.

ADO.NET Entities - Model-first data annotations

I'm using the approach explained here:-
http://blogs.msdn.com/b/adonet/archive/2010/12/06/ef-feature-ctp5-code-first-walkthrough.aspx
I'd like to use the surface designer to develop my model, then use partial classes to include data annotations for validation. However, if I use a partial class and then attempt to reference an existing property the compiler complains that the property already exists.
How do I get over this?
e.g.
public partial class Product
{
[Required(ErrorMessage="Name is required")]
public string Name { get; set; }
}
The compiler says "Name is already defined".
I just ran across the same problem on how to annotate an entity class that is generated from the Entity Data Model and I found the answer on another stackoverflow thread:
Using System.ComponentModel.DataAnnotations with Entity Framework 4.0

Resources