using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata;
public static void Main()
using (var context = new EntityContext())
context.Database.EnsureCreated();
using (var context = new EntityContext())
context.Database.OpenConnection();
var entity = new GeneratedValueWithDataAnnotation() {
TestName = "INSERT with values",
DefaultValue = "INSERT (Throw)",
context.GeneratedValueWithDataAnnotations.Add(entity);
public class EntityContext : DbContext
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
optionsBuilder.UseSqlServer(new SqlConnection(FiddleHelper.GetConnectionStringSqlServer()));
base.OnConfiguring(optionsBuilder);
protected override void OnModelCreating(ModelBuilder modelBuilder)
modelBuilder.Entity<GeneratedValueWithDataAnnotation>().HasKey(x => x.ID);
modelBuilder.Entity<GeneratedValueWithDataAnnotation>().Property(x => x.DefaultValue).HasDefaultValue("Entity Framework Extensions").Metadata.SetBeforeSaveBehavior(PropertySaveBehavior.Throw);
base.OnModelCreating(modelBuilder);
public DbSet<GeneratedValueWithDataAnnotation> GeneratedValueWithDataAnnotations { get; set; }
public class GeneratedValueWithDataAnnotation
public Guid ID { get; set; }
public string TestName { get; set; }
public string DefaultValue { get; set; }