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

时间:2017-01-31 16:47:14

标签: c# .net azure azure-mobile-services backend

我的后端是用.NET构建的,通过在解决方案中包含一个表,我收到了以下错误:

  

无法在表'dbo.Favorit'上创建多个聚簇索引。   在创建之前删除现有的聚簇索引“PK_dbo.Favorit”   另一个。

此代码是在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。 怎么解决这个?或者,这个错误是什么? 谢谢。

编辑:

模特课程:

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; }

    }
}

1 个答案:

答案 0 :(得分:3)

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

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

错误消息是由Entity框架没有用于创建不是主键的聚簇索引的注释引起的。移动SDK手动创建正确的SQL语句,以将CreateAt设置为非主键聚簇索引。更多详细信息请参阅另一个SO thread

  

通常,此错误消息是由未运行移动应用/移动服务数据库生成器引起的。实体框架没有用于创建非主键的聚簇索引的注释,因此移动服务器SDK手动创建正确的SQL语句以将CreatedAt设置为非主键聚簇索引。

我为它做了一个测试,它运行正常。以下是我的详细步骤:

1.从azure portal下载移动项目

enter image description here

2.在项目中添加新模型

enter image description here

3.在XXXContext.cs文件中添加属性

enter image description here

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

enter image description here

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

enter image description here

6。检查表格是否正确创建

enter image description here