1

我正在尝试首先将自动迁移与实体框架和代码一起使用。我向我的班级添加了新属性(团队也是自定义班级):

public virtual Team SecondTeam { get; set; }

我在更新数据库时遇到问题。在我编写命令后,我在控制台中得到了这个:

SQL Server Compact 不支持直接重命名列。要重命名 SQL Server Compact 中的列,您需要重新创建它。

通常正在更新工作(我尝试添加简单的 int 道具并且它只是更新)但如果那是虚拟的我有问题。谁能帮我这个?谢谢

编辑:之后的所有行Update-Database -Verbose

Using NuGet project 'Project'.  
Using StartUp project 'Project'.  
Target database is: '|DataDirectory|database.sdf' (DataSource: |DataDirectory|database.sdf, Provider: System.Data.SqlServerCe.4.0, Origin: Configuration). 
No pending explicit migrations. 
Applying automatic migration: 201206130828142_AutomaticMigration. 
Direct column renaming is not supported by SQL Server Compact. To rename a column in SQL Server Compact, you will need to recreate it.

Edit2:我要更改数据库的播放器类:

public class Player
    {
        [Key]
        public int PlayerID { get; set; }

        [Required]
        public string Name { get; set; }
        [Required]
        public string Surname { get; set; }
        public string Nickname { get; set; }
        public DateTime Birth { get; set; }

        public string PhotoUrl { get; set; }
        public string Post { get; set; }

        public int TeamID { get; set; }
        public virtual Team Team { get; set; }

        public int SecondTeamID { get; set; }
        public virtual Team SecondTeam { get; set; }

        public int UserId { get; set; }
        public virtual User User { get; set; }
    }

团队班:

public class Team
    {
        [Key]
        public int TeamID { get; set; }
        public string Name { get; set; }

        public string League { get; set; }
        public string Trainings { get; set; }

        public string PhotoUrl { get; set; }

        public int CoachID { get; set; }
        public int AassistantID { get; set; }
        public int ManagerID { get; set; }

        public virtual ICollection<Player> Players { get; set; }

        public bool showPosts { get; set; }
    }

这是 EF 生成的 SQL:

CREATE TABLE "Players" (
    "PlayerID" int not null identity,
    "Name" nvarchar(4000) not null,
    "Surname" nvarchar(4000) not null,
    "Nickname" nvarchar(4000) null,
    "Birth" datetime not null,
    "PhotoUrl" nvarchar(4000) null,
    "Post" nvarchar(4000) null,
    "TeamID" int not null,
    "SecondTeamID" int not null,
    "UserId" int not null,
    "Team_TeamID" int null,
    "Team_TeamID1" int null,
    "SecondTeam_TeamID" int null,
    PRIMARY KEY ("PlayerID")
);

ALTER TABLE "Players" ADD CONSTRAINT "Team_Players" FOREIGN KEY ("Team_TeamID") REFERENCES "Teams"("TeamID");
ALTER TABLE "Players" ADD CONSTRAINT "Player_Team" FOREIGN KEY ("Team_TeamID1") REFERENCES "Teams"("TeamID");
ALTER TABLE "Players" ADD CONSTRAINT "Player_SecondTeam" FOREIGN KEY ("SecondTeam_TeamID") REFERENCES "Teams"("TeamID");
ALTER TABLE "Players" ADD CONSTRAINT "Player_User" FOREIGN KEY ("UserId") REFERENCES "Users"("UserID") ON DELETE CASCADE;
4

2 回答 2

2

我知道这是一个老问题,但我认为该解决方案以后会很有用。

除非您确定代码仅限于调试环境,否则请勿使用该RecreateDatabaseIfModelChanges功能。如果它在生产代码中出现(并且您可能并不总是在发布之前查看 DAL)意外的数据丢失很容易成为一个问题。

如果您正确使用迁移,您将看到 EF 为您生成如下代码:

namespace Migrations_CE
{
    using System;
    using System.Data.Entity.Migrations;

    public partial class TargetInventoryChange : DbMigration
    {
        public override void Up()
        {
            RenameColumn(table: "dbo.TargetInventoryItems", name: "MatchedPrinter_ID", newName: "MatchedPrinterID");
            AddColumn("dbo.TargetInventoryItems", "CustomerID", c => c.String(maxLength: 40));
            AlterColumn("dbo.TargetInventoryItems", "Name", c => c.String(maxLength: 100));
        }

        public override void Down()
        {
            AlterColumn("dbo.TargetInventoryItems", "Name", c => c.String(maxLength: 4000));
            DropColumn("dbo.TargetInventoryItems", "CustomerID");
            RenameColumn(table: "dbo.TargetInventoryItems", name: "MatchedPrinterID", newName: "MatchedPrinter_ID");
        }
    }
}

在这个特定的项目中,我保留了两组迁移,一组用于 SQL CE 数据库,一组用于 SQL Server。在为 Compact 版本进行迁移时,我偶然发现了您的问题。

我解决了它只是按照错误消息所说的那样做。请注意,这将导致您正在重命名的列上的数据丢失。当心。

这个例子解决了外键情况,这是(我相信)最糟糕的情况之一,因为 EF 自己生成列、FK 约束和自动索引。

public override void Up()
{
    //RenameColumn(table: "dbo.TargetInventoryItems", name: "MatchedPrinter_ID", newName: "MatchedPrinterID");
    DropIndex("dbo.TargetInventoryItems", "IX_MatchedPrinter_ID");
    DropForeignKey("dbo.TargetInventoryItems", "FK_dbo.TargetInventoryItems_dbo.CustPrinters_MatchedPrinter_ID");
    DropColumn("dbo.TargetInventoryItems", "MatchedPrinter_ID");
    AddColumn("dbo.TargetInventoryItems", "MatchedPrinterID", c => c.Int());
    AddForeignKey("dbo.TargetInventoryItems", "MatchedPrinterID", "dbo.CustPrinters", "ID");
    CreateIndex("dbo.TargetInventoryItems", "MatchedPrinterID", unique: false);
}

如果您不知道索引的名称,可以使用 SQL CE 工具箱 ( http://sqlcetoolbox.codeplex.com/ ) 查看特定表。您将看到一个索引列表,您可以在其中找到要删除的名称。

对于 FK 约束,您必须查询INFORMATION_SCHEMA,例如再次使用 SQL CE Toolbox,查询如下

SELECT * FROM INFORMATION_SCHEMA.REFERENTIAL_CONSTRAINTS

根据需要设置一些WHERE条件。我希望这将在适当的 SQL 生成器上得到修复,但与此同时,这实际上解决了问题 - 进行小的更改。

于 2013-10-15T13:08:40.947 回答
0

我认为,如果您遵循此示例Steven Sanderson - MvcScaffolding:一对多关系并在开发期间使用该功能在启动时根据Scott Guthrie - Code-First Development with Entity Framework 4 blog 重新创建数据库。(搜索段落Taking advantage of SQL CE 4The RecreateDatabaseIfModelChanges Feature

于 2012-06-22T07:09:39.887 回答