I am migrating from .NET EF to EF CORE. I have a working solution that automatically generates a unique GUID aside from its primary key. Below is the solution.
[Key, Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
[Index(IsUnique = true)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public Guid TenantId { get; set; }
However, when i tried to migrate to EF Core, i am getting an error message when inserting the data: >SqlException: Cannot insert the value NULL into column 'TenantId', table 'DBContext.dbo.Tenants'; column does not allow nulls. INSERT fails. The statement has been terminated.
Here's my new code:
[Key, Column(Order = 0)]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int Id { get; set; }
public Guid TenantId { get; set; }
Fluent:
base.OnModelCreating(builder);
builder.Entity<Tenant>()
.HasIndex(t => t.TenantId)
.IsUnique();
builder.Entity<Tenant>()
.Property(t => t.TenantId)
.ValueGeneratedOnAdd();
This is inserted by using:
_context.Tenants.Add(tenant);
await _context.SaveChangesAsync();
Is this something that is not supported by EF Core and requires a workaround?