【发布时间】:2016-11-02 17:00:01
【问题描述】:
请注意:这是我的“share your knowledge”问题!
我有一个 Web 客户端,它以 JSON 格式向我的 ASP.NET 应用程序发送数据。数据是对象图或对象图的集合。
使用 Web Api 控制器将数据反序列化为 C# 对象图。我想用 ADO.NET 和 one 存储过程保存那个 C# 对象图。
我想在没有 GUID 和 EF 的情况下执行此操作! :)
假设 Web 客户端发送由三个对象组成的对象图:
-
GrandRecord -
Record ChildRecord
让它成为GrandRecords 的集合,其中:
- 每个
GrandRecord都有一个Records的集合 - 并且每个
Record都有一个ChildRecords的集合 - Id 值是整数,由数据库自动生成
- 虽然对象未保存在数据库中,但 Id 的值 = 0
这是对象图集合(或对象图)的示例:
Id, Name
GrandRecord 1, (A)
Record |-- 2, (A)A
ChildRecord |-- 3, (A)Aa
ChildRecord |-- 0, (A)Ab
Record |-- 0, (A)B
ChildRecord |-- 0, (A)Ba
ChildRecord |-- 0, (A)Bb
GrandRecord 0, (B)
Record |-- 0, (B)A
或者同样的JSON格式:
grandRecords: [
{
id: 1,
name: "(A)",
records: [
{
id: 2,
name: "(A)A",
childRecords: [
{
id: 3,
name: "(A)Aa",
},
{
id: 0,
name: "(A)b",
},
]
},
{
id: 0,
name: "(A)B",
childRecords: [
{
id: 0,
name: "(A)Ba",
},
{
id: 0,
name: "(A)Bb",
},
]
}
]
},
{
id: 0,
name: "(B)",
records: [
{
id: 0,
name: "(B)A",
childRecords: []
}
]
}
]
在 ASP.NET 控制器的 Web 服务器上,上述 JSON 字符串被反序列化为三个类的对象图:
public class GrandRecord
{
public Int32 Id { get; set; }
public String Name { get; set; }
public IList<Record> Records { get; set; }
}
public class Record
{
public Int32 Id { get; set; }
public Int32 GrandRecordId { get; set; }
public String Name { get; set; }
public IList<ChildRecord> ChildRecords { get; set; }
}
public class ChildRecord
{
public Int32 Id { get; set; }
public Int32 RecordId { get; set; }
public String Name { get; set; }
}
现在必须用一个存储过程将对象图保存到三个数据库表中:
create table dbo.GrandRecords
(
Id int not null identity primary key clustered,
Name varchar(30) not null
);
create table dbo.Records
(
Id int not null identity primary key clustered,
GrandRecordId int not null foreign key (GrandRecordId) references dbo.GrandRecords (Id) on delete cascade,
Name varchar(30) not null
);
create table dbo.ChildRecords
(
Id int not null identity primary key clustered,
RecordId int not null foreign key (RecordId) references dbo.Records (Id) on delete cascade,
Name varchar(30) not null
);
问题是如何?
【问题讨论】:
标签: c# sql-server stored-procedures orm ado.net