Click here to Skip to main content
15,301,401 members
Home / Discussions / C#
   

C#

 
AnswerRe: Dynamically change int from a string Pin
OriginalGriff29-Dec-21 21:53
mveOriginalGriff29-Dec-21 21:53 
QuestionC# Entity Framework : how to generate custom automatic numbers Pin
Fidele Okito27-Dec-21 4:50
MemberFidele Okito27-Dec-21 4:50 
AnswerRe: C# Entity Framework : how to generate custom automatic numbers Pin
Gerry Schmitz27-Dec-21 5:16
mveGerry Schmitz27-Dec-21 5:16 
GeneralRe: C# Entity Framework : how to generate custom automatic numbers Pin
Fidele Okito27-Dec-21 5:32
MemberFidele Okito27-Dec-21 5:32 
GeneralRe: C# Entity Framework : how to generate custom automatic numbers Pin
Gerry Schmitz27-Dec-21 6:45
mveGerry Schmitz27-Dec-21 6:45 
GeneralRe: C# Entity Framework : how to generate custom automatic numbers Pin
Fidele Okito27-Dec-21 7:55
MemberFidele Okito27-Dec-21 7:55 
AnswerRe: C# Entity Framework : how to generate custom automatic numbers Pin
#realJSOP28-Dec-21 0:24
mva#realJSOP28-Dec-21 0:24 
AnswerRe: C# Entity Framework : how to generate custom automatic numbers Pin
jschell29-Dec-21 7:17
Memberjschell29-Dec-21 7:17 
I am not really in agreement with the other comments since they somehow seem to be drilling down on a 'primary' key even though I see nothing in your request that suggests that.

Your posting should focus on creating an 'invoice number'
It will need to have the following
1. Be unique
2. Be human readable
3. Be small enough that humans will not be annoyed by it.

Presumably the 'prefix_text' is unique by itself. It does not rely on the 'table_name'. If that is not the case then the uniqueness constraint for the invoice number will not be met by the form that you posted.

I would suggest that you do not use underscores. A separator is ok but use a dash since it tends to be more obvious. However you can also consider the dash as being a display character only. So for example credit card numbers often use a dash but the numbers are actually the only part of the actual id. So your UI could take it with dashes but remove it for searches. And in the database you store it without the dashes.

Without the separator the format must be fixed however. So it is a design decision.

Volume of the business and generation of the code, now and for some time into the future, impacts how you might want to do this. If you are producing say 100 of them a day then it is easier than if you are producing 100 million in a day.

There might be a security concern if you generate numbers that are easily guessed. So for example if you use the current date along with an incrementing number then it is easy to guess valid invoice numbers. This also is a matter for how the business works. It isn't much of a concern if there are other authentication factors used to limit access to the invoices.

I don't see anything conceptually wrong with your basic idea although I would format the id (pad zeros to the left.)

Fidele Okito wrote:
using entity framework.


I am guessing that this in fact is your real problem.

What you want to do is just use native SQL. Myself I would probably use a stored proc but you can do it with SQL in the application.

You need the following in a transaction. This is pseudo code but the idea is basically sound.

declare @lastId int;
update table1 set next_id = next_id + 1 where prefix_text='xxx' and table_name='fff';
select @lastId = next_id from table1 where prefix_text='xxx' and table_name='fff';
insert into table2 (invoice_number) values('xxx' + '-' + @lastId + '-' + ...);
select @lastId;


The last part of that returns the newest id to your application. Not needed if that is not something that is needed at that point.

The transaction is necessary to insure uniqueness.
GeneralRe: C# Entity Framework : how to generate custom automatic numbers Pin
Gerry Schmitz29-Dec-21 15:22
mveGerry Schmitz29-Dec-21 15:22 
JokeRe: C# Entity Framework : how to generate custom automatic numbers Pin
Peter_in_278029-Dec-21 15:32
professionalPeter_in_278029-Dec-21 15:32 
GeneralRe: C# Entity Framework : how to generate custom automatic numbers Pin
Gerry Schmitz29-Dec-21 15:54
mveGerry Schmitz29-Dec-21 15:54 
GeneralRe: C# Entity Framework : how to generate custom automatic numbers Pin
jschell30-Dec-21 7:31
Memberjschell30-Dec-21 7:31 
GeneralRe: C# Entity Framework : how to generate custom automatic numbers Pin
Gerry Schmitz30-Dec-21 9:23
mveGerry Schmitz30-Dec-21 9:23 
GeneralRe: C# Entity Framework : how to generate custom automatic numbers Pin
jschell23-Jan-22 7:01
Memberjschell23-Jan-22 7:01 
GeneralRe: C# Entity Framework : how to generate custom automatic numbers Pin
Fidele Okito30-Dec-21 3:14
MemberFidele Okito30-Dec-21 3:14 
GeneralRe: C# Entity Framework : how to generate custom automatic numbers Pin
jschell30-Dec-21 7:26
Memberjschell30-Dec-21 7:26 
GeneralRe: C# Entity Framework : how to generate custom automatic numbers Pin
Fidele Okito30-Dec-21 8:57
MemberFidele Okito30-Dec-21 8:57 
QuestionFull path Crystal report on client server in c# Pin
remiki26-Dec-21 22:54
Memberremiki26-Dec-21 22:54 
AnswerRe: Full path Crystal report on client server in c# Pin
OriginalGriff26-Dec-21 23:22
mveOriginalGriff26-Dec-21 23:22 
AnswerRe: Full path Crystal report on client server in c# Pin
remiki26-Dec-21 23:30
Memberremiki26-Dec-21 23:30 
GeneralRe: Full path Crystal report on client server in c# Pin
jschell29-Dec-21 7:25
Memberjschell29-Dec-21 7:25 
QuestionWPF Filtering DataGrid causes UI Freeze Pin
Member 1480965126-Dec-21 4:25
MemberMember 1480965126-Dec-21 4:25 
AnswerRe: WPF Filtering DataGrid causes UI Freeze Pin
Gerry Schmitz26-Dec-21 6:54
mveGerry Schmitz26-Dec-21 6:54 
AnswerRe: WPF Filtering DataGrid causes UI Freeze Pin
Mycroft Holmes26-Dec-21 11:41
professionalMycroft Holmes26-Dec-21 11:41 
QuestionWMI Connection to Remote PC with ManegementScope too slow(8-10 seconds). The WMI Query itself is OK (under 1 second) Pin
Mustafa Levrek26-Dec-21 1:47
MemberMustafa Levrek26-Dec-21 1:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.