How to update(by merging) edmx without override the model classes in asp.net mvc

asp.net-mvc c# entity-framework entity-framework-6 orm

Question

I am developing an application in asp.net mvc. I use entity framework as ORM. I have a problem. To use javascript unobstrusive validation, I need to add annotation to model objects. For example; [Required], [EMailAddress]. But when we add something to the database and update it, all model classes are override, and all annotations disappear. Or, as soon as you open edmx, automatic model classes are automatically override. How can I solve this problem. There are dozens of screens and classes, the slightest change in edmx erases the annotation in all classes, causing huge waste of time.

// <auto-generated>

using System.ComponentModel.DataAnnotations;

namespace MerinosSurvey.Models
{
using System;
using System.Collections.Generic;

public partial class Surveys
{
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2214:DoNotCallOverridableMethodsInConstructors")]
    public Surveys()
    {
        this.SurveyQuestions = new HashSet<SurveyQuestions>();
        this.SurveyCustomers = new HashSet<SurveyCustomers>();
    }

    public int SurveyId { get; set; }
    [Required(ErrorMessage = "Plase enter survey name.")]
    public string SurveyName { get; set; }
    [Required(ErrorMessage = "Please enter survey description.")]
    public string SurveyDescription { get; set; }

    // [DataType(DataType.Date)]
    public System.DateTime? CreatedDate { get; set; }
    //[DataType(DataType.Date)]

    public System.DateTime? UpdatedDate { get; set; }
    public int CreatedUserId { get; set; }
    public bool IsActive { get; set; }
    public bool Status { get; set; }

    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<SurveyQuestions> SurveyQuestions { get; set; }
    [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
    public virtual ICollection<SurveyCustomers> SurveyCustomers { get; set; }

    public string Token { get; set; }
}
}

Editted for Metadata

Surveys Partial & Metadata

 //PartialClass
 [MetadataType(typeof(SurveyMetadata))]
 public partial class Surveys
{
}
//Metadata
 public partial class SurveyMetadata
{
    public int SurveyId { get; set; }
    [Required(ErrorMessage = "Lütfen anket adını giriniz.")]
    public string SurveyName { get; set; }
    [Required(ErrorMessage = "Lütfen anket açıklamasını giriniz.")]
    public string SurveyDescription { get; set; }

    // [DataType(DataType.Date)]
    public System.DateTime? CreatedDate { get; set; }
    //[DataType(DataType.Date)]

    public System.DateTime? UpdatedDate { get; set; }
    public int CreatedUserId { get; set; }
    public bool IsActive { get; set; }
    public bool Status { get; set; }

    public virtual ICollection<SurveyQuestionMetadata> SurveyQuestionMetadatas { get; set; }
    public virtual ICollection<SurveyCustomerMetadata> SurveyCustomerMetadatas { get; set; }

    public string Token { get; set; }
}

GetData Ajax Event

  // GET: Survey
    public ActionResult GetData()
    {
        using (MerinosSurveyEntities entity = new MerinosSurveyEntities())
        {
            List<Surveys> surveys = entity.Surveys.Where(x => x.IsActive && x.Status)
                .OrderBy(x => x.SurveyId).ToList();
            return Json(new { data = surveys }, JsonRequestBehavior.AllowGet);
        }
    }

How I should change my GetData event.And what list should go to the client side??

1
1
1/22/2020 9:59:32 AM

Accepted Answer

Best Practice is, use ViewModel[Not Entity/Model classes] to manipulate / play at Client Side.

So use ViewModel, Inherit Model classes and then use Annotations For eg. Public class ViewModelClass: ModelClass { [Required("First Name is Required")] Public String FirstName {get; set;} }

0
1/22/2020 6:44:25 AM

Popular Answer

Working from memory at the moment, but the EF classes are always partial so create another partial implementation of the same class, then add the Interface and Metadatatype binding to that.

// Entity Framework Model
public partial class User
{
    public string Email { get; set; }
    public string Password { get; set; }
}

// Your Interface with data annotations
public interface IUser
{
    [Required]
    string Email { get; set; }

    [Required]
    string Password { get; set; }
}

// Partial Model appling the interface to the entity model
[MetadataType(typeof(IUser))]
public partial class User : IUser
{
}

Under this approach, in the future, you only need to worry about updating your interface should you add new properties



Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow