How to set NewId() for GUID in entity framework

后端 未结 7 1221
孤街浪徒
孤街浪徒 2020-12-06 01:43

I am creating asp.net mvc4 sample.In this i created Id column as GUID in Sample table of datacontext.

public class Sample
{
    [Required]
    public Guid          


        
相关标签:
7条回答
  • 2020-12-06 02:26

    If we ignore the politics around if this is a good idea or not, then the answer by @TombMedia is most likely what you are looking for.

    If however, you need to add a new column to an existing table and want to specify newId() to be used for the default value because the field is not nullable then use this in your migration class:

    AddColumn(
        "dbo.Samples",
        "UUID", 
        c => c.Guid(nullable: false, defaultValueSql: "newId()")
    );
    

    Note: this is an old question that is still relevant in EF6 and ranks high when looking for assistance on how to use newId inside EF migrations which is why this answer was added.

    0 讨论(0)
提交回复
热议问题