我们如何使用 C# 和 EF Core 获取最后插入的行集的 ID 列表
How can we get list of IDs of last inserted set of rows using C# and EF Core
我有一组图像,我尝试使用 EF Core 将它们添加到 table (ImageGalleries) 中。插入很顺利。插入后,我需要插入记录的 ID(table 中有一个标识列)。我在 Whosebug 中尝试了以下 post。但是我联系不上。
public ActionResult Save(IEnumerable<IFormFile> images,RoomGalleryVM vm)
{
if (images!= null)
{
string fileName = "";
foreach (var image in images)
{
var fileContent = ContentDispositionHeaderValue.Parse(image.ContentDisposition);
fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
var gallery = new RoomGallery()
{
RoomId = vm.RoomId,
Description=fileName,
Status="A"
};
Context.RoomGalleries.Add(gallery);
}
Context.SaveChanges();
//var Ids = vm.Select(c => c.Id).ToList();
}
return View("RoomGalleryIndex");
}
RoomGalleryVM
public class RoomGallery
{
public int Id { get; set; }
public int RoomId { get; set; }
public Room Room { get; set; }
[StringLength(100)] public string Description { get; set; }
[StringLength(10)] public string Status { get; set; }
}
索引页
<form method="post" asp-action="Save" asp-controller="RoomGallery">
<h6 style="margin-top: 2em;">Room:</h6>
@(Html.Kendo().DropDownList()
.Name("RoomId")
.HtmlAttributes(new { style = "width:100%" })
.OptionLabel("Select room...")
.DataTextField("RoomName")
.DataValueField("Id")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetRooms", "Reservation");
})
.ServerFiltering(true);
})
.Enable(false)
.AutoBind(false)
)
<br />
<div class="demo-section">
@(Html.Kendo().Upload()
.Name("images")
.HtmlAttributes(new { aria_label = "files" })
)
<p style="padding-top: 1em; text-align: right">
<button>Submit</button>
</p>
</div>
</form>
只需记住您创建的所有图库,并在保存后检索它们的 ID:
var newRGs = new List<RoomGallery>();
foreach (var image in images)
{
var fileContent = ContentDispositionHeaderValue.Parse(image.ContentDisposition);
fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
var gallery = new RoomGallery()
{
RoomId = vm.RoomId,
Description=fileName,
Status="A"
};
Context.RoomGalleries.Add(gallery);
newRGs.Add(gallery);
}
然后你可以在保存后得到你需要的任何ID(数据库计算的值将被修补回本地对象)
var ids = newRGs.Select(rg => rg.Id).ToList();
如果你想在你的视图中使用它们,请不要忘记将它们修补回你的视图模型(我不清楚你想用它们做什么;这个答案的要点是“如果你记住在保存之前将哪些对象添加到上下文中,然后您可以在保存后为任何数据库计算值枚举它们")
将您的代码更新到下面,
public ActionResult Save(IEnumerable<IFormFile> images,RoomGalleryVM vm)
{
List<int> Ids=new List<int>();
if (images!= null)
{
string fileName = "";
foreach (var image in images)
{
var fileContent = ContentDispositionHeaderValue.Parse(image.ContentDisposition);
fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
var gallery = new RoomGallery()
{
RoomId = vm.RoomId,
Description=fileName,
Status="A"
};
Context.RoomGalleries.Add(gallery);
Context.SaveChanges();
Ids.Add(gallery.Id)
}
}
var data = Ids; // here you will get list of ids
return View("RoomGalleryIndex");
}
我有一组图像,我尝试使用 EF Core 将它们添加到 table (ImageGalleries) 中。插入很顺利。插入后,我需要插入记录的 ID(table 中有一个标识列)。我在 Whosebug 中尝试了以下 post。但是我联系不上。
public ActionResult Save(IEnumerable<IFormFile> images,RoomGalleryVM vm)
{
if (images!= null)
{
string fileName = "";
foreach (var image in images)
{
var fileContent = ContentDispositionHeaderValue.Parse(image.ContentDisposition);
fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
var gallery = new RoomGallery()
{
RoomId = vm.RoomId,
Description=fileName,
Status="A"
};
Context.RoomGalleries.Add(gallery);
}
Context.SaveChanges();
//var Ids = vm.Select(c => c.Id).ToList();
}
return View("RoomGalleryIndex");
}
RoomGalleryVM
public class RoomGallery
{
public int Id { get; set; }
public int RoomId { get; set; }
public Room Room { get; set; }
[StringLength(100)] public string Description { get; set; }
[StringLength(10)] public string Status { get; set; }
}
索引页
<form method="post" asp-action="Save" asp-controller="RoomGallery">
<h6 style="margin-top: 2em;">Room:</h6>
@(Html.Kendo().DropDownList()
.Name("RoomId")
.HtmlAttributes(new { style = "width:100%" })
.OptionLabel("Select room...")
.DataTextField("RoomName")
.DataValueField("Id")
.DataSource(source =>
{
source.Read(read =>
{
read.Action("GetRooms", "Reservation");
})
.ServerFiltering(true);
})
.Enable(false)
.AutoBind(false)
)
<br />
<div class="demo-section">
@(Html.Kendo().Upload()
.Name("images")
.HtmlAttributes(new { aria_label = "files" })
)
<p style="padding-top: 1em; text-align: right">
<button>Submit</button>
</p>
</div>
</form>
只需记住您创建的所有图库,并在保存后检索它们的 ID:
var newRGs = new List<RoomGallery>();
foreach (var image in images)
{
var fileContent = ContentDispositionHeaderValue.Parse(image.ContentDisposition);
fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
var gallery = new RoomGallery()
{
RoomId = vm.RoomId,
Description=fileName,
Status="A"
};
Context.RoomGalleries.Add(gallery);
newRGs.Add(gallery);
}
然后你可以在保存后得到你需要的任何ID(数据库计算的值将被修补回本地对象)
var ids = newRGs.Select(rg => rg.Id).ToList();
如果你想在你的视图中使用它们,请不要忘记将它们修补回你的视图模型(我不清楚你想用它们做什么;这个答案的要点是“如果你记住在保存之前将哪些对象添加到上下文中,然后您可以在保存后为任何数据库计算值枚举它们")
将您的代码更新到下面,
public ActionResult Save(IEnumerable<IFormFile> images,RoomGalleryVM vm)
{
List<int> Ids=new List<int>();
if (images!= null)
{
string fileName = "";
foreach (var image in images)
{
var fileContent = ContentDispositionHeaderValue.Parse(image.ContentDisposition);
fileName = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
var gallery = new RoomGallery()
{
RoomId = vm.RoomId,
Description=fileName,
Status="A"
};
Context.RoomGalleries.Add(gallery);
Context.SaveChanges();
Ids.Add(gallery.Id)
}
}
var data = Ids; // here you will get list of ids
return View("RoomGalleryIndex");
}