title: EF Core - Creating Unexpected Incomplete Rows in Sql Server LocalDb
body: <p>I have a new VS2022 solution with EFCore 6.0.1 available at: <a href="https://github.com/RobBowman/BillByTime" rel="nofollow noreferrer">https://github.com/RobBowman/BillByTime</a></p>
  <p>I've created a db context to represent the following:</p>
  <p><a href="https://i.stack.imgur.com/bPI8g.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/bPI8g.png" alt="db_diagram" /></a></p>
  <p>I've used EFCore Power Tools from with VS2022 and compared the diagram it creates against my intended schema - they match!</p>
  <p>Here is the db context C#:</p>
  <pre><code>public class BillByTimeContext : DbContext
      {
          public BillByTimeContext(DbContextOptions&lt;BillByTimeContext&gt; options) : base(options)
          {
              RelationalDatabaseCreator databaseCreator =
                          (RelationalDatabaseCreator)this.Database.GetService&lt;IDatabaseCreator&gt;();
              databaseCreator.EnsureCreated();
          }
  
          public DbSet&lt;ClientOrg&gt;? ClientOrg { get; set; }
          public DbSet&lt;Contract&gt;? Contract { get; set; }
          public DbSet&lt;PurchaseOrder&gt;? PurchaseOrder { get; set; }
          public DbSet&lt;Tenant&gt; Tenant { get; set; }
          public DbSet&lt;TenantManager&gt;? TenantManager { get; set; }
          public DbSet&lt;Timesheet&gt;? Timesheet { get; set; }
          public DbSet&lt;TimesheetHistory&gt;? TimesheetHistory { get; set; }
          public DbSet&lt;Worker&gt;? Worker { get; set; }
  
          protected override void OnModelCreating(ModelBuilder modelBuilder)
          {
              modelBuilder.Entity&lt;Tenant&gt;()
                  .HasIndex(x =&gt; x.Name)
                  .IsUnique();
  
              modelBuilder.Entity&lt;TenantManager&gt;()
                  .HasIndex(x =&gt; x.Email)
                  .IsUnique();
  
              modelBuilder.Entity&lt;Worker&gt;()
                  .HasMany(p =&gt; p.Contracts)
                  .WithOne(t =&gt; t.Worker)
                  .OnDelete(DeleteBehavior.NoAction);
  
              modelBuilder.Entity&lt;Timesheet&gt;()
                  .HasMany(p =&gt; p.TimesheetHistories)
                  .WithOne(t =&gt; t.Timesheet)
                  .OnDelete(DeleteBehavior.NoAction);
  
              modelBuilder.Entity&lt;Worker&gt;()
                  .HasMany(p =&gt; p.TimesheetHistories)
                  .WithOne(t =&gt; t.Worker)
                  .OnDelete(DeleteBehavior.NoAction);
  
              modelBuilder.Entity&lt;Contract&gt;()
                  .Property(x =&gt; x.UnitCharge)
                  .HasPrecision(10, 2);
  
              modelBuilder.Entity&lt;PurchaseOrder&gt;()
                  .Property(x =&gt; x.Amount)
                  .HasPrecision(10, 2);
  
              modelBuilder.Entity&lt;Timesheet&gt;()
                  .Property(x =&gt; x.Monday)
                  .HasPrecision(10, 2);
              modelBuilder.Entity&lt;Timesheet&gt;()
                  .Property(x =&gt; x.Tuesday)
                  .HasPrecision(10, 2);
              modelBuilder.Entity&lt;Timesheet&gt;()
                  .Property(x =&gt; x.Wednesday)
                  .HasPrecision(10, 2);
              modelBuilder.Entity&lt;Timesheet&gt;()
                  .Property(x =&gt; x.Thursday)
                  .HasPrecision(10, 2);
              modelBuilder.Entity&lt;Timesheet&gt;()
                  .Property(x =&gt; x.Friday)
                  .HasPrecision(10, 2);
              modelBuilder.Entity&lt;Timesheet&gt;()
                  .Property(x =&gt; x.Saturday)
                  .HasPrecision(10, 2);
              modelBuilder.Entity&lt;Timesheet&gt;()
                  .Property(x =&gt; x.Sunday)
                  .HasPrecision(10, 2);
          }
  
      }
  </code></pre>
  <p>I have an xunit test that calls the following <em>SeedData</em> method:</p>
  <pre><code>public static void SeedData(BillByTimeContext context)
          {
  
              context.Database.EnsureCreated();
  
              var timesheetHistory = new TimesheetHistory
              {
                  Timestamp = new DateTime(2022, 1, 7, 14, 30, 0),
                  StatusId = TimesheetStatus.PendingApproval
              };
  
              var timesheet = new Timesheet
              {
                  Monday = 1,
                  Tuesday = .5M,
                  Wednesday = 1,
                  Thursday = 0,
                  Friday = 1,
                  WeekCommencingMonday = new DateTime(2022, 01, 03),
                  TimesheetHistories = new List&lt;TimesheetHistory&gt; { timesheetHistory },
              };
  
              var purchaseOrder = new PurchaseOrder
              {
                  DateIssued = DateTime.Now,
                  Amount = 5462.5M,
                  Timesheets = new List&lt;Timesheet&gt; { timesheet }
              };
  
              var clientManager = new ClientManager
              {
                  FirstName = &quot;Paul&quot;,
                  LastName = &quot;Arndel&quot;,
                  Email = &quot;p.arndel@brc.com&quot;,
                  SmsNumber = &quot;867428764&quot;,
                  TimesheetHistories = new List&lt;TimesheetHistory&gt; { timesheetHistory }
              };
  
              var contract = new Contract
              {
                  UnitId = ContractUnits.Days,
                  UnitCharge = 550,
                  Timesheets = new List&lt;Timesheet&gt; { timesheet }
              };
  
              var clientOrg = new ClientOrg
              {
                  Name = &quot;BRC&quot;,
                  PurchaseOrders = new List&lt;PurchaseOrder&gt; { purchaseOrder },
                  ClientManagers = new List&lt;ClientManager&gt; { clientManager },
                  Contracts = new List&lt;Contract&gt; { contract },
                  Timesheets = new List&lt;Timesheet&gt; { timesheet }
              };
  
              var tenantManager = new TenantManager
              {
                  FirstName = &quot;Rob&quot;,
                  LastName = &quot;Bowman&quot;,
                  Email = &quot;rob@biztalkers.com&quot;,
                  ClientOrgs = new List&lt;ClientOrg&gt; { clientOrg }
              };
  
              var worker = new Worker
              {
                  FirstName = &quot;Fabio&quot;,
                  LastName = &quot;Capello&quot;,
                  Email = &quot;f.cap@ital.com&quot;,
                  Contracts = new List&lt;Contract&gt; { contract },
                  TimesheetHistories= new List&lt;TimesheetHistory&gt; { timesheetHistory }
              };
  
              var tenant = context.Tenant.FirstOrDefault(x =&gt; x.Name == &quot;BizTalkers&quot;);
              if (tenant == null)
              {
                  context.Tenant.Add(new Tenant
                  {
                      Name = &quot;BizTalkers&quot;,
                      TenantManagers = new List&lt;TenantManager&gt; { tenantManager },
                      Workers = new List&lt;Worker&gt; { worker }
                  }); ;
              }
  
              context.SaveChanges();
  </code></pre>
  <p>If I clear all data from the db and run the test, it gives the following error:</p>
  <pre><code>---- Microsoft.Data.SqlClient.SqlException : Cannot insert duplicate key row in object 'dbo.Tenant' with unique index 'IX_Tenant_Name'. The duplicate key value is ()
  </code></pre>
  <p>Can anyone please let me know why it's trying to create multiple records in the &quot;Tenant&quot; table?</p>