在Azure移动服务控制器中添加相关的数据库条目 [英] Add related database entry in Azure Mobile Services controller

查看:52
本文介绍了在Azure移动服务控制器中添加相关的数据库条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Azure移动服务中,我有一个控制器类UserController : TableController<User>,它是一个get方法:

In my Azure Mobile Service I have a controller class UserController : TableController<User> and in it is a get method:

// GET tables/User/48D68C86-6EA6-4C25-AA33-223FC9A27959
public SingleResult<User> GetUser(string id)
{
    return Lookup(id);
}

我想记录每次访问用户的时间,因此我向模型添加了一个简单的类型:

I want to record each time a user is accessed and so I add a simple type to the model:

public class UserVisit : Microsoft.WindowsAzure.Mobile.Service.EntityData
{
    public string VisitingUser { get; set; }
    public DateTime TimeOfVisit { get; set; }
}

并将属性public DbSet<UserVisit> UserVisits { get; set; }包含在我的VCollectAPIContext : DbContext类中(并通过代码优先的迁移来更新数据库).

and include the property public DbSet<UserVisit> UserVisits { get; set; } in my VCollectAPIContext : DbContext class (and update the database with a code-first migration).

要在查询用户ID时将UserVisit添加到数据库,我将控制器方法更改为

To add a UserVisit to the database when a user id is queried I change my controller method to

// GET tables/User/48D68C86-6EA6-4C25-AA33-223FC9A27959
public async Task<SingleResult<User>> GetUser(string id)
{
    var userVisit = new UserVisit { VisitingUser = id, TimeOfVisit = DateTime.UtcNow };
    context.UserVisits.Add(userVisit);
    await context.SaveChangesAsync();
    return Lookup(id);
}

但是SaveChangesAsync失败,并显示System.Data.Entity.Validation.DbEntityValidationException.在异常的EntityValidationErrors属性中浏览,我发现问题是"Id字段是必需的."

But the SaveChangesAsync fails with a System.Data.Entity.Validation.DbEntityValidationException. Digging around in the exception's EntityValidationErrors property I find that the problem is "The Id field is required."

有点奇怪. Id字段是基类Microsoft.WindowsAzure.Mobile.Service.EntityData中的属性之一,我希望该属性在插入时会自动添加.没关系,我可以这样添加它以及其他一些基类的属性:

That's a little odd. The Id field is one of the properties in the base-class Microsoft.WindowsAzure.Mobile.Service.EntityData that I would expect to be added automatically on insert. No matter, I can add it and several of the other base-class's properties thus:

// GET tables/User/48D68C86-6EA6-4C25-AA33-223FC9A27959
public async Task<SingleResult<User>> GetUser(string id)
{
    var userVisit = new UserVisit { Id = Guid.NewGuid().ToString(), Deleted = false, VisitingUser = id, TimeOfVisit = DateTime.UtcNow, CreatedAt = DateTimeOffset.Now };
    context.UserVisits.Add(userVisit);
    await context.SaveChangesAsync();
    return Lookup(id);
}

这次我得到一个System.Data.Entity.Infrastructure.DbUpdateException,因为我们无法将值NULL插入到列'CreatedAt'中".在对Add的调用中,它不为null.因此CreatedAt已在我的代码之外的某处设置为null,然后插入失败!

This time I get a System.Data.Entity.Infrastructure.DbUpdateException because we "Cannot insert the value NULL into column 'CreatedAt'". It was not null in the call to Add. So CreatedAt has been set to null somewhere outside my code and then the insert fails as a result!

我还尝试在控制器的初始化程序中设置EntityDomainManager<UserVisit> userVisitDomainManager;实例变量,然后将控制器的get方法重写为

I also tried setting up an EntityDomainManager<UserVisit> userVisitDomainManager; instance variable in the controller's initializer, and then rewriting my controller get method as

// GET tables/User/48D68C86-6EA6-4C25-AA33-223FC9A27959
public async Task<SingleResult<User>> GetUser(string id)
{
    var userVisit = new UserVisit { VisitingUser = id, TimeOfVisit = DateTime.UtcNow };
    await userVisitDomainManager.InsertAsync(userVisit);
    return Lookup(id);
}

失败并显示相同的消息,无法将值NULL插入列'CreatedAt'"

That fails with the same message, "Cannot insert the value NULL into column 'CreatedAt'"

我应该如何执行在控制器方法中插入相关数据项的看似简单的任务?

How should I perform the seemingly simple task of inserting a related data item within my controller method?

推荐答案

该解决方案可能类似于此答案.我猜测您的迁移未使用Mobile Services SqlGenerator,因此某些自定义SQL设置未得到应用.那是什么意思:

The solution is likely similar to this answer. I'm guessing that your migration is not using the Mobile Services SqlGenerator so some of the custom SQL settings aren't getting applied. What that means is that:

  • Id未获得NEWID()的默认值-这说明了您的"Id字段为必填"错误.
  • CreatedAt没有获得默认值SYSUTCDATETIME(),它与EntityData.CreatedAt的[DatabaseGenerated]属性结合使用,可以解释"NULL CreatedAt"错误.

尝试根据上面的链接更新您的迁移,然后查看是否适合您.

Try updating your migration according to the link above and see if that works for you.

这篇关于在Azure移动服务控制器中添加相关的数据库条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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