使用TableController和AzureMobileApps发布新实体时出现错误500 [英] Error 500 on Post new entity with TableController and AzureMobileApps

查看:62
本文介绍了使用TableController和AzureMobileApps发布新实体时出现错误500的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在AzureMobileApps上使用TableController遇到问题.我已经在Visual Studio中使用脚手架创建了一个新的Azure移动应用TableController.在帖子中,我修改了结果代码,在dbContext上添加了一个附件,以避免在子表上插入期间创建引用的项目.这就是结果代码:

I'm experiencing a problem using TableController on AzureMobileApps. I've created a new Azure Mobile App TableController in visual studio using scaffolding. In post I modified the resulting code adding an attach on the dbContext to avoid creating referenced item during insert on the child table. That's the resulting code:

 public async Task<IHttpActionResult> PostLocation(Location item)
    {
        _context.Accounts.Attach(item.LocationAccount);
        _context.Categories.Attach(item.PrimaryCategory);

        Location current = await InsertAsync(item);
        return CreatedAtRoute("Tables", new { id = current.Id }, current);
    }

问题是,即使我已正确插入实体,每次调用post方法时,在"CreatedAtRoute"事件上都会收到500个内部服务器错误.

The problem is that each time I invoke the post method I get a 500 internal server error on the "CreatedAtRoute" event even if the entity has been correctly inserted.

有什么问题的想法吗?!

Any idea on what is the problem ?!

更新:实体模型

public class Account : EntityData
{
    public Account()
    {
        this.Locations = new HashSet<Location>();
    }

    [Required]
    public string Username { get; set; }

    [Required]
    public string FirstName { get; set; }

    [Required]
    public string LastName { get; set; }

    public virtual ICollection<Location> Locations { get; private set; }
}

public class Location : EntityData
{
    [Required]
    public Account LocationAccount { get; set; }

    ........
}   

谢谢大家.

推荐答案

AFAIK,在添加与Attach相关的代码之前,如果LocationAccountPrimaryCategory是新项目,则将自动创建它们.如果数据库表中存在它们中的任何一个(LocationAccountPrimaryCategory),那么您将检索409状态代码.

AFAIK, before you adding Attach related code, if the LocationAccount,PrimaryCategory are the new items, then they would be created automatically. If any of them (LocationAccount,PrimaryCategory) exists in the database table, then you would retrieve the 409 status code.

根据我的测试,添加Attach相关代码后,如果LocationAccountPrimaryCategory存在,则可以成功创建新的Location项目.但是,如果其中任何一个都不存在,那么您可能会得到如下错误:

Based on my test, after added Attach related code, if LocationAccount and PrimaryCategory exist, then you could create new Location item successfully. But if any of them does not exist, then you may get the error as follows:

根据我的理解,您需要检查Location的导航属性是否存在.对于现有的导航属性项目,可以使用DbSet.Attach方法来阻止附加实体的插入,而对于新的导航属性项目,则需要使用DbSet.Add或不执行任何操作.

Per my understanding, you need to check if the navigation properties of Location exist. For the existing navigation property item you could use the DbSet.Attach method to stop the attached entity from inserting, while the new navigation property item, you need to use DbSet.Add or do nothing.

此外,您可以在Startup.MobileApp.cs文件的ConfigureMobileApp方法中添加以下代码,以包括错误详细信息并返回到客户端.

Moreover, you could add the following code in the ConfigureMobileApp method of your Startup.MobileApp.cs file for including error details and return to your client side.

config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

更新:

默认情况下,将插入引用的实体(LocationAccountPrimaryCategory),如果存在任何实体,则您将收到如下所示的409:

By default, the referenced Entities (LocationAccount,PrimaryCategory) would be inserted, if any entity exists, then you would receive the 409 as follows:

添加_context.Accounts.Attach(item.LocationAccount);后,如果引用实体(LocationAccountPrimaryCategory)不存在,则可以创建与现有引用实体(LocationAccountPrimaryCategory)有关系的Location实体存在,您将收到以下错误:

After added _context.Accounts.Attach(item.LocationAccount);, you could create the Location entity which has relationships with the existing referenced Entities (LocationAccount,PrimaryCategory), if the referenced Entities (LocationAccount,PrimaryCategory) do not exist, you would receive the following error:

对于您的方案,您发布现有的引用实体(LocationAccountPrimaryCategory)作为位置.即使可以根据您的实体模型成功创建位置项目,您也可能会遇到500错误,如下所示:

For your scenario, you post the existing referenced Entities (LocationAccount,PrimaryCategory) for location. Even though the location item could be successfully created, based on your entity models, you may encounter the 500 error as follows:

您可以将Account实体模型中的Locations属性标记为JsonIgnore.或者,您需要修改实体模型以在处理序列化时忽略参考循环.

You may mark the Locations property in Account entity model as JsonIgnore. Or you need to modify your entity model to ignore the reference Loops when handling serialization.

此外,您可以利用以下代码段代替CreatedAtRoute.

Moreover, you could leverage the following code snippet instead of CreatedAtRoute.

return Json(current, new JsonSerializerSettings()
{
    ReferenceLoopHandling = ReferenceLoopHandling.Ignore
});

我还尝试如下在Startup.MobileApp.cs下设置SerializerSettings的全局设置,但是它不能按预期工作.

I also tried to set the global setting for SerializerSettings under Startup.MobileApp.cs as follows, but it does not work as expected.

config.Formatters.JsonFormatter.SerializerSettings.Re‌​ferenceLoopHandling = ReferenceLoopHandling.Ignore;

此外,您可以遵循 Web API中的循环引用处理了解更详细的方法.

Additionally, you could follow Loop Reference handling in Web API for more detailed approaches.

这篇关于使用TableController和AzureMobileApps发布新实体时出现错误500的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆