jackncoke
jackncoke

Reputation: 2020

Coverting Guid to string

I am creating a application and I wanted to try and use GUIDs. I am using Entity Framework code first and LINQ so I thought the easiest way would be to use

string guid = Guid.NewGuid().ToString();
var acc = new v_Account();   
acc.v_AcctGuid = guid;

And then store my GUIDs as type string in my database. Is this a bad practice? Is a GUID really just a globally unique randomly generated string potentially?

I am pretty sure it is going to work but I just don't want to face problems down the road or create a vulnerability by doing this.

Upvotes: 0

Views: 2008

Answers (2)

dutzu
dutzu

Reputation: 3920

If the db you are targeting does not support GUIDs (like SQLite for instance) then it's ok to store as string and then expose them to the Business Layer as GUID making the necessary conversions on the getter and setter of a property of type GUID that masks the string property of the entity.

Upvotes: 1

Davin Tryon
Davin Tryon

Reputation: 67336

A GUID maps directly to a MSSQL database as a uniqueidentifier type. So there is no need to stringify it.

Just use it directly:

acc.v_AcctGuid = Guid.NewGuid();

Assuming:

public class Account
{
    public Guid v_AcctGuid { get; set; }
}

Upvotes: 6

Related Questions