For an Azure mobile app to be able to transfer DTO's from their mobile SDK to their backend SDK, they need to inherit the EntityData class that has some necessary properties, for some features like offline sync. When you need to use an existing database in your mobile app you need to make some changes to it, adding the necessary columns that Azure require, like on this tutorial in this link: https://azure.microsoft.com/en-us/documentation/articles/mobile-services-dotnet-backend-use-existing-sql-database/
Im following it, but its not working. I have an initializer that runs when I change my model, and create some Identity users. Those users have a relation with a entity that I added the EntityData properties like the guide said to. But everytime the initializer kicks in I get a NULL exception to the CreatedAt field saying it cant be inserted with null value. Even it having the proper annotations to mark it as database generated. I tryed to do "CreatedAt = DateTimeOffset.Now();" in the initializer but didn't work either.
Here's the initializer code:
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 = "[email protected]",
UserName = "[email protected]",
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");
}
The 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; }
}