C# 实体类型需要定义主键
我有3个模型类(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
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()
{
}
}