Azure Mobile使用现有数据库

时间:2016-02-12 22:21:33

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

要使Azure移动应用能够将DTO从其移动SDK转移到其后端SDK,他们需要继承具有某些必要属性的EntityData类,以实现某些功能,例如离线同步。 当您需要在移动应用程序中使用现有数据库时,需要对其进行一些更改,添加Azure所需的必要列,例如此链接中的本教程:https://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-use-existing-sql-database/

我跟着它,但它不起作用。 我有一个初始化程序,在我更改模型时运行,并创建一些Identity用户。那些用户与我添加了EntityData属性的实体有关系,就像指南所说的那样。但每次初始化程序启动时,我都会获得CreatedAt字段的NULL异常,说它不能插入null值。即使它有正确的注释标记为数据库生成。 我试着去做#34; CreatedAt = DateTimeOffset.Now();"在初始化程序中,但也没有工作。

这是初始化代码:

public class EqcDbInitializer : DropCreateDatabaseIfModelChanges<ContextoEQC>
{
    protected override void Seed(ContextoEQC context)
    {

     if (!contexto.Roles.Any())
         .
         .
         .
         .
        var usuarioRestaurante = new Usuario
        {
            DataCadastro = new DateTime(2014, 10, 12),
            Email = "gerente@restaurante.com.br",
            UserName = "gerente@restaurante.com.br",
            Endereco = new Endereco {
              Id = Guid.NewGuid().ToString(); //Here dosent matter if I fill
              CreatedAt = DateTimeOffset.Now; //those two or not, same exepction always.
              Bairro = bairro;
              Cep = cep,
              Cidade = cidade,
              Estado = estado,
              Logradouro = logradouro,
              Numero = numero,
              Pais = pais,
           }
        };
        userManager.Create(usuarioRestaurante, "12345678");
        userManager.AddToRole(usuarioRestaurante.Id, "Restaurante");
}

Enyity Code:

public class Endereco
{

    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    [Index]
    [TableColumn(TableColumnType.CreatedAt)]
    public DateTimeOffset? CreatedAt { get; set; }

    [TableColumn(TableColumnType.Deleted)]
    public bool Deleted { get; set; }

    [Index]
    [TableColumn(TableColumnType.Id)]
    [MaxLength(36)]
    public string Id { get; set; }

    [DatabaseGenerated(DatabaseGeneratedOption.Computed)]
    [TableColumn(TableColumnType.UpdatedAt)]
    public DateTimeOffset? UpdatedAt { get; set; }

    [TableColumn(TableColumnType.Version)]
    [Timestamp]
    public byte[] Version { get; set; }

    [Key]
    public int UsuarioId { get; set; }
    [Required]
    public string Pais { get; set; }
    [Required]
    public string Estado { get; set; }
    [Required]
    public string Cidade { get; set; }
    [Required]
    public string Bairro { get; set; }
    [Required]
    public string Logradouro { get; set; }
    [Required]
    public string Numero { get; set; }
    [Required]
    public string Cep { get; set; }

    //Navegação
    public virtual Usuario Usuario { get; set; }

}

1 个答案:

答案 0 :(得分:0)

问题是您的实体类上有一些与Mobile Apps SDK期望的注释冲突的注释。您应该删除注释并仅保留TableColumn

[TableColumn(TableColumnType.CreatedAt)]
public DateTimeOffset? CreatedAt { get; set; }

[TableColumn(TableColumnType.Deleted)]
public bool Deleted { get; set; }

[TableColumn(TableColumnType.Id)]
[MaxLength(36)]
public string Id { get; set; }

[TableColumn(TableColumnType.UpdatedAt)]
public DateTimeOffset? UpdatedAt { get; set; }

[TableColumn(TableColumnType.Version)]
public byte[] Version { get; set; }

尝试更改DTO,然后重新运行数据库初始化程序。如果您有自定义初始值设定项,请确保在迁移配置中设置移动应用程序(请参阅How to make data model changes to a .NET backend mobile service):

public Configuration()
{
    AutomaticMigrationsEnabled = false;
    SetSqlGenerator("System.Data.SqlClient", new EntityTableSqlGenerator());
}