In my simple class I'm using data annotations to map properties to columns as such:
[Table("Member")]
public class Member
{
[Key]
public Guid MemberId { get; set; }
// More properties go here
}
I can already write new members to my table but MemberId always defaults to a new Guid (0000-000-00...) instead of a beautiful server generated Guid.
I've seen scenarios where people change some setting called StoreGeneratedPattern in an EDMX file. But since I'm using Code First obviously I don't have this EDMX file...
So how would I go about solving this?
Any help is very much appreciated.
Ok, just found the answer myself. You can use an annotation.
[Table("Member")]
public class Member
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid MemberId { get; set; }
// More properties go here
}
Use an annotation:
[Table("Member")]
public class Member
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid MemberId { get; set; }
// More properties go here
}
I have been initializing my Guids in the class constructor since you don't need / can't use the auto-increment functionality that you use with int type keys.
So, I would change to...
[Table("Member")]
public class Member
{
[Key]
public Guid MemberId (get; set;}
public Member
{
MemberId = Guid.NewGuid();
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With