I am new to Entity Framework Core. In my shopping cart models design, I have a little confusion in designing my model classes using Entity Framework Core.
I have assign collection of address to user model as multiple delivery address are possible for one user.
In my order model, multiple order can be possible for one user so I am declaring userID as foreign key in the order table along with cartID (as order belongs to a cart).
While processing the order, in my view, my requirement is that user should be able to select his convenient address from dropdown.
These are my model classes in the project:
User model:
public class User
{
public int Id { get; set; }
public string userName { get; set; }
public string phone { get; set; }
public string email { get; set; }
public string password { get; set; }
public Cart cart { get; set; }
public ICollection<Address> Addresses { get; set; }
}
Address model:
public class Address
{
public int Id { get; set; }
public string AddressLine1 { get; set; }
public string AddressLine2 { get; set; }
public string County { get; set;}
public string Eircode { get; set; }
public int UserId { get; set; }
public User user { get; set; }
}
Order model:
public class Order { public int Id { get; set; } public int CartID { get; set; } public Cart Cart { get; set; }
public bool IsDelivered { get; set; }
public int UserId { get; set;}
public User User { get; set; }
}
Cart model:
public class Cart
{
public int Id { get; set; }
public int UserId { get; set; }
public User user { get; set;}
public ICollection<CartProduct> CartProducts { get; set;}
}
Is the design of my model classes correct?
Should I include ICollection<Order> in the User model as one user can have multiple orders?
Should I declare UserAddress as a separate model class for mapping of user and their addresses?
My Cart model is mapped with user (using userid) as each user has its own cart, so userID is repeating in both cart and order model (is there any way to remove this dependency) or is it correct?