C # Entity with id Guid does not allow me to register in database


I have modified the question as a result of your requests: first, to clarify that my database was generated using the wizard that Visual Studio has (2015 is the one I use) with the ADO.NET Entity Model (see attached image), in which I designed my entities and then generated them on the basis of data ( here I ask for an assessment of this via ).

The properties that I put in the design about the entity are these:

After the generation of the DDL, this is the SQL code of the Clock table (named by the EF as ClockSet)

CREATE TABLE [dbo].[RelojSet] (
   [Id] uniqueidentifier  NOT NULL,
   [Direccion] nvarchar(max)  NOT NULL,
   [Puerto] nvarchar(max)  NOT NULL,
   [Descripcion] nvarchar(max)  NOT NULL,
   [Activo] bit  NOT NULL,
   [LastUpdate] datetime  NULL

The context is as follows (I have not touched anything, it is self-generated by EF):

public partial class ZKTDataBaseContainer : DbContext
    public ZKTDataBaseContainer()
        : base("name=ZKTDataBaseContainer")

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
        throw new UnintentionalCodeFirstException();

    public virtual DbSet<Reloj> RelojSet { get; set; }
    public virtual DbSet<Trabajador> TrabajadorSet { get; set; }
    public virtual DbSet<Marcaje> MarcajeSet { get; set; }
    public virtual DbSet<Settings> SettingsSet { get; set; }

The mapping created in the model is the one I referred to earlier:

public partial class Reloj
    public Reloj()
        this.Marcaje = new HashSet<Marcaje>();

    public System.Guid Id { get; set; }
    public string Direccion { get; set; }
    public string Puerto { get; set; }
    public string Descripcion { get; set; }
    public bool Activo { get; set; }
    public Nullable<System.DateTime> LastUpdate { get; set; }

    public virtual ICollection<Marcaje> Marcaje { get; set; }

In my controller, I instantiate a Clock object, and I try to persist it:

public ActionResult Create(FormCollection collection)        
    var reloj = new Reloj
        Activo = collection.Get("Activo").Equals("on"),
        Descripcion = collection.Get("Descripcion"),
        Direccion = collection.Get("Direccion"),
        Puerto = collection.Get("Puerto"),
        LastUpdate = DateTime.Now,
        Id = Guid.newGuid(),   // => No funciona
        // Id = new Guid() => No Funciona
        // Id = Guid.Parse("3F2504E0-4F89-11D3-9A0C-0305E82C3301") => Tampoco
        // Id = Guid.TryParse("3F2504E0-4F89-11D3-9A0C-0305E82C3301") => Menos

        using (var db = new ZKTDataBaseContainer())
    }catch (Exception ex){


The error generated is related to the Guid identifier of the Clock entity:

"You can not insert the NULL value in the 'Id' column, table 'ZKTDB.dbo.Settle.Rule' The column does not support NULL values INSERT error. \ r \ nThe instruction was terminated."

What am I forgetting here?

asked by Rigo 01.03.2018 в 22:32

2 answers


As a recommendation, the GUID can be generated automatically from your database using the instruction NEWID () for example:

CREATE TABLE [dbo].[RelojSet]
    [Activo] BIT NULL,
    [Descripcion] NVARCHAR(100) NOT NULL, 
    [Direccion] NVARCHAR(100) NOT NULL, 

Thus it is no longer necessary to assign the Id property within your model as it is directly generated by the database.

answered by 02.03.2018 / 00:57

Recommendation: Use NEWSEQUENTIALID () instead of NEWID () when it is a key column, for speed issues (issues of indeces and creation page * verification of existence, since it will be ORDERED by creation of the Id, with NEWID () are all alterations) That is, it is a more physical and organizational theme of DB, also continue creating GUID identifiers

Text of docs / help:


When a GUID column is used as a row identifier, the use of NEWSEQUENTIALID may be faster than the use of the NEWID function. The reason is that the NEWID function causes random activity and uses fewer pages of data in cache. The use of NEWSEQUENTIALID also helps to completely fill in the data and index pages

In your example it would be the Id column as follows


Links that can help you

answered by 04.03.2018 в 13:35