Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何向实体框架生成的.cs文件添加属性_C#_Asp.net Mvc_Entity Framework - Fatal编程技术网

C# 如何向实体框架生成的.cs文件添加属性

C# 如何向实体框架生成的.cs文件添加属性,c#,asp.net-mvc,entity-framework,C#,Asp.net Mvc,Entity Framework,我正在创建一个ASP.net MVC web应用程序,并尝试将验证属性添加到使用实体框架从SQL server生成的ClientsTbl.cs文件中。我已经完成了搭建,并为表(模型)创建了视图和控制器,但出于某种原因,搭建没有将主键列识别为主键。除此之外,它不允许我在[]中添加属性。我甚至无法更改“视图表单”文本框上方显示的字段说明。在接下来的课程中,我该如何做上述工作 namespace Testit.Models { using System; using System.Collection

我正在创建一个ASP.net MVC web应用程序,并尝试将验证属性添加到使用实体框架从SQL server生成的ClientsTbl.cs文件中。我已经完成了搭建,并为表(模型)创建了视图和控制器,但出于某种原因,搭建没有将主键列识别为主键。除此之外,它不允许我在[]中添加属性。我甚至无法更改“视图表单”文本框上方显示的字段说明。在接下来的课程中,我该如何做上述工作

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

public partial class ClientsTbl
{
    public ClientsTbl()
    {
        this.ProgramClientTbls = new HashSet<ProgramClientTbl>();
    }

    public int id { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int CenterId { get; set; }
    public string MiddleName1 { get; set; }
    public string MiddleName2 { get; set; }
    public System.DateTime DateAdded { get; set; }

    public virtual CenterTbl CenterTbl { get; set; }
    public virtual ICollection<ProgramClientTbl> ProgramClientTbls { get; set; }
}
}
namespace Testit.Models
{
使用制度;
使用System.Collections.Generic;
公共部分类ClientsTbl
{
公共客户端stbl()
{
this.ProgramClientTbls=new HashSet();
}
公共int id{get;set;}
公共字符串名{get;set;}
公共字符串LastName{get;set;}
public int CenterId{get;set;}
公共字符串MiddleName1{get;set;}
公共字符串MiddleName2{get;set;}
public System.DateTime DateAdded{get;set;}
公共虚拟中心tbl CenterTbl{get;set;}
公共虚拟ICollection程序ClientTbls{get;set;}
}
}

您可以创建一个元数据类来实现这一点。例如,如果Id、FirstName和LastName是必填字段,则可以创建如下所示的新类

public class ClientsTblMetadata
{
    [Required()]
    public int Id { get; set; }

    [Required()]
    public string FirstName { get; set; }

    [Required()]
    public string LastName { get; set; }
}
然后,您需要添加一个新的分部类,该分部类具有
MetadataType
属性。请确保此类位于同一命名空间下,否则它将无法工作

namespace Testit.Models
{
    [MetadataType(typeof(ClientsTblMetadata))] // you need this line of code.
    public partial class ClientsTbl

为什么它不让你呢?!