using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Z.EntityFramework.Plus;
public static void Main()
using (var context = new EntityContext())
context.Database.EnsureCreated();
var guidID = new Guid("00000000-0000-0000-0000-000000000013");
using (var context = new EntityContext())
context.Database.OpenConnection();
context.GeneratedValueWithDataAnnotations.SqlServerSetIdentityInsertOn();
var entity = new GeneratedValueWithDataAnnotation() {
TestName = "INSERT with values",
Computed = "INSERT (Ignore)",
Concurrency = "INSERT (Save)",
Timestamp = "INSERT (Ignore)",
context.GeneratedValueWithDataAnnotations.Add(entity);
FiddleHelper.WriteTable("1 - INSERT with values", context.GeneratedValueWithDataAnnotations.AsNoTracking().ToList());
using (var context = new EntityContext())
var entity = context.GeneratedValueWithDataAnnotations.First();
entity.TestName = "UPDATE with values";
entity.Computed = "UPDATE (Ignore)";
entity.Concurrency = "UPDATE (Save)";
entity.Timestamp = "UPDATE (Ignore)";
FiddleHelper.WriteTable("2 - UPDATE with values", context.GeneratedValueWithDataAnnotations.AsNoTracking().ToList());
public class EntityContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));
base.OnConfiguring(optionsBuilder);
public DbSet<GeneratedValueWithDataAnnotation> GeneratedValueWithDataAnnotations { get; set; }
public class GeneratedValueWithDataAnnotation
public string TestName { get; set; }
public Guid ID { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Identity { get; set; }
[DatabaseGenerated(DatabaseGeneratedOption.Computed)]
public string Computed { get; set; }
public string Concurrency { get; set; }
public string Timestamp { get; set; }