Solved

MVC - Automatically Generate GUID for Id field

Posted on 2014-03-22
4
1,602 Views
Last Modified: 2014-03-31
Hi:

A have an MVC application in which I have the following class:

 public class Actor
{
	public Actor(Guid id, string name)
	{
		Id = id;
		Name = name;
	}

	public Guid Id { get; set; }
	public string Name { get; set; }
}

Open in new window


I would like to make it so that:

1.

when I create an actor, the guid is generated automatically - as in an identity field

2.

I can seed the database with 3 names -
Any help greatly  appreciated

RBS
0
Comment
Question by:RBS
  • 2
4 Comments
 
LVL 15

Accepted Solution

by:
WalkaboutTigger earned 500 total points
ID: 39948332
Have you tried guid.newguid()?

See
stackoverflow.com/questions/16195863/c-sharp-generate-guid-that-is-uniquedoes-not-exist-in-a-table-in-the-database
For an example
0
 

Author Comment

by:RBS
ID: 39948587
Thank you but this does not deal with either of my questions - namely how to generate a guid automatically and how to seed a database with three records that use a guid as an id.
0
 
LVL 63

Expert Comment

by:Fernando Soto
ID: 39948669
How are you connecting to the database? Entity Framework or Linq to SQL or other?
0
 

Author Closing Comment

by:RBS
ID: 39967689
Thanks - Guid.newguid() was the answer - sorry I didn't see this earlier.

RBS
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question