Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.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
Model view controller 如何为列表中的一个元素生成GUID<;T>;在MVC中?_Model View Controller_Guid - Fatal编程技术网

Model view controller 如何为列表中的一个元素生成GUID<;T>;在MVC中?

Model view controller 如何为列表中的一个元素生成GUID<;T>;在MVC中?,model-view-controller,guid,Model View Controller,Guid,我在模型中创建了一个类,它将是视图中显示的数据库表 using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Runtime.InteropServices; namespace showtable.Models { public class Employee { public Guid EmployeeId { get;

我在模型中创建了一个类,它将是视图中显示的数据库表

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Runtime.InteropServices; 

namespace showtable.Models
{
    public class Employee
    {
        public Guid  EmployeeId { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public DateTime hiredate { get; set; }
        public decimal salary { get; set; }
    }
}
现在我想为主键生成一个GUID,它是EmployeeId。 如何在我的控制器中写入它

您可以这样做:

public class Employee
{
    public Guid  EmployeeId { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public DateTime hiredate { get; set; }
    public decimal salary { get; set; }

    public Employee () 
    {
       EmployeeId = Guid.NewGuid();
    }
}

然后在控制器中,当您实例化一个新的
Employee
类时,您将使用
GUID
填充
EmployeeId
属性

它实际上是一个列表。我想给每个记录添加值。你能准确地说吗?什么?如果您的意思是在创建类的新实例时,该属性将具有唯一的
GUID
,则为“是”。