AzureMobileClient DatabaseFirst手动创建数据表失败?

时间:2017-11-08 15:45:59

标签: azure azure-sql-database azure-mobile-services

我尝试使用Database First模型在我的XamarinForm中使用AzureMobileClient。目前我不使用离线同步。 所以我使用这个脚本在我的AZURE SQL DB中创建表:

CREATE TABLE [dbo].[TodoItems] (
-- This must be a string suitable for a GUID
[Id]            NVARCHAR (128)     NOT NULL,

-- These are the system properties
[Version]       ROWVERSION         NOT NULL,
[CreatedAt]     DATETIMEOFFSET (7) NOT NULL,
[UpdatedAt]     DATETIMEOFFSET (7) NULL,
[Deleted]       BIT                NOT NULL,

-- These are the properties of our DTO not included in EntityFramework
[Text]          NVARCHAR (MAX)     NULL,
[Complete]      BIT                NOT NULL,
);

CREATE CLUSTERED INDEX [IX_CreatedAt]
ON [dbo].TodoItems([CreatedAt] ASC);

ALTER TABLE [dbo].[TodoItems]
ADD CONSTRAINT [PK_dbo.TodoItems] PRIMARY KEY NONCLUSTERED ([Id] ASC);

CREATE TRIGGER [TR_dbo_TodoItems_InsertUpdateDelete] ON [dbo].[TodoItems]
AFTER INSERT, UPDATE, DELETE AS
BEGIN
    UPDATE [dbo].[TodoItems]
        SET [dbo].[TodoItems].[UpdatedAt] = CONVERT(DATETIMEOFFSET, 
              SYSUTCDATETIME())
        FROM INSERTED WHERE inserted.[Id] = [dbo].[TodoItems].[Id]
END;

基于Azure提供的示例TodoItem。我可以做一个没有任何错误的GetAllItems(表现在是空的)。但是当我尝试插入一个项目时,我的Azure后端出现了这个错误:

{[Message, The operation failed with the following error: 'Cannot insert the value NULL into column 'CreatedAt', table 'TechCenterCentaur.dbo.TodoItems'; column does not allow nulls. INSERT fails.The statement has been terminated.'.]}

通常Azure应该自动处理吗?

我只是在我的XF代码中执行此操作:

TodoItem cl = new TodoItem();
cl.Name = "Test";

await _todoTable.InsertAsync(cl);

使用仅包含Test的TodoItem对后端进行调用,所有其他字段均为null。异常发生在后端:

public async Task<IHttpActionResult> PostTodoItem(TodoItem item)
{
  try
  {
     TodoItem current = await InsertAsync(item);  //crash here
     return CreatedAtRoute("Tables", new { id = current.Id }, current);
  }
  catch (System.Exception e)
  {

     throw;
  }
}

有什么建议吗?

1 个答案:

答案 0 :(得分:0)

好的,我找到了解决方案。问题出在我的SQL表中。我错过了2个ALTER TABLE,用于ID和CreatedDate的新GUID。

这是我的新剧本:

USE [TechCenterCentaur]
GO

/****** Object:  Table [dbo].[TodoItems]    Script Date: 2017-11-08 11:09:14 
  ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

CREATE TABLE [dbo].[TodoItems](
  [Id] [nvarchar](128) NOT NULL,
  [Text] [nvarchar](max) NULL,
  [Complete] [bit] NOT NULL,
  [Version] [timestamp] NOT NULL,
  [CreatedAt] [datetimeoffset](7) NOT NULL,
  [UpdatedAt] [datetimeoffset](7) NULL,
  [Deleted] [bit] NOT NULL,
CONSTRAINT [PK_dbo.TodoItems] PRIMARY KEY NONCLUSTERED 
(
  [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, 
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON)
)

GO

ALTER TABLE [dbo].[TodoItems] ADD  DEFAULT (newid()) FOR [Id]
GO

ALTER TABLE [dbo].[TodoItems] ADD  DEFAULT (sysutcdatetime()) FOR 
       [CreatedAt]
GO

CREATE TRIGGER [TR_dbo_TodoItems_InsertUpdateDelete] ON [dbo].[TodoItems]
AFTER INSERT, UPDATE, DELETE AS
BEGIN
    UPDATE [dbo].[TodoItems]
        SET [dbo].[TodoItems].[UpdatedAt] = CONVERT(DATETIMEOFFSET, 
           SYSUTCDATETIME())
        FROM INSERTED WHERE inserted.[Id] = [dbo].[TodoItems].[Id]
END;
GO