Fala Lucas!
Seguem as classes abaixo:
Classe cliente
public class Cliente
{
public int Id { get; set; }
public string Nome { get; internal set; }
public Endereco EnderecoDeEntrega { get; set; }
}
Classe endereço
public class Endereco
{
public int Numero { get; internal set; }
public string Logradouro { get; internal set; }
public string Complemento { get; internal set; }
public string Bairro { get; internal set; }
public string Cidade { get; internal set; }
public Cliente Cliente { get; set; }
}
Migration Cliente
public partial class Cliente : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "Enderecos",
columns: table => new
{
ClienteId = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
Bairro = table.Column<string>(type: "nvarchar(max)", nullable: true),
Cidade = table.Column<string>(type: "nvarchar(max)", nullable: true),
Complemento = table.Column<string>(type: "nvarchar(max)", nullable: true),
Logradouro = table.Column<string>(type: "nvarchar(max)", nullable: true),
Numero = table.Column<int>(type: "int", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Enderecos", x => x.ClienteId);
});
migrationBuilder.CreateTable(
name: "Clientes",
columns: table => new
{
Id = table.Column<int>(type: "int", nullable: false)
.Annotation("SqlServer:ValueGenerationStrategy", SqlServerValueGenerationStrategy.IdentityColumn),
EnderecoDeEntregaClienteId = table.Column<int>(type: "int", nullable: true),
Nome = table.Column<string>(type: "nvarchar(max)", nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_Clientes", x => x.Id);
table.ForeignKey(
name: "FK_Clientes_Enderecos_EnderecoDeEntregaClienteId",
column: x => x.EnderecoDeEntregaClienteId,
principalTable: "Enderecos",
principalColumn: "ClienteId",
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateIndex(
name: "IX_Clientes_EnderecoDeEntregaClienteId",
table: "Clientes",
column: "EnderecoDeEntregaClienteId",
unique: true,
filter: "[EnderecoDeEntregaClienteId] IS NOT NULL");
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "Clientes");
migrationBuilder.DropTable(
name: "Enderecos");
}
}
Não sei o por que, mas a formatação da migration ficou bagunçada, espero que dê pra entender!
Obrigado Lucas!