Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/268.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# 实体类型需要定义主键_C#_Asp.net_Asp.net Mvc_Entity Framework_Asp.net Core - Fatal编程技术网

C# 实体类型需要定义主键

C# 实体类型需要定义主键,c#,asp.net,asp.net-mvc,entity-framework,asp.net-core,C#,Asp.net,Asp.net Mvc,Entity Framework,Asp.net Core,我有3个模型类(Customer、Manager、technology),它们继承自基类Person。Id键在Person基类中定义 当我尝试为Customer类生成控制器时,会出现一个错误,指示实体类型Customer必须具有主键 这是我的个人课程: public class Person { [Key] public int Id { get; } [EmailAddress] public string? Email { get; set; } pu

我有3个模型类(
Customer、Manager、technology
),它们继承自基类
Person
Id
键在
Person
基类中定义

当我尝试为
Customer
类生成控制器时,会出现一个错误,指示实体类型Customer必须具有主键

这是我的
个人
课程:

public class Person
{
    [Key]
    public int Id { get; }
    [EmailAddress]
    public string? Email { get; set; }
    public int? Cin { get; set; }
    public string? Address { get; set; }
    [Required]
    public string Name { get; set; }
    [Phone, Required]
    public int PhoneNumber { get; set; }

    public Person(int PhoneNumber, string Name, string Email = null, int? Cin = null, string Address = null)
    {
        this.PhoneNumber = PhoneNumber;
        this.Name = Name;
        this.Email = Email;
        this.Address = Address;
        this.Cin = Cin;
    }

    public Person()
    {
    }
}
public class Customer : Person
{
    public List<Device> CustomerDevices { get; set; }

    public Customer(int PhoneNumber, string Name, string Email = null, int? Cin = null, string Address = null)
        : base(PhoneNumber, Name, Email, Cin, Address)
    {
    }

    public Customer() : base()
    {
    }
}
这是我的
客户
课程:

public class Person
{
    [Key]
    public int Id { get; }
    [EmailAddress]
    public string? Email { get; set; }
    public int? Cin { get; set; }
    public string? Address { get; set; }
    [Required]
    public string Name { get; set; }
    [Phone, Required]
    public int PhoneNumber { get; set; }

    public Person(int PhoneNumber, string Name, string Email = null, int? Cin = null, string Address = null)
    {
        this.PhoneNumber = PhoneNumber;
        this.Name = Name;
        this.Email = Email;
        this.Address = Address;
        this.Cin = Cin;
    }

    public Person()
    {
    }
}
public class Customer : Person
{
    public List<Device> CustomerDevices { get; set; }

    public Customer(int PhoneNumber, string Name, string Email = null, int? Cin = null, string Address = null)
        : base(PhoneNumber, Name, Email, Cin, Address)
    {
    }

    public Customer() : base()
    {
    }
}
公共类客户:个人
{
公共列表CustomerDevices{get;set;}
公共客户(int电话号码、字符串名称、字符串电子邮件=null、int?Cin=null、字符串地址=null)
:base(电话号码、姓名、电子邮件、Cin、地址)
{
}
公共客户():基本客户()
{
}
}

代码示例中的问题是您应该向
Id
属性添加set,以便实体框架可以设置自动生成的Id。

代码示例中的问题是您应该向
Id
属性添加set,因此实体框架可以设置自动生成的id。

我认为您的
id
属性需要一个setter

public int Id { get; }              // not work
public int Id { get; set; }         // work
public int Id { get; private set; } // also work
您可以更改类别
Person

public class Person
{
    [Key]
    public int Id { get; private set; }
    [EmailAddress]
    public string? Email { get; set; }
    public int? Cin { get; set; }
    public string? Address { get; set; }
    [Required]
    public string Name { get; set; }
    [Phone, Required]
    public int PhoneNumber { get; set; }

    public Person(int PhoneNumber, string Name, string Email = null, int? Cin = null, string Address = null)
    {
        this.PhoneNumber = PhoneNumber;
        this.Name = Name;
        this.Email = Email;
        this.Address = Address;
        this.Cin = Cin;

    }
    public Person()
    {

    }
}

我认为您的
id
属性需要一个setter

public int Id { get; }              // not work
public int Id { get; set; }         // work
public int Id { get; private set; } // also work
您可以更改类别
Person

public class Person
{
    [Key]
    public int Id { get; private set; }
    [EmailAddress]
    public string? Email { get; set; }
    public int? Cin { get; set; }
    public string? Address { get; set; }
    [Required]
    public string Name { get; set; }
    [Phone, Required]
    public int PhoneNumber { get; set; }

    public Person(int PhoneNumber, string Name, string Email = null, int? Cin = null, string Address = null)
    {
        this.PhoneNumber = PhoneNumber;
        this.Name = Name;
        this.Email = Email;
        this.Address = Address;
        this.Cin = Cin;

    }
    public Person()
    {

    }
}