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

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

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

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。 怎么解决这个问题呢?或者,这个错误是什么? 谢谢。

编辑:

模型类:

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 设置为非主键聚集索引。更多详细信息请引用另一篇SO thread .

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.

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

1.从azure门户下载移动项目

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

关于c# - 执行迁移后出错(.Net 后端),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41962943/

相关文章:

c# - 在修改与 NHibernate 的多对多关系时防止删除/插入

c# - 单场比赛中有多个 Regexoptions?

azure - SQLAzure 数据库服务器 - 命名管道提供程序,错误 : 40 - the network path was not found

Azure:跨多个资源组共享资源

c# - 在全局命名空间中找不到 "Telerik"

c# - 如果我有静态变量,WebServices 是无状态的吗?

.net - 有什么方法可以控制 ASMX Web 服务中 float/double 的格式吗?

azure - 为什么要更改显示数百万延迟的 Feed 延迟估计器?

c# - 在哪里可以找到新的 Span<T>?

c# - 打印带有字母的倒三角形