0
votes

I have created an MVC 4 application with EF db-first using ADO.NET Entity Data Model.

I've previously been adding data validation and updating constructors directly into the generated Model classes, but as I foresee these tables to be updated I don't want to have to add these all back in, plus I shouldn't be editing these auto generated classes anyway.

Using Metadata.cs and PartialClasses.cs from http://www.asp.net/mvc/tutorials/mvc-5/database-first-development/enhancing-data-validation I'm not sure the best way to update the default constructors for these Model classes.

Here's an example model, simplified. Within .edmx

public partial class Campaign
{
    public Campaign()
    {
        this.Fees = new HashSet<Fee>();
    }

    public int ID { get; set; }
    public string Name { get; set; }
    public string CreatedBy { get; set; }
    public System.DateTime CreatedOnDate { get; set; }

    public virtual ICollection<Fee> Fees { get; set; }
}

within ParticalClasses.cs [errors as the generated Modal class defines the default constructor]

[MetadataType(typeof(CampaignMetadata))]
public partial class Campaign
{
    public Campaign()
    {
        this.Fees = new HashSet<Fee>();

        // Non-Generated 
        this.CreatedOnDate = DateTime.Now;
    }
}

I have other models I would also like to have other constructors with different parameters, so to simplify my question, where do I add constructors for DB first MVC as to no update the generated Model classes?

1
you can apply validation to you viewmodels - same concept as applying to the data model entities. - tintyethan

1 Answers

0
votes

Not 100% sure about what you are trying to do, but I'll try to answer your question.

First of all, it seems that you are missing the point of the MVC: your link refers to view model validators, but you are talking about data models. Two VERY different things. There's nothing to validate in a data model - those change and are govern by what's going on in the database.

This is what I would do:
1) Create a data layer: this would hold all your entity classes.
2) Create a service layer: this will instantiate and populate the entity classes using either raw sql, or a pattern (repository pattern, for exam). 3) Create your website: this will hold your controllers, view models (they are the ones you want to validate) and views.

For your Campaign class:

public interface IEntity
{
   object EntityID { get; set; }
}

public abstract class BaseEntity: IEntity
{
   public abstract object EntityID { get; set; }
}

public class Campaign : BaseEntity
{
   #region Properties

   public int ID { get; set; }

   public string Name { get; set; }

   public string CreatedBy { get; set; }

   public DateTime CreatedOnDate { get; set; }

   public virtual List<Fee> Fees { get; set; }

   #endregion

   #region BaseEntity Implementation

   public override object EntityID
   {
       get { return this.ID; }
   }

   #endregion   

   #region Constructors

   public Campaign()
   {
      this.CreatedOnDate = DateTime.Now;
      this.Fees = new List<Fee>();
   }

   #endregion
}

//View model
//THIS is the class you want to validate
public class CampaignViewModel
{
   #region Properties

   public int ID { get; set; }

   [StringLength(50)]
   public string Name { get; set; }

   [Required]
   public string CreatedBy { get; set; }

   public DateTime CreatedOnDate { get; set; }

   public Fee AssociatedFee { get; set; } 

   #endregion

   #region Constructors

   public CampaignViewModel() 
   { }

   public CampaignViewModel(Campaign data)
   {
      this.ID = data.ID
      this.Name = data.Name;
      this.CreatedBy = data.CreatedBy;
      this.CreatedOn = data.CreatedOn;
      this.AssociatedFee = data.Fees.Where(x=>x.Active && x.ID == this.ID);
      //Just an example
   }

   #endregion
}

Also, you could use Fluent Validation for a more in-depth separation of concerns. (http://fluentvalidation.codeplex.com/)