I'm adding a row to Videos and then I want to use that same ID as the video's physical filename. so I need to add the row without the filename, and then use the ID I get, then update that row with the filename. I'm just now sure how to do that.
我正在为视频添加一行,然后我想使用与视频的物理文件名相同的ID。所以我需要添加没有文件名的行,然后使用我得到的ID,然后使用文件名更新该行。我现在肯定该怎么做。
public ActionResult UpVideo(Report Report, string VideoName, HttpPostedFileBase file)
{
Video v = new Video();
v.Name = VideoName;
v.Report = Report;
db.Videos.Add(v);
var filename = v.ID + "." + Path.GetExtension(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/video_uploads"), filename);
file.SaveAs(path);
v.FileName = filename;
//** update Row here with filename
db.SaveChanges();
//** redirect back to Report Details (need to figure out how do do this too)
return RedirectToAction(Report);
}
1 个解决方案
#1
1
Assuming that you have an auto incrementing primary key for ID in your database, you need to call save changes before referencing the ID property. You have to save the entity to the database so that the ID can be assigned.
假设数据库中有ID的自动递增主键,则需要在引用ID属性之前调用保存更改。您必须将实体保存到数据库,以便可以分配ID。
public ActionResult UpVideo(Report Report, string VideoName, HttpPostedFileBase file)
{
Video v = new Video();
v.Name = VideoName;
v.Report = Report;
db.Videos.Add(v);
db.SaveChanges();
var filename = v.ID + "." + Path.GetExtension(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/video_uploads"), filename);
file.SaveAs(path);
v.FileName = filename;
db.SaveChanges();
//** redirect back to Report Details (need to figure out how do do this too)
return RedirectToAction(Report);
}
Since 'filename' is only a combination of the Id and the file extension, why not just save the file extension and do the concatenation when you need to reference the video. This would reduce your calls to the database back down to one and save a little on DB storage
由于'filename'只是Id和文件扩展名的组合,为什么不只是保存文件扩展名并在需要引用视频时进行连接。这会将您对数据库的调用减少到一个,并节省一点数据库存储空间
public ActionResult UpVideo(Report Report, string VideoName, HttpPostedFileBase file)
{
Video v = new Video();
v.Name = VideoName;
v.FileName = Path.GetExtension(file.FileName);
v.Report = Report;
db.Videos.Add(v);
db.SaveChanges();
var filename = v.ID + "." + v.FileName;
var path = Path.Combine(Server.MapPath("~/App_Data/video_uploads"), filename);
file.SaveAs(path);
//** redirect back to Report Details (need to figure out how do do this too)
return RedirectToAction(Report);
}
#1
1
Assuming that you have an auto incrementing primary key for ID in your database, you need to call save changes before referencing the ID property. You have to save the entity to the database so that the ID can be assigned.
假设数据库中有ID的自动递增主键,则需要在引用ID属性之前调用保存更改。您必须将实体保存到数据库,以便可以分配ID。
public ActionResult UpVideo(Report Report, string VideoName, HttpPostedFileBase file)
{
Video v = new Video();
v.Name = VideoName;
v.Report = Report;
db.Videos.Add(v);
db.SaveChanges();
var filename = v.ID + "." + Path.GetExtension(file.FileName);
var path = Path.Combine(Server.MapPath("~/App_Data/video_uploads"), filename);
file.SaveAs(path);
v.FileName = filename;
db.SaveChanges();
//** redirect back to Report Details (need to figure out how do do this too)
return RedirectToAction(Report);
}
Since 'filename' is only a combination of the Id and the file extension, why not just save the file extension and do the concatenation when you need to reference the video. This would reduce your calls to the database back down to one and save a little on DB storage
由于'filename'只是Id和文件扩展名的组合,为什么不只是保存文件扩展名并在需要引用视频时进行连接。这会将您对数据库的调用减少到一个,并节省一点数据库存储空间
public ActionResult UpVideo(Report Report, string VideoName, HttpPostedFileBase file)
{
Video v = new Video();
v.Name = VideoName;
v.FileName = Path.GetExtension(file.FileName);
v.Report = Report;
db.Videos.Add(v);
db.SaveChanges();
var filename = v.ID + "." + v.FileName;
var path = Path.Combine(Server.MapPath("~/App_Data/video_uploads"), filename);
file.SaveAs(path);
//** redirect back to Report Details (need to figure out how do do this too)
return RedirectToAction(Report);
}