0

In my solution there are total three projects. Two of them is web application in ASP.Net MVC and one is C# class library. Both web applications use same DB so I created C# class library and put ADO.Net Entity Data Model(EDMX) in it. I added reference of C# class libray project in web applications and using ADO.Net Entity Data Model to access database. This working properly, I am able to access table, add/edit/delete records but issue comes when I am trying to write data annotations for validation. Class generated by EDMX in C# class library project for table is like this:

namespace EDMXProjectNamespace
{
     using System;
     using System.Collections.Generic;

     public partial class DrugClass
     {
        public DrugClass()
        {
           this.Drugs = new HashSet<Drug>();
        }

        public int ID { get; set; }
        public string Name { get; set; }
        public string NName { get; set; }

        public virtual ICollection<Drug> Drugs { get; set; }
     }
}

For connection I added connection string generated in my C# class library project's App.config to Web.config so it's working fine. Now only issue is where to write data annotations for validating user input.

Thanks in advance....

CodeWarrior
  • 763
  • 3
  • 14
  • 33
  • 1
    As I know for the MVC there is a trick, look here - [DataAnnotations with Entity Framework](http://stackoverflow.com/questions/4915957/using-system-componentmodel-dataannotations-with-entity-framework-4-0/) But as you are using separate class library maybe that would be of use [Where to put EF DataAnnotations](http://stackoverflow.com/questions/4496526/where-should-i-put-the-ef-entity-and-data-annotations-in-asp-net-mvc-entity-fr) – Den Jul 24 '14 at 11:57
  • Thank for reply. But where do I put data annotations and create buddy classes so that even EDMX file is regenerated my data annotations remain there. – CodeWarrior Jul 24 '14 at 12:20
  • You put them in separate file (so that when you regenerate your model they're not affected) - partial class look the answer of first link, put it in separate file – Den Jul 24 '14 at 12:26

1 Answers1

0

Thanks all for your valuable replies.

Finally I found solution for this problem. Instead of creating partial classes and add data annotations there, I created ViewModels for my views and added my data annotations there. With ViewModel I am able to validate my user's input.

CodeWarrior
  • 763
  • 3
  • 14
  • 33