【问题标题】:Error after performing migrations (.Net backend)执行迁移后出错(.Net 后端)
【发布时间】:2017-01-31 16:47:14
【问题描述】:

我的后端是在 .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; }

    }
}

【问题讨论】:

  • 嗯,错误表明您在表中只能有一个聚集索引。我认为它正在为Id 创建一个,因为它是主键,然后为CreatedAt 创建另一个,正如迁移中定义的那样。我不确定它为什么要创建它。你能分享你的模型课吗?
  • @juunas 当然,我可以分享。
  • @juunas 在那里

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


【解决方案1】:

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

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

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

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

我做了一个测试,它可以正常工作。以下是我的详细步骤:

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

2.在项目中添加新模型

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

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

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

6 .检查表是否正确创建

【讨论】:

  • Sun 我需要删除这个创建的表吗?因为它出现在迁移中
猜你喜欢
  • 2017-08-07
  • 2022-01-21
  • 2011-03-13
  • 1970-01-01
  • 1970-01-01
  • 2015-10-20
  • 1970-01-01
  • 2020-10-24
  • 1970-01-01
相关资源
最近更新 更多