EntityGraph with entity framework

Jun 22, 2011 at 3:12 PM

Hello,

  I am trying to use entity graph with the entity framework by defining the entity graph shapes with entity collections. I am following the documentation and adding the validation classes also but it does not seem to run the validations. I have kept a breakpoint and it is not hitting at all. Please help.

Thanks,
Ajay. 

Developer
Jun 22, 2011 at 3:20 PM

Hi Ajay,

Could you share your code? That would make it much easier for me to help you.

Kind regards,

Merijn

Jun 22, 2011 at 3:33 PM
Edited Jun 22, 2011 at 3:54 PM

Hello Merijn,

      Thanks for the reply. Below is the part of code where I have my entity collections, entitygraphshape and validations defined

Entity Collection Properties of the client object:

            public EntityCollection<client_assessments> client_assessments { get; set; }

            public EntityCollection<client_medical_cd4> client_medical_cd4 { get; set; }

 Partial shared class:

        private static EntityGraphShape _shape =    new EntityGraphShape().Edge<client, client_medical_cd4>(client => client.client_medical_cd4).Edge<client, client_assessments>(client => client.client_assessments);

        public static EntityGraphShape Shape { get { return _shape; } }

Shared Validations class:

    [CLSCompliant(true)]
    public class ClientMedicalCD4Validator : ValidationRule<client_medical_cd4>    {       

       public ClientMedicalCD4Validator()            : base(InputOutput<client, IEnumerable <client_medical_cd4>>(client => client.client_medical_cd4))        {


        }
        public ValidationResult Validate(IEnumerable<client_medical_cd4> clientMedicalCD4Collection)        { 

          return new ValidationResult("Test failure.");       

       }
    }

And in the silverlight application, I am doing the following :

            client CurrentClient = ChampClientContext.clients.Where(c => c.guid == new System.Guid("C6D5E7AB-4C13-49B8-835D-01C99C9544ED")).FirstOrDefault(); 
          client_medical_cd4 CurrentCD4 = new client_medical_cd4(); 
          CurrentClient.client_medical_cd4.Add(CurrentCD4); 
            MEFValidationRules.RegisterAssembly(typeof(client).Assembly);
            var graph = CurrentClient.EntityGraph(client.Shape);

I hope I am making some sense, otherwise let me know I will try to send you the class files.

Thanks,
Ajay. 

Developer
Jun 22, 2011 at 7:26 PM

Hi,
Thanks for providing the code.


I think the problem is caused by the fact that you derive ClientMedicalCD4Validator from ValidationRule<client_medical_cd4> where it should derive from ValidationRule<ValidationResult> (if you're using RIA). Your validate method also returns an object of this type.

The thing is that on the lowest level, ValidationRule is a generic type. Meaning that you can use the validation framework to return all kinds of validation results. However, for RIA services I created an instantiation of this validation framework, where the type parameter TEntity is instantiated by the type System.ServiceModel.DomainServices.Client.Entity and TResult by System.ComponentModel.DataAnnotations.ValidationResult.

Now, if you write your validation rule client-side only, you could simply define a class ClientMedicalCD4Validator : ValidationRule. Since you defined it server-side you can't make use of the RIA-specific instantiation and therefore you have to derive your validation rule from ValidationRule<ValidationResult>.

To be honest I have not tested this scenario. It might be that you now also need to instantiate the base version of EntityGraph instead of the RIA specific one. This means, instantiating it like this: CurrentClient.EntityGraph<Entity>(client.Shape). But I'm not sure.

Let me know if this works or if you still encounter problems.

 

Good luck,

Merijn

Jun 23, 2011 at 3:52 PM

Thank you Merijn. It is working perfectly. Thanks for taking time and looking into my code. 

Thanks,
Ajay.