• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1936
  • Last Modified:

MVC - Automatically Generate GUID for Id field

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
RBS
Asked:
RBS
  • 2
1 Solution
 
WalkaboutTiggerCommented:
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
 
RBSAuthor Commented:
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
 
Fernando SotoRetiredCommented:
How are you connecting to the database? Entity Framework or Linq to SQL or other?
0
 
RBSAuthor Commented:
Thanks - Guid.newguid() was the answer - sorry I didn't see this earlier.

RBS
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now