Owned object's Id missing
EF 2.1 creates the migration miss the Id property of the valueobject. if i change the Id to EmpId , it's create correctly.
public class Project : AuditableEntity, IAggregateRoot
{
public string No { get; private set; }
public string Name { get; private set; }
public Employee Owner { get; private set; }
public DateTime PreparationDate { get; private set; }
public DateTime? StartDate { get; private set; }
public DateTime? EndDate { get; private set; }
public Employee Sponsor { get; private set; }
public ProjectStatus Status { get; private set; }
}
public class Employee:ValueObject<Employee>
{
internal static readonly Employee Empty = new Employee();
public int? Id { get; private set; }
[MaxLength(30)]
public string Name { get; private set; }
private Employee() { }
public Employee(int id, string name)
{
Id = id;
Name = name;
}
}
migrations missing Owner_Id & Sponsor_Id:
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateSequence(
name: "ProjectSeq",
incrementBy: 10);
migrationBuilder.CreateTable(
name: "Project",
columns: table => new
{
Id = table.Column<int>(nullable: false),
CreatorId = table.Column<int>(nullable: false),
CreatorName = table.Column<string>(maxLength: 30, nullable: true),
CreateAt = table.Column<DateTime>(nullable: false),
UpdaterId = table.Column<int>(nullable: false),
UpdaterName = table.Column<string>(maxLength: 30, nullable: true),
UpdateAt = table.Column<DateTime>(nullable: false),
No = table.Column<string>(maxLength: 30, nullable: false),
Name = table.Column<string>(maxLength: 300, nullable: false),
Owner_Name = table.Column<string>(maxLength: 30, nullable: true),
PreparationDate = table.Column<DateTime>(nullable: false),
StartDate = table.Column<DateTime>(nullable: true),
EndDate = table.Column<DateTime>(nullable: true),
Sponsor_Name = table.Column<string>(maxLength: 30, nullable: true),
Status = table.Column<int>(nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_Project", x => x.Id);
});
}
@Cuiqs76 Because the two entity types are being mapped to the same table (table-splitting), they must share the same primary key. The common way this is used with owned types is to not give the owned type a key property at all, in which case a shadow key property is created that maps to the same column as the owner primary key. When you create an Id property, then this is picked up by convention as the key property on the owned type, which means this shadow key is not created. So now the Id property is mapped to the same column as the owner primary key, and hence you don't see a separate column. If the property is named EmpId then it no longer matches the convention for a key property and hence it is just mapped like any column. However, this property is not the key/ID for the owned entity and doesn't have any key semantics, it's just a regular property like any other.