-
Notifications
You must be signed in to change notification settings - Fork 3.3k
Closed
Copy link
Description
modelBuilder.Entity<Person>(b =>
{
b.ComplexProperty(p => p.Details, pb =>
{
pb.ToJson();
});
});public class Person
{
public Guid Id { get; set; }
public string Name { get; set; } = null!;
public PersonDetails Details { get; set; }
}
public struct PersonDetails()
{
public List<Address> Addresses { get; set; } = [];
public List<Phone> Phones { get; set; } = [];
}
public class Address
{
public string City { get; set; } = null!;
public string State { get; set; } = null!;
public string Street { get; set; } = null!;
public string ZipCode { get; set; } = null!;
}
public class Phone
{
public string Number { get; set; } = null!;
public PhoneType Type { get; set; } = PhoneType.Mobile;
}
public enum PhoneType
{
Mobile,
Home,
Work,
Other
}Snapshot:
protected override void BuildModel(ModelBuilder modelBuilder)
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "10.0.0-rc.1.25418.116")
.HasAnnotation("Relational:MaxIdentifierLength", 128);
SqlServerModelBuilderExtensions.UseIdentityColumns(modelBuilder);
modelBuilder.Entity("Person", b =>
{
b.Property<Guid>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("uniqueidentifier");
b.Property<string>("Name")
.IsRequired()
.HasColumnType("nvarchar(max)");
b.ComplexProperty(typeof(Dictionary<string, object>), "Details", "Person.Details#PersonDetails", b1 =>
{
b1.IsRequired();
b1.ComplexCollection(typeof(Dictionary<string, object>), "Addresses", "Person.Details#PersonDetails.Addresses#Address", b2 =>
{
b2.IsRequired();
b2.Property<string>("City")
.IsRequired();
b2.Property<string>("State")
.IsRequired();
b2.Property<string>("Street")
.IsRequired();
b2.Property<string>("ZipCode")
.IsRequired();
});
b1.ComplexCollection(typeof(Dictionary<string, object>), "Phones", "Person.Details#PersonDetails.Phones#Phone", b2 =>
{
b2.IsRequired();
b2.Property<string>("Number")
.IsRequired();
b2.Property<int>("Type");
});
b1.ToJson("Details");
});
b.HasKey("Id");
b.ToTable("Persons");
});
#pragma warning restore 612, 618
}