Reputation: 13
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?
Upvotes: 1
Views: 1332
Reputation: 2565
I have some knowledge about E-Commerce domain.
I wouldn't let Order have a foreign key to Cart, I would copy all the attributes that they have in common. This because a Cart shouldn't have a Total, it can change dynamically if the product price are changed, an Order instead must have the Total property, which cannot change after the order is purchased. Normally you want to have just one cart for user, so after that the order is purchased you can delete the cart. It is a common practice to let cartId be the userId or something depending on it (for instance "defaultshopname") so that you can get a cart by key when knowing only the user id.
Let Order have a foreign key to the address. A user can change an address, but the address related to an order is the one used at the moment of the purchase
I would include ICollection to User without removing "User property" from Order. You can have both navigations, at database level nothing will change.
The UserAddress entity is not needed. Entity Framework manages navigations, you don't have to manually declare the "tables" for many-to-many relationships.
I would leave User in both Cart and Order because, as I said, I would treat them as different entities.
Upvotes: 1