Share via

EF Core 6 Model Creation feedback

Cenk 1,051 Reputation points
2022-07-06T12:10:36.89+00:00

Hi,

I am trying to design business models for my Blazor Server order management application. Here are my final conceptual models. I would like to hear your suggestions and tips. By the way, I am not sure about the User model. There might be users and roles for identity.

Order
-------
int OrderId
int OrderNumber - autoincrement from 1
DateTime OrderDate
string DoneBy
string Status
int CustomerId - navigation
OrderDetails - navigation
Customer - navigation

OrderDetail

----------

int OrderDetailId
int ProductNumber - autoincrement from 1
string ProductCode
string ProductName
int Quantity
double UnitPrice
double TotalPrice
string ShippingNumber
string Status
Description
int OrderId - navigation
int VendorId - navigation
Order - navigation
VendorId - navigation

Customer
--------
int CustomerId
string Name
string Address
string Email
string PhoneNumber
string MainResponsibleName
string AssistantResponsibleName

Vendor
-------
int VendorId
string Name
string Address
string Email
string PhoneNumber
string MainResponsibleName
string AssistantResponsibleName

User
-----
int UserId
string UserName
string Email

Should I also need to add relations to the model creation like the sample one below?

protected override void OnModelCreating(ModelBuilder modelBuilder)  
        {  
            //relations  
            modelBuilder.Entity<ProductInventory>()  
                .HasKey(pi => new { pi.ProductId, pi.InventoryId });  
  
            modelBuilder.Entity<ProductInventory>()  
                .HasOne(pi=>pi.Product)  
                .WithMany(p=>p.ProductInventories)  
                .HasForeignKey(pi => pi.ProductId);  
  
            modelBuilder.Entity<ProductInventory>()  
                .HasOne(pi => pi.Inventory)  
                .WithMany(p => p.ProductInventories)  
                .HasForeignKey(pi => pi.InventoryId);  
Developer technologies | .NET | Entity Framework Core

Answer accepted by question author

Jack J Jun 25,306 Reputation points
2022-07-11T09:29:24.1+00:00

@Cenk , Welcome to Microsoft Q&A,

You could try the following code to generate autoincrement fields in ef core 6.

 protected override void OnModelCreating(ModelBuilder modelBuilder)  
        {  
            modelBuilder.Entity<Order>()  
    .Property(e => e.OrderNumber)  
    .ValueGeneratedOnAdd().UseIdentityColumn(1, 1);  
        }  

The above code means that OrderNumber column will start from 1 and increase by 1.

Hope this could help you.

Best Regards,
Jack


If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

Was this answer helpful?

0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Cenk 1,051 Reputation points
    2022-07-07T05:41:15.273+00:00

    OrderDetail in the Order should be List, right @Xinran Shen - MSFT ? Also how to generate autoincrement fields on EF Core 6? Let's say I want to make productNumber autoincrement. Should I add [DatabaseGenerated(DatabaseGeneratedOption.Identity)] data annotation to it? I want productNumber to start from 1.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.