title: Can't map model with a couple of one to one relations with Entity Framework Core
body: <p>I'm developing my first API with ASP.NET Core, and using Entity Framework Core to model my data. I can't map 2 nested class objects within the parent one.</p>
  
  <p>I've read about one to one, and one to many, but I get nothing when I look for several one to one mappings except some posts that didn't help me.</p>
  
  <p>I've seen these:</p>
  
  <p><a href="https://stackoverflow.com/questions/39626581/entity-framework-table-with-multiple-optional-one-to-one-relationships">Entity Framework table with multiple optional one to one relationships</a></p>
  
  <p><a href="https://github.com/aspnet/EntityFrameworkCore/issues/5344" rel="nofollow noreferrer">https://github.com/aspnet/EntityFrameworkCore/issues/5344</a></p>
  
  <p><strong>This is my model</strong></p>
  
  <p>The <code>Contact</code> class owns 2 class properties address, and contact phone.
  Then in my <code>dbcontext</code>, I specify both one to one relations.
  Im using :</p>
  
  <p>Entity Framework Core .NET Command-line Tools
  2.2.3-servicing-35854</p>
  
  <p>Sql server 13.0.40001</p>
  
  <pre><code>public class Contact
  {
      public long Id { get; set; }
      [MaxLength(40)]
      [Required(ErrorMessage = "Contact name is required ")]
      public string Name { get; set; }
      [Required(ErrorMessage = "Company name is required ")]
      public string Company { get; set; }
      public string Pro`enter code here`fileImageURL { get; set; }
      [Required(ErrorMessage = "Contact Emil is required ")]
      public string Email { get; set; }
      [Required(ErrorMessage = "Birthday is required ")]
      public DateTime BirthDate { get; set; }
      [Required(ErrorMessage = "Contact phone is required ")]
      public ContactPhone ContactPhone { get; set; }
      public Address Address { get; set; }
  }
  
  public class Address
  {
      public long id { get; set; }
      public string AddressLine1 { get; set; }
      public string AddressLine2 { get; set; }
      public string City { get; set; }
      public string State { get; set; }
      public Contact Contact { get; set; }
      public long AddresssContactForeignKey { get; set; }
  }
  
  public class ContactPhone
  {
      public long ContactPhoneid { get; set; }
      public string PersonalPhone { get; set; }
      public string WorkPhone { get; set; }
      public Contact Contact { get; set; }
      public long ContactPhonesContactForeignKey { get; set; }
  }
  </code></pre>
  
  <p><strong>My DB context:</strong></p>
  
  <pre><code>public class ContactDbContext : DbContext
  {
      public DbSet&lt;Contact&gt; Contacts { get; set; }
      public DbSet&lt;ContactPhone&gt; ContactPhones { get; set; }
      public DbSet&lt;Address&gt; ContactAddress { get; set; }
  
      public ContactDbContext(DbContextOptions&lt;ContactDbContext&gt; options)
          : base(options)
      {
      }
  
      protected override void OnModelCreating(ModelBuilder modelBuilder)
      {
          modelBuilder.Entity&lt;Contact&gt;(e =&gt;
          {
              e.HasOne(x =&gt; x.ContactPhone)
                  .WithOne(y =&gt; y.Contact)
                  .HasForeignKey&lt;ContactPhone&gt;(z =&gt; 
                  z.ContactPhonesContactForeignKey);
  
              e.HasOne(x =&gt; x.Address)
                  .WithOne(y =&gt; y.Contact)
                  .HasForeignKey&lt;Address&gt;(z =&gt; 
                  z.AddresssContactForeignKey);
          });
      }
  }
  </code></pre>
  
  <p><strong>Then i apply the migration :</strong></p>
  
  <pre><code>PM&gt; Add-Migration Migration2
  </code></pre>
  
  <p>Microsoft.EntityFrameworkCore.Infrastructure[10403]
        Entity Framework Core 2.2.3-servicing-35854 initialized 'ContactDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None
  An operation was scaffolded that may result in the loss of data. Please review the migration for accuracy.
  To undo this action, use Remove-Migration.</p>
  
  <p>Seems there are no errors, then I update the database</p>
  
  <pre><code>PM&gt; update-database Microsoft.EntityFrameworkCore.Infrastructure[10403]
    Entity Framework Core 2.2.3-servicing-35854 initialized 'ContactDbContext' using provider 'Microsoft.EntityFrameworkCore.SqlServer' with options: None Microsoft.EntityFrameworkCore.Database.Command[20101]
    Executed DbCommand (29ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    SELECT OBJECT_ID(N'[__EFMigrationsHistory]'); Microsoft.EntityFrameworkCore.Database.Command[20101]
    Executed DbCommand (1ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    SELECT OBJECT_ID(N'[__EFMigrationsHistory]'); Microsoft.EntityFrameworkCore.Database.Command[20101]
    Executed DbCommand (28ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    SELECT [MigrationId], [ProductVersion]
    FROM [__EFMigrationsHistory]
    ORDER BY [MigrationId]; Microsoft.EntityFrameworkCore.Migrations[20402]
    Applying migration '20190323155442_Migration2'. Applying migration '20190323155442_Migration2'. Microsoft.EntityFrameworkCore.Database.Command[20101]
    Executed DbCommand (42ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    ALTER TABLE [ContactPhones] DROP CONSTRAINT [FK_ContactPhones_Contacts_ContactForeignKey]; Microsoft.EntityFrameworkCore.Database.Command[20101]
    Executed DbCommand (40ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    DROP INDEX [IX_ContactPhones_ContactForeignKey] ON [ContactPhones]; Microsoft.EntityFrameworkCore.Database.Command[20101]
    Executed DbCommand (385ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    DECLARE @var0 sysname;
    SELECT @var0 = [d].[name]
    FROM [sys].[default_constraints] [d]
    INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
    WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Contacts]') AND [c].[name] = N'Address');
    IF @var0 IS NOT NULL EXEC(N'ALTER TABLE [Contacts] DROP CONSTRAINT [' + @var0 + '];');
    ALTER TABLE [Contacts] DROP COLUMN [Address]; Microsoft.EntityFrameworkCore.Database.Command[20101]
    Executed DbCommand (17ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    DECLARE @var1 sysname;
    SELECT @var1 = [d].[name]
    FROM [sys].[default_constraints] [d]
    INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
    WHERE ([d].[parent_object_id] = OBJECT_ID(N'[ContactPhones]') AND [c].[name] = N'ContactForeignKey');
    IF @var1 IS NOT NULL EXEC(N'ALTER TABLE [ContactPhones] DROP CONSTRAINT [' + @var1 + '];');
    ALTER TABLE [ContactPhones] DROP COLUMN [ContactForeignKey]; fail: Microsoft.EntityFrameworkCore.Database.Command[20102]
    Failed executing DbCommand (51ms) [Parameters=[], CommandType='Text', CommandTimeout='30']
    DECLARE @var2 sysname;
    SELECT @var2 = [d].[name]
    FROM [sys].[default_constraints] [d]
    INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id]
    WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Contacts]') AND [c].[name] = N'Id');
    IF @var2 IS NOT NULL EXEC(N'ALTER TABLE [Contacts] DROP CONSTRAINT [' + @var2 + '];');
    ALTER TABLE [Contacts] ALTER COLUMN [Id] bigint NOT NULL; System.Data.SqlClient.SqlException (0x80131904): The object 'PK_Contacts' is dependent on column 'Id'. ALTER TABLE ALTER COLUMN Id failed because one or more objects access this column.    at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)    at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)    at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean&amp; dataReady)    at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)    at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)    at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()    at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues) ClientConnectionId:7bf3ba67-b8dc-4d76-a11b-c9b5ef584fad Error Number:5074,State:1,Class:16 Failed executing DbCommand (51ms) [Parameters=[], CommandType='Text', CommandTimeout='30'] DECLARE @var2 sysname; SELECT @var2 = [d].[name] FROM [sys].[default_constraints] [d] INNER JOIN [sys].[columns] [c] ON [d].[parent_column_id] = [c].[column_id] AND [d].[parent_object_id] = [c].[object_id] WHERE ([d].[parent_object_id] = OBJECT_ID(N'[Contacts]') AND [c].[name] = N'Id'); IF @var2 IS NOT NULL EXEC(N'ALTER TABLE [Contacts] DROP CONSTRAINT [' + @var2 + '];'); ALTER TABLE [Contacts] ALTER COLUMN [Id] bigint NOT NULL; System.Data.SqlClient.SqlException (0x80131904): The object 'PK_Contacts' is dependent on column 'Id'. ALTER TABLE ALTER COLUMN Id failed because one or more objects access this column. at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)    at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection, Action`1 wrapCloseInAction)    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, Boolean callerHasConnectionLock, Boolean asyncClose)    at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj, Boolean&amp; dataReady)    at System.Data.SqlClient.SqlCommand.RunExecuteNonQueryTds(String methodName, Boolean async, Int32 timeout, Boolean asyncWrite)    at System.Data.SqlClient.SqlCommand.InternalExecuteNonQuery(TaskCompletionSource`1 completion, Boolean sendToPipe, Int32 timeout, Boolean asyncWrite, String methodName)    at System.Data.SqlClient.SqlCommand.ExecuteNonQuery()    at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.Execute(IRelationalConnection connection, DbCommandMethod executeMethod, IReadOnlyDictionary`2 parameterValues)    at Microsoft.EntityFrameworkCore.Storage.Internal.RelationalCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)    at Microsoft.EntityFrameworkCore.Migrations.MigrationCommand.ExecuteNonQuery(IRelationalConnection connection, IReadOnlyDictionary`2 parameterValues)    at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)    at Microsoft.EntityFrameworkCore.Migrations.Internal.Migrator.Migrate(String targetMigration)    at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration, String contextType)    at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.&lt;&gt;c__DisplayClass0_1.&lt;.ctor&gt;b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action) ClientConnectionId:7bf3ba67-b8dc-4d76-a11b-c9b5ef584fad Error Number:5074,State:1,Class:16 The object 'PK_Contacts' is dependent on column 'Id'. ALTER TABLE ALTER COLUMN Id failed because one or more objects access this column.
  </code></pre>
  
  <p><strong>And this is the resulting Migration</strong></p>
  
  <pre><code>public partial class Migration2 : Migration
   {
       protected override void Up(MigrationBuilder migrationBuilder)
       {
           migrationBuilder.DropForeignKey(
               name: "FK_ContactPhones_Contacts_ContactForeignKey",
               table: "ContactPhones");
  
           migrationBuilder.DropIndex(
               name: "IX_ContactPhones_ContactForeignKey",
               table: "ContactPhones");
  
           migrationBuilder.DropColumn(
               name: "Address",
               table: "Contacts");
  
           migrationBuilder.DropColumn(
               name: "ContactForeignKey",
               table: "ContactPhones");
  
           migrationBuilder.AlterColumn&lt;long&gt;(
               name: "Id",
               table: "Contacts",
               nullable: false,
               oldClrType: typeof(int))
               .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
               .OldAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
  
           migrationBuilder.AlterColumn&lt;long&gt;(
               name: "ContactPhoneid",
               table: "ContactPhones",
               nullable: false,
               oldClrType: typeof(int))
               .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
               .OldAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
  
           migrationBuilder.AddColumn&lt;long&gt;(
               name: "ContactPhonesContactForeignKey",
               table: "ContactPhones",
               nullable: false,
               defaultValue: 0L);
  
           migrationBuilder.CreateTable(
               name: "ContactAddress",
               columns: table =&gt; new
               {
                   id = table.Column&lt;long&gt;(nullable: false)
                       .Annotation("SqlServer:ValueGenerationStrategy",
  SqlServerValueGenerationStrategy.IdentityColumn),
                   AddressLine1 = table.Column&lt;string&gt;(nullable: true),
                   AddressLine2 = table.Column&lt;string&gt;(nullable: true),
                   City = table.Column&lt;string&gt;(nullable: true),
                   State = table.Column&lt;string&gt;(nullable: true),
                   AddresssContactForeignKey = table.Column&lt;long&gt;(nullable: false)
               },
               constraints: table =&gt;
               {
                   table.PrimaryKey("PK_ContactAddress", x =&gt; x.id);
                   table.ForeignKey(
                       name: "FK_ContactAddress_Contacts_AddresssContactForeignKey",
                       column: x =&gt; x.AddresssContactForeignKey,
                       principalTable: "Contacts",
                       principalColumn: "Id",
                       onDelete: ReferentialAction.Cascade);
               });
  
           migrationBuilder.CreateIndex(
               name: "IX_ContactPhones_ContactPhonesContactForeignKey",
               table: "ContactPhones",
               column: "ContactPhonesContactForeignKey",
               unique: true);
  
           migrationBuilder.CreateIndex(
               name: "IX_ContactAddress_AddresssContactForeignKey",
               table: "ContactAddress",
               column: "AddresssContactForeignKey",
               unique: true);
  
           migrationBuilder.AddForeignKey(
               name: "FK_ContactPhones_Contacts_ContactPhonesContactForeignKey",
               table: "ContactPhones",
               column: "ContactPhonesContactForeignKey",
               principalTable: "Contacts",
               principalColumn: "Id",
               onDelete: ReferentialAction.Cascade);
       }
  
       protected override void Down(MigrationBuilder migrationBuilder)
       {
           migrationBuilder.DropForeignKey(
               name: "FK_ContactPhones_Contacts_ContactPhonesContactForeignKey",
               table: "ContactPhones");
  
           migrationBuilder.DropTable(
               name: "ContactAddress");
  
           migrationBuilder.DropIndex(
               name: "IX_ContactPhones_ContactPhonesContactForeignKey",
               table: "ContactPhones");
  
           migrationBuilder.DropColumn(
               name: "ContactPhonesContactForeignKey",
               table: "ContactPhones");
  
           migrationBuilder.AlterColumn&lt;int&gt;(
               name: "Id",
               table: "Contacts",
               nullable: false,
               oldClrType: typeof(long))
               .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
               .OldAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
  
           migrationBuilder.AddColumn&lt;string&gt;(
               name: "Address",
               table: "Contacts",
               nullable: false,
               defaultValue: "");
  
           migrationBuilder.AlterColumn&lt;int&gt;(
               name: "ContactPhoneid",
               table: "ContactPhones",
               nullable: false,
               oldClrType: typeof(long))
               .Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn)
               .OldAnnotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn);
  
           migrationBuilder.AddColumn&lt;int&gt;(
               name: "ContactForeignKey",
               table: "ContactPhones",
               nullable: false,
               defaultValue: 0);
  
           migrationBuilder.CreateIndex(
               name: "IX_ContactPhones_ContactForeignKey",
               table: "ContactPhones",
               column: "ContactForeignKey",
               unique: true);
  
           migrationBuilder.AddForeignKey(
               name: "FK_ContactPhones_Contacts_ContactForeignKey",
               table: "ContactPhones",
               column: "ContactForeignKey",
               principalTable: "Contacts",
               principalColumn: "Id",
               onDelete: ReferentialAction.Cascade);
       }
   }
  </code></pre>
  
  <p>Finally the console throws this :</p>
  
  <blockquote>
    <p>Error Number:5074,State:1,Class:16<br>
    The object 'PK_Contacts' is dependent on column 'Id'.<br>
    ALTER TABLE ALTER COLUMN Id failed because one or more objects access this column.</p>
  </blockquote>
  
  <p>I expect the mapping to be 1:1</p>
  
  <blockquote>
    <p><strong>UPDATE</strong></p>
  </blockquote>
  
  <p>Ive found the problem.
  I used to have all my entities Ids beint <em>int</em>, then i changed them to <em>long</em>
  I started a new project with the written code and started a new database with a first migration and it worked
  THanks to everyone that was so nice to help me through it !!</p>