EF Core 6 Identity value generation problem

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
362
Programming Experience
10+
Hello,


I am using EF Core 6 and SQL server express and trying to migrate my entities. I am getting this error.

The properties 'Order.Id', 'Order.OrderNumber' are configured to use 'Identity' value generation and are mapped to the same table 'Orders', but only one column per table can be configured as 'Identity'. Call 'ValueGeneratedNever' in 'OnModelCreating' for properties that should not use 'Identity'


Here are my entities
Entities:
     public class Order
         {
             public int Id { get; set; }
             [Required]
             [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
             public int OrderNumber { get; set; }
             [Required]
             public DateTime OrderDateTime { get; set; }
             [Required]
             [MaxLength(250)]
             public string CustomerName { get; set; }
             [Required]
             [MaxLength(250)]
             public string VendorName { get; set; }
             public string Status { get; set; }
             [MaxLength(50)]
             public string DoneBy { get; set; }
             public List<OrderDetail> OrderDetails { get; set; }
        
         }
      public class OrderDetail
         {
             public int Id { get; set; }
             [Required]
             [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
             public int ProductNumber { get; set; }
             [Required]
             [MaxLength(100)]
             public string ProductCode { get; set; }
             [Required]
             [MaxLength(250)]
             public string ProductName { get; set; }
             [Required]
             public int BuyQuantity { get; set; }
             [Required]
             public int SellQuantity { get; set; }
             public double CostRatio { get; set; }
             public double UnitCost { get; set; }
             public double TotalBuyPrice { get; set; }
             public double TotalSellPrice { get; set; }
             [MaxLength(150)]
             public string ShippingNumber { get; set; }
             public string Status { get; set; }
             [MaxLength(150)]
             public string TrackingNumber { get; set; }
             [MaxLength(400)]
             public string Description { get; set; }
             public int OrderId { get; set; }
             public Order Order { get; set; }
         }

And Dbcontext
DbContext:
...
     public DbSet<Order> Orders { get; set; }
             public DbSet<OrderDetail> OrdersDetail { get; set; }
        
             protected override void OnModelCreating(ModelBuilder modelBuilder)
             {
                 //relations
                 modelBuilder.Entity<Order>()
                     .Property(p => p.OrderNumber)
                     .ValueGeneratedOnAdd();
        
                 modelBuilder.Entity<OrderDetail>()
                     .Property(p => p.ProductNumber)
                     .ValueGeneratedOnAdd();
 
Solution
The error message and how to remedy the problem seems clear enough. What problem are you encountering after you follow the directions?

As an aside, my understanding of Entity Framework, is that by convention, a property name Id will be made the identity column. There's ways to override this convention as I vaguely recall. Anyway, in my mind, if you want the OrderNumber to be the identity column, then don't even bother declaring Id. But since I'm anti-EF, I don't know the nitty gritty details. You'll have to refer to the documentation on what the convention is and how to override it.
The error message and how to remedy the problem seems clear enough. What problem are you encountering after you follow the directions?

As an aside, my understanding of Entity Framework, is that by convention, a property name Id will be made the identity column. There's ways to override this convention as I vaguely recall. Anyway, in my mind, if you want the OrderNumber to be the identity column, then don't even bother declaring Id. But since I'm anti-EF, I don't know the nitty gritty details. You'll have to refer to the documentation on what the convention is and how to override it.
 
Solution
What are the purposes of Id and OrderNumber? If you're specifying that OrderNumber should be an identity then where are you expecting the Id value to come from? As suggested, you probably don't need both. If you do need both then, as the error message is telling you, they can't both be an identity. What would be the point of two identities in one table?
 

Latest posts

Back
Top Bottom