Reputation: 95
I am currently working on an Invoice App project, and, what I don't really know how to implement it's the following example :
As you know, an invoice must have a unique serial number (or a combination of a Series + Number).
I'm working on a Web API, that supports mostly a CRUD operation. Now, for a POST Method, where should I implement a logic, so that, when a client inserts an Invoice, the number of the invoice gets automatically generated by my app.
My Web API project uses an EF DbContext to get/write data to the DB. Should I implement a Repository, and that repository should take care of Inserting/Reading data?
Upvotes: 0
Views: 1360
Reputation: 726
I'll take a stab at answering this as well; it seems you have two concerns here, which might be the same concern, said differently, I may be misunderstanding your question.
Here are some potential options...
MX-
.POST
request, and call _repository.AddInvoice(invoice);
.Upvotes: 0
Reputation: 31
it would help more if you can supply more information such as how your current model looks like. with current information I see following approach that can help you
assuming this is your invoice model
public class invoice
{
public int Id { get; set; }
DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public string InvoiceNumber { get; set; }
//other properties
}
then in your model builder do as following
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<invoice>()
.Property(b => b.Created)
.HasComputedColumnSql("[somepropertyoftable] + ', ' + getdate()");
}
link below can be helpful:
https://learn.microsoft.com/en-us/ef/core/modeling/generated-properties?tabs=data-annotations
Upvotes: 2