Witam
Mam sobie klasę User, która zawiera właściwość PhoneNumber w postaci rekordu, który jest obiektem wartościowym.
PhoneNumber konfiguruję jako "Owned Type". Niestety, ale PhoneNumber podczas generowania migracji jest tworzony jako oddzielna tabela, a nie jako dodatkowa kolumna w tabeli User.
Przejrzałem wszelkie materiały w internecie i wszędzie jest to tworzone jako dodatkowa kolumna zamiast oddzielna tabela.
Np tutaj https://learn.microsoft.com/en-us/ef/core/modeling/owned-entities
Niestety nie mogę użyć "Complex Type" gdyż nie mogę być one "nullowalne".
Jest tu jakiś mądry co poradzi jak to zrobić aby migracja generowała PhoneNumber jako kolumnę w tabeli?
public class User
{
public Guid Id { get; private set; }
...
public PhoneNumber PhoneNumber { get; private set; }
}
public sealed record PhoneNumber
{
private PhoneNumber() { }
public string Value { get; }
public PhoneNumber(string phoneNumber)
{
if (!string.IsNullOrWhiteSpace(phoneNumber) && phoneNumber.Length is < 3 or > 60)
throw new InvalidPhoneNumberException();
Value = phoneNumber;
}
public static implicit operator PhoneNumber(string phoneNumber) => new(phoneNumber);
public static implicit operator string(PhoneNumber phoneNumber) => phoneNumber.Value;
public override string ToString() => Value;
public bool Contains(string value) => Value.Contains(value);
}
builder.OwnsOne(x => x.PhoneNumber, phoneNumber =>
{
phoneNumber.Property(p => p.Value)
.HasColumnName(nameof(User.PhoneNumber))
.HasMaxLength(300)
.IsRequired(false);
});
migrationBuilder.CreateTable(
name: "PhoneNumber",
schema: "Identity",
columns: table => new
{
UserId = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
PhoneNumber = table.Column<string>(type: "nvarchar(300)", maxLength: 300, nullable: true)
},
constraints: table =>
{
table.PrimaryKey("PK_PhoneNumber", x => x.UserId);
});
migrationBuilder.CreateTable(
name: "User",
schema: "Identity",
columns: table => new
{
Id = table.Column<Guid>(type: "uniqueidentifier", nullable: false),
...
},