英文:
How to populate complex entity from flat table
问题
I have a table which is flat from which data is retrieved and then is packaged as JSON and included as part of an API request. This is straightforward.
However, I now have an API spec that expects data like this:
{
"first_name" : "Greg",
"last_name" : "Gum",
"billing_address" : {
"address" : "123 Main St",
"city" : "New York",
"state" : "New York",
"zip" : "12345"
}
}
This data is stored in a flat table (there is only ever one billing address, so there is no reason to have a separate address table) so it's not really a parent/child relationship.
Is it possible to map this from a flat table to a complex object using only attributes? The only way I see doing this is by hand. In other words, the Entity matches the table, and then I have yet another entity which matches the JSON, and the code maps it manually from one field to the other.
It just seems rather crude to be writing mapping code for this.
英文:
I have a table which is flat from which data is retrieved and then is packaged as json and included as part of an API request. This is straightforward.
However, I now have an api spec that is expects data like this:
{
"first_name" : "Greg",
"last_name" : "Gum",
"billing_address" : {
"address" : "123 Main St",
"city" : "New York",
"state" : "New York",
"zip" : "12345"
}
This data is stored in a flat table (there is only ever one billing address, so there is no reason to have a separate address table) so it's not really a parent/child relationship.
Is it possible to map this from a flat table to a complex object using only attributes? The only way I see doing this is by hand. In other words, the Entity matches the table, and then I have yet another entity which matches the Json and the code maps it manually from one field to the other.
It just seems rather crude to be writing mapping code for this.
答案1
得分: 2
以下是您要翻译的内容:
这可以通过拥有的实体类型来完成。
不知道数据库模式的情况下,让我们假设这些类适用:
class Person
{
public int Id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public Address billing_address { get; set; }
}
class Address
{
public string address { get; set; }
public string city { get; set; }
public string state { get; set; }
public string zip { get; set; }
}
在上下文的OnModelCreating
重写中,您可以这样做:
var ownedNavigationBuilder = modelBuilder.Entity<Person>().OwnsOne(p => p.billing_address);
// 仅用于显示一些自定义映射到数据库列名:
ownedNavigationBuilder.Property(a => a.address).HasColumnName("Address");
ownedNavigationBuilder.Property(a => a.city).HasColumnName("City");
这将构建以下数据库表(在启用了SQL Server和C#可空引用类型的情况下):
CREATE TABLE [People] (
[Id] int NOT NULL IDENTITY,
[first_name] nvarchar(max) NOT NULL,
[last_name] nvarchar(max) NOT NULL,
[Address] nvarchar(max) NOT NULL,
[City] nvarchar(max) NOT NULL,
[billing_address_state] nvarchar(max) NOT NULL,
[billing_address_zip] nvarchar(max) NOT NULL,
CONSTRAINT [PK_People] PRIMARY KEY ([Id])
);
现在,带有Address
对象的新Person
将作为此表中的一条记录保存,并在查询中重新生成为带有嵌套的Address
的Person
。请注意,查询不需要Include
。拥有的地址将始终包含在结果中。
英文:
This can be done using owned entity types.
Not knowing the database schema, let's assume that these classes fit the bill:
class Person
{
public int Id { get; set; }
public string first_name { get; set; }
public string last_name { get; set; }
public Address billing_address { get; set; }
}
class Address
{
public string address { get; set; }
public string city { get; set; }
public string state { get; set; }
public string zip { get; set; }
}
In the context's OnModelCreating
override you could have:
var ownedNavigationBuilder = modelBuilder.Entity<Person>().OwnsOne(p => p.billing_address);
// Just to show some custom mapping to db column names:
ownedNavigationBuilder.Property(a => a.address).HasColumnName("Address");
ownedNavigationBuilder.Property(a => a.city).HasColumnName("City");
This builds the following database table (in SQL Server and C# nullable reference types enabled):
CREATE TABLE [People] (
[Id] int NOT NULL IDENTITY,
[first_name] nvarchar(max) NOT NULL,
[last_name] nvarchar(max) NOT NULL,
[Address] nvarchar(max) NOT NULL,
[City] nvarchar(max) NOT NULL,
[billing_address_state] nvarchar(max) NOT NULL,
[billing_address_zip] nvarchar(max) NOT NULL,
CONSTRAINT [PK_People] PRIMARY KEY ([Id])
);
Now a new Person
with an Address
object will be saved as one record in this table and in queries be re-materialized as a Person
with the nested Address
. Note that queries don't require Include
. The owned address will always be included in the result.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论