简体   繁体   中英

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:

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.

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
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM