Solved

MVC - Automatically Generate GUID for Id field

Posted on 2014-03-22
4
1,642 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
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…
Suggested Courses

710 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