如何在不覆盖 asp.net mvc 中的模型 类 的情况下更新(通过合并)edmx

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

我正在 asp.net mvc 中开发应用程序。我使用 entity framework 作为 ORM。我有个问题。要使用 javascript 不显眼的验证,我需要向模型对象添加注释。例如; [必填]、[电子邮件地址]。但是当我们向数据库添加一些东西并更新它时,所有模型 类 都被覆盖,所有注释都消失了。或者,只要您打开 edmx,自动模型 类 就会自动覆盖。我怎么解决这个问题。有几十个屏幕类,edmx稍有改动就会删除所有类中的注释,造成巨大的时间浪费。

// <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; }
}
}

已编辑 元数据

调查部分和元数据

 //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; }
}

获取数据Ajax事件

  // 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);
        }
    }

我应该如何更改我的 GetData event.And 什么列表应该发送到客户端??

最佳做法是,使用 ViewModel[而非 Entity/Model classes] 在客户端进行操作/播放。

所以使用 ViewModel,继承模型 classes 然后使用 Annotations 例如。 Public class 视图模型类:模型类 { [必填("First Name is Required")] Public 字符串名字 {get;放;} }

要做到这一点,您可以使用部分 classes 并在 class 上方的 .net 核心注释中使用 "ModelMetadataType"。 让我们用代码来做: 这是您在 edmx 中创建的模型:

public partial class Student{
     public string FirstName {get; set;}
}

首先你必须在另一个与学生 class 同名的文件中创建一个部分 class 并且注意它的名称 space 应该与上面的相同 class. (classes 必须在 edmx 文件之外)

[ModelMetadataType(typeof(StudentMetaData))]
public partial class Student{

}

最后,您必须像这样创建元数据 class:

public class StudentMetaData{
     [Display(name="First Name")]
     public string FirstName {get; set;}
}

现在您可以更新 edmx 文件而无需更改元数据中的数据注释 classes。

https://docs.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.metadatatypeattribute?view=netframework-4.8

目前根据内存工作,但 EF classes 总是部分的,因此创建相同 class 的另一个部分实现,然后将接口和元数据类型绑定添加到它。

// 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
{
}

在这种方式下,以后只要添加新的属性,你只需要更新你的界面

复制你已经添加了注释的生成的CS文件(那个是你的table名字)到另一个文件夹,然后覆盖新生成的,我这样做并且应该继续,直到有更多 hassle-free 的方法。