0

I have two model classes "customerdetails" and "addresssdetails".my question is "one customer can save with multiple addresss?

namespace customer2.Models
{
    public class customerdetails
    {
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        public int id { set; get; }
        [Key]
        public int customerid { set; get; }
        public string customername { set; get; }
    }
    public class addressdetails
    {
        public int addressno { set; get; }
        public string street { set; get; }
        public string landmark { set; get; }          
        public int pincode { set; get; }           
    }
    public class MkContext : DbContext
    {
        public virtual DbSet<customerdetails> customers { get; set; }
        public virtual DbSet<addressdetails> address { get; set; }    
    }

    public class customerviewmodel
    {
        public customerdetails cd { set; get; }
        public List<addressdetails> ad { set; get; }           

    }   
}![enter image description here](https://i.stack.imgur.com/4QCA1.jpg)
manikumar
  • 7
  • 2

1 Answers1

0

Before I answer, just a quick tip: Make sure to properly case your class names and properties to improve readability. Here is a link with some guidelines

If you want to store a Customer with multiple AddressDetails, you will need to define a class and add it to the context.

public class Customer {
    public int Id { get; set; }
    public CustomerDetails CustomerDetails { get; set; }
    public IList<AddressDetails> AddressDetails { get; set; }
}

Then add this to the context.

sander
  • 719
  • 2
  • 9
  • 21
  • can send me, how to bind the data from view to model (especially to IList property defined in Customer Class) – manikumar Sep 10 '19 at 06:30
  • https://stackoverflow.com/questions/19964553/mvc-form-not-able-to-post-list-of-objects – sander Sep 10 '19 at 06:38