简体   繁体   English

使用带有字符串主键和一对多关系的数据库优先方法在 ASP.NET 核心中不起作用 Web API

[英]Using database-first approach with string primary key and a one-to-many relationship not working in ASP.NET Core Web API

I have two model classes:我有两个 model 类:

public partial class customer
{
    public string customer_id { get; set; }   // Primary Key
    public string customer_name { get; set; }
    public string course_mobile { get; set; }

    public  List<customer_address> customer_addresses { get; set; }
}

public partial class customer_address
{
    public string address_id { get; set; }    // Primary Key
    public string adreess { get; set; }
  
    public  customer customer { get; set; }
    public string customer_id { get; set; }   // Foreign Key
}

I get an error:我得到一个错误:

The entity type 'customer' requires a primary key to be defined.实体类型“客户”需要定义主键。

So how it solve?那怎么解决呢?

Datbase table structure数据库表结构

You need to add [Key] attrubute.您需要添加[Key]属性。

public partial class customer
{
    [Key]
    public string customer_id { get; set; }   // Primary Key
    public string customer_name { get; set; }
    public string course_mobile { get; set; }

    public  List<customer_address> customer_addresses { get; set; }
}

public partial class customer_address
    {
        [Key]
        public string address_id { get; set; } // Primary Key
        public string adreess { get; set; }


        public customer customer { get; set; }
        
        public string customer_id { get; set; }  // Foriegn Key
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 asp.Net Core 一对多关系 UPDATE 语句与 FOREIGN KEY 约束冲突 - asp.Net Core one-to-many relationship UPDATE statement conflicted with the FOREIGN KEY constraint ASP.NET Core中如何将一对多的关系数据添加到数据库中? - How to add one-to-many relationship data to the database in ASP.NET Core? 使用asp.net core和mongodb时如何从一对多关系表中获取数据 - How to get data from one-to-many relationship table while working with asp.net core and mongodb 如何在 ASP.NET Core 6 Web API 中使用一对一关系将外键添加到表中? - How to add a foreign key into a table using one-to-one relationship in ASP.NET Core 6 Web API? 通过asp.net core POST创建EF Core一对多关系数据 - Create EF Core One-To-Many Relationship data through asp.net core POST ASP.NET Core 3.1 和 Entity Framework Core:一对多的关系 - ASP.NET Core 3.1 and Entity Framework Core : one-to-many relationship .Net Core 一对多关系导航不起作用 - .Net Core navigation on one-to-many relationship not working 将具有一对多关系的 SQL 查询转换为具有父/子 object 结构的 ASP.NET 核心/实体框架核心调用 - Convert SQL query with a one-to-many relationship to an ASP.NET Core / Entity Framework Core call with a parent/children object structure 如何在 ASP.NET CORE WEB API 中实现多对多关系 - How to implement many to many relationship in ASP.NET CORE WEB API ASP.Net Core:如何更新(更改/添加/删除)嵌套项对象(一对多关系)? - ASP.Net Core: How do I update (change/add/remove) nested item objects (One-to-Many relationship)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM