在我的数据库c#中添加一个对象
本文关键字:添加 一个对象 我的 数据库 | 更新日期: 2023-09-27 18:09:28
我正试图将一个对象放入我的数据库中,我想知道这是否可能。目前这是我的代码
private async void SaveSchedule()
{
using (HttpClient client = new HttpClient())
{
IList<Item> lstABC = LstSchedule.ToList();
foreach(Item i in LstSchedule)
{
lstABC.Add(i);
}
Session s = new Session();
s.Name = Text;
s.Items = lstABC;
string json = JsonConvert.SerializeObject(s);
HttpResponseMessage response = await client.PostAsync("http://******:****/api/session", new StringContent(json, Encoding.UTF8, "application/json"));
if (response.IsSuccessStatusCode)
{
string jsonresponse = await response.Content.ReadAsStringAsync();
int result = JsonConvert.DeserializeObject<int>(jsonresponse);
}
}
这里的问题是我的s。items是一个列表里面有对象比如x y z
我怎么能得到它进入我的数据库像Name = Text, Items = object1x,object1y,object1z;object2x、object2y object2z…
这是会话的模型:
public class Session
{
public int ID { get; set; }
public string Name { get; set; }
public IList<Item> Items { get; set; }
}
试试这个
public async Task<Uri> Post<T>(T obj, Uri url)
{
using (var client = new HttpClient())
{
var response = await client.PostAsJsonAsync(url, obj);
response.EnsureSuccessStatusCode();
return response.Headers.Location;
}
}