0

I have boolean functions like this:

<h3 class="text-info">@(objRoom.Id != 0 ? "Update" : "Create") Room</h3>

How do I change it to make it work when the primary key is a Guid instead of an Int?

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nicolas Lewentorp
  • 99
  • 1
  • 1
  • 10
  • @PanagiotisKanavos - but the context is very much Blazor, that tag was not entirely wrong. – H H Jan 29 '20 at 16:57
  • @HenkHolterman duh, that's what I get for reading the question instead of the code – Panagiotis Kanavos Jan 29 '20 at 17:00
  • A bit of warning - GUIDs are *terrible* primary keys. Primary keys are usually the clustered index of a table - this defines the order in which the table rows are stored. A random value like a GUID ends up writing rows all over the table, causing fragmentation and forcing the server to jump around index and data pages to find the correct row or rows. At the very least, ensure you use a *sequential* algorithm to generate the GUIDs, preferably in the database itself. – Panagiotis Kanavos Jan 29 '20 at 17:06
  • In SQL Server, [NEWSEQUENTIALID](https://learn.microsoft.com/en-us/sql/t-sql/functions/newsequentialid-transact-sql?view=sql-server-ver15) generates unique sequential GUIDs. Windows also has a Win32 API method that generates sequential GUIDs – Panagiotis Kanavos Jan 29 '20 at 17:06
  • Thanks for the warning and the tip about Newsequentialid, will def check it out. – Nicolas Lewentorp Jan 29 '20 at 19:26

1 Answers1

1

You need to Follow as:

  1. Change DataType of DB Column With UniqueId from Int/BigInt

  2. Create/Change Property Data Type in Model class

    Public Guid Id { get; set;} or if it is nullable then Public Guid? Id { get; set;}

Then check if it is null or not while using Id == Guid.Empty

// Pseudo code

if(Id == Guid.Empty ? "Create" : "Update"){
}

Mark it Solve if answers your question

Community
  • 1
  • 1
Shyam
  • 182
  • 1
  • 14