I am trying to get EF 4.1 to work with the repository, UnitOfWork, split entities from EF and validate.
I followed this guide to get a good separation of my POCO objects from the EF model, and now I follow this guide to implement validation (using the IValidatableObject method).
My solution consists of:
- Contacts.Repository [EF and Contacts.Entities links]:
- Contacts.edmx
- ContactsDbContext.cs
- Contacts.Entities [no links]:
- Contact.cs (Contacts.Entities.Contact partial class)
- Contacts. Validation [Contacts.Entities and Contacts.Repository links]
- Contact.cs (Contacts.Entities.Contact partial class)
But I hit a brick wall with a check:
- I cannot add validation logic to Contacts.Entities because it will cause a circular link from Contacts.Repository (contact.Validate (...) you must use ContactsDbContext). Therefore, I created a separate Contact.Validation project.
- But this means splitting the Contact class with partial classes to define a contact inside both contacts. Contacts and contacts. The code no longer compiles because you cannot define a partial class for different assemblies.
Does anyone have any pointers for me here? I sent the code below ...
Contacts.Repository.ContactsDbContext.cs:
namespace Contacts.Repository { public partial class ContactsDbContext : DbContext { public DbSet<Contact> Contacts { get; set; } protected override DbEntityValidationResult ValidateEntity(DbEntityEntry entityEntry, IDictionary<object, object> items) { items.Add("Context", this); return base.ValidateEntity(entityEntry, items); } } }
Contacts.Entities.Contact.cs:
namespace Contacts.Entities { public partial class Contact { public string Name { get; set; } } }
Contacts. Validation.Contact.cs contains:
namespace Contacts.Entities { public partial class Contact : IValidatableObject { public IEnumerable<ValidationResult> Validate(ValidationContext validationContext) { ContactsDbContext contacts = (ContactsDbContext)validationContext.Items["Context"];
source share