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
);
GO
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
};
try{
using (var db = new ZKTDataBaseContainer())
{
db.RelojSet.Add(reloj);
db.SaveChanges();
}
}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?