【发布时间】:2019-03-22 00:30:26
【问题描述】:
我有一个正在创建子实体的 Web 应用控制器操作。我有一个带有 LocationPic 集合的 Location 模型。我正在尝试将新的 LocationPic 添加到现有位置。在本地这工作正常,但是当我在 Azure 上运行它时,会创建 LocationPic 但不引用位置。所以我最终得到了一个不知道它属于哪个位置的孤立 LocationPic。
此外,对于已经有图片的位置,它在本地和 Azure 上都可以正常工作(我有一个单独的 API 控制器,似乎可以正常工作)。所以我可以将新图片添加到已经有一些的位置。但我无法将新照片添加到没有任何照片的新位置。
这是我的控制器操作:
// POST: Pictures/Create
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Create([Bind("LocationID,Name,Description")] LocationPic locationPic, IFormFile image)
{
var location = await _context.Locations.FindAsync(locationPic.LocationID);
if (location == null)
{
return NotFound();
}
Trace.TraceInformation($"Create Pic for location[{location.LocationID}:{location.LocationName}]");
_userService = new UserService((ClaimsIdentity)User.Identity, _context);
if (!_userService.IsAdmin)
{
return Unauthorized();
}
if (image == null)
{
return BadRequest();
}
if (String.IsNullOrEmpty(locationPic.Name))
{
locationPic.Name = image.FileName;
}
var helper = new AzureTools();
var filename = await helper.GetFileName(image);
locationPic.FileName = filename;
//Added this to try to force LocationPics to be initialized
if (location.LocationPics == null)
{
Trace.TraceInformation("location.LocationPics is null");
location.LocationPics = new List<LocationPic>();
}
else
{
Trace.TraceInformation($"location.LocationPics count == {location.LocationPics.Count}");
}
if (ModelState.IsValid)
{
Trace.TraceInformation($"Location pic valid: [{locationPic.LocationID}] {locationPic.Name}");
//Overly-explicit attempts to get entities linked
locationPic.Location = location;
location.LocationPics.Add(locationPic);
//I've tried _context.LocationPics.Add as well and seemingly no difference
_context.Add(locationPic);
await _context.SaveChangesAsync();
return RedirectToAction(nameof(Edit), new { locationID = location.LocationID });
} else
{
Trace.TraceInformation("Invalid model state");
return BadRequest();
}
}
所有正确的信息似乎都从我的表单正确地进入了我的参数,并且 LocationPic 被创建得很好。尽管“LocationID”在保存之前正确显示,但它只是没有链接到位置。另外,我得到了正确的重定向回 Edit 操作,而不是 BadRequest 或任何东西。
在本地,我注意到的唯一区别是没有图片的 Location 有一个 LocationPics,它是一个 Count==0 的空集合。在 Azure 上,没有图片的位置似乎有一个为空的 LocationPics。所以这就是为什么我尝试添加初始化它的位(如果它为空),尽管我工作的 API 控制器不需要做任何事情。
这是我的(工作)API 控制器操作,供参考:
[HttpPost("{id}", Name = "PostPicture")]
public async Task<IActionResult> PostPicture([FromRoute] int id, IFormFile image, string Name, string Description)
{
var location = _context.Locations.Find(id);
if (location == null)
{
return NotFound();
}
_userService = new UserService((ClaimsIdentity)User.Identity, _context);
if (!_userService.IsCustomer(location.CustomerID))
{
return Unauthorized();
}
if (image == null)
{
return BadRequest();
}
if (String.IsNullOrEmpty(Name))
{
Name = image.FileName;
}
var helper = new AzureTools();
var filename = await helper.GetFileName(image);
var locationPic = new LocationPic
{
Name = Name,
FileName = filename,
Description = Description,
Location = location
};
_context.LocationPics.Add(locationPic);
_context.SaveChanges();
return Ok(filename);
}
【问题讨论】:
-
LocationPic和Location的模型定义是什么?您是否从本地和 Azure 访问相同的数据库?如果不检查数据库中生成的数据库。一般来说,您可以考虑为LocationPic设置LocationId,而不是设置Location。 -
@TaoZhou 模型非常简单,具有明确的 ID 和 ICollection。我可以发布它们,但它现在可以工作了,下面我的答案中的详细信息。本地和 Azure 上的不同数据库。此外,LocationPic.LocationID 是在参数中的模型绑定中设置的,它来自我的表单数据。所以我同时设置了 LocationID 和 Location。感谢您的宝贵时间。
标签: asp.net-mvc azure asp.net-core entity-framework-core asp.net-core-2.1