Error While Enabling Code-First Migrations On Mobile Services Database

前端 未结 5 1667
你的背包
你的背包 2021-02-07 17:12

I have an Azure Mobile Services project (C# backend) that I recently created and attached to an Azure SQL database. I have been trying to enable Code-First Migrations on that ba

相关标签:
5条回答
  • 2021-02-07 17:50

    I was fighting with this problem today for a few hours. Until I found this link:

    How to make data model changes to a .NET backend mobile service

    If you follow the instructions there, it will definitely work. The main thing is, that the migration will take place, when you hit F5 during a local debugging session.

    0 讨论(0)
  • 2021-02-07 17:50

    Several of the answers about deriving from a custom entity class will work, but they are not the ideal solution. As the EF team (and others) have mentioned, you need to simply add this line to your Context Configuration constructor.

     SetSqlGenerator("System.Data.SqlClient", new EntityTableSqlGenerator());
    

    This will remove your errors when creating migrations and allow you to update the database via powershell command.

    0 讨论(0)
  • 2021-02-07 17:52

    I just had the same issue.

    It is caused by the definition of EntityData that is our base class:

    public class OrgTest : EntityData
    {
        public string Prop1 { get; set; }
    }
    

    I replaced EntityData with my own implementation "CustomEntity" where I removed the attribute [Index(IsClustered = true)] on the CreatedAt column:

    public abstract class CustomEntity : ITableData
    {
       // protected CustomEntity();
    
        [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
        [TableColumn(TableColumnType.CreatedAt)]
        public DateTimeOffset? CreatedAt { get; set; }
        [TableColumn(TableColumnType.Deleted)]
        public bool Deleted { get; set; }
        [Key]   
        [TableColumn(TableColumnType.Id)]
        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; }
    } 
    

    and now I inherit from this one:

    public class OrgTest : CustomEntity   // EntityData
    {
        public string Prop1 { get; set; }
    }
    

    Probably I will have troubles further on, but for the time being I can create my model! Hope you can also start like this!

    0 讨论(0)
  • 2021-02-07 18:01

    If you are getting this error on update-database after creating the migration class, Then have a look @ your migration class. The migration class will take primary is clustered index. So remove that from the up() method.

    .PrimaryKey(t => t.Id, clustered: false) .Index(t => t.CreatedAt, clustered: true);

    If you using it on azure mobile service, do not call 'update-database' manually. refer http://azure.microsoft.com/en-in/documentation/articles/mobile-services-dotnet-backend-how-to-use-code-first-migrations/

    0 讨论(0)
  • 2021-02-07 18:03

    See this article:

    avoid nightmares using ef first migration in azure mobile services

    EntityData define a cluster indext to CreateAt and Id is by default a cluster index, this way it provide an error and you should define only one.

    0 讨论(0)
提交回复
热议问题