执行迁移后出错(.Net 后端)

Error after performing migrations (.Net backend)

我的后端是在 .NET 中构建的,通过在解决方案中包含 table,我得到了以下错误:

Cannot create more than one clustered index on table 'dbo.Favorit'. Drop the existing clustered index 'PK_dbo.Favorit' before creating another.

此代码是在 Add-Migration CreateFavoritupdate-database 命令后生成的:

namespace appService.Migrations
{
    using System;
    using System.Collections.Generic;
    using System.Data.Entity.Infrastructure.Annotations;
    using System.Data.Entity.Migrations;

    public partial class CreateFavorit : DbMigration
    {
        public override void Up()
        {
            CreateTable(
                "dbo.Favorit",
                c => new
                    {
                        Id = c.String(nullable: false, maxLength: 128,
                            annotations: new Dictionary<string, AnnotationValues>
                            {
                                { 
                                    "ServiceTableColumn",
                                    new AnnotationValues(oldValue: null, newValue: "Id")
                                },
                            }),
                        Nome = c.String(),
                        Lat_dest = c.Double(nullable: false),
                        Lon_dest = c.Double(nullable: false),
                        Id_usuario = c.String(),
                        Endereco = c.String(),
                        MeioTransporte = c.String(),
                        Id_usuario_2 = c.String(),
                        Version = c.Binary(nullable: false, fixedLength: true, timestamp: true, storeType: "rowversion",
                            annotations: new Dictionary<string, AnnotationValues>
                            {
                                { 
                                    "ServiceTableColumn",
                                    new AnnotationValues(oldValue: null, newValue: "Version")
                                },
                            }),
                        CreatedAt = c.DateTimeOffset(nullable: false, precision: 7,
                            annotations: new Dictionary<string, AnnotationValues>
                            {
                                { 
                                    "ServiceTableColumn",
                                    new AnnotationValues(oldValue: null, newValue: "CreatedAt")
                                },
                            }),
                        UpdatedAt = c.DateTimeOffset(precision: 7,
                            annotations: new Dictionary<string, AnnotationValues>
                            {
                                { 
                                    "ServiceTableColumn",
                                    new AnnotationValues(oldValue: null, newValue: "UpdatedAt")
                                },
                            }),
                        Deleted = c.Boolean(nullable: false,
                            annotations: new Dictionary<string, AnnotationValues>
                            {
                                { 
                                    "ServiceTableColumn",
                                    new AnnotationValues(oldValue: null, newValue: "Deleted")
                                },
                            }),
                    })
                .PrimaryKey(t => t.Id)
                .Index(t => t.CreatedAt, clustered: true);

        }

        public override void Down()
        {
            DropIndex("dbo.Favorit", new[] { "CreatedAt" });
            DropTable("dbo.Favorit",
                removedColumnAnnotations: new Dictionary<string, IDictionary<string, object>>
                {
                    {
                        "CreatedAt",
                        new Dictionary<string, object>
                        {
                            { "ServiceTableColumn", "CreatedAt" },
                        }
                    },
                    {
                        "Deleted",
                        new Dictionary<string, object>
                        {
                            { "ServiceTableColumn", "Deleted" },
                        }
                    },
                    {
                        "Id",
                        new Dictionary<string, object>
                        {
                            { "ServiceTableColumn", "Id" },
                        }
                    },
                    {
                        "UpdatedAt",
                        new Dictionary<string, object>
                        {
                            { "ServiceTableColumn", "UpdatedAt" },
                        }
                    },
                    {
                        "Version",
                        new Dictionary<string, object>
                        {
                            { "ServiceTableColumn", "Version" },
                        }
                    },
                });
        }
    }
}

服务器microsoft-azure,数据库SQLServer。 如何解决这个问题?或者,这个错误是什么? 谢谢。

编辑:

型号Class:

namespace appService.DataObjects
{
    public class Favorit : EntityData
    {
        public string Nome { get; set; }
        public double Lat_dest { get; set; }
        public double Lon_dest { get; set; }
        public string Id_usuario { get; set; }
        public string Endereco { get; set; }
        public string MeioTransporte { get; set; }
        public string Id_usuario_2 { get; set; }

    }
}

我们可以在Configuration.cs文件中包含如下代码来解决。

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

错误消息是由于Entity framework没有用于创建非主键的聚簇索引的注解。移动 SDK 手动创建正确的 SQL 语句以将 CreateAt 设置为 non-primary 键聚簇索引。更多详细信息请参考另一篇.

Generally, this error message is caused by not running the Mobile Apps/Mobile Services DB generator. Entity Framework does not have an annotation for creating a clustered index that is not a primary key, so the mobile server SDK manually creates the right SQL statements to set CreatedAt as a non-primary key clustered index.

我做了一个测试,它有效correctly.The以下是我的详细步骤:

1.Download 来自 Azure 门户的移动项目

2.Add项目中的新模型

3.Add 属性 在 XXXContext.cs 文件中

4.Add SetSqlGenerator("System.Data.SqlClient", new EntityTableSqlGenerator()) 在 Configuration.cs 文件中

5.Run enable-migrations -forceadd-migration tomtest-somechangeupdate-database 在程序包管理器控制台中。

6。检查 table 是否正确创建