代码迁移意外尝试重命名表 [英] Code migration unexpectedly tries to rename table

查看:87
本文介绍了代码迁移意外尝试重命名表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要按照的建议实施变更日志 Dev Express XAF T474899

I want to implement a change log as advised in Dev Express XAF T474899

我正在使用XAF新解决方案向导生成的安全系统

I am using the security system generated by the XAF new solution wizard

我已经定义了一些业务对象来存储更改日志信息.

I have defined some business objects to store the change log information.

这些对象之一存储指向用户的链接

One of these objects stores a link to the user

公共虚拟用户用户{放; }

public virtual User User { get; set; }

在生成代码迁移时,我很惊讶地看到Up()方法添加了以下内容

On generating the code migration I am surprised to see the Up() method add the following

RenameTable(name: "dbo.UserRoles", newName: "RoleUsers");
DropPrimaryKey("dbo.RoleUsers");
AddPrimaryKey("dbo.RoleUsers", new[] { "Role_ID", "User_ID" });

我又一次在Up()中找到了以下内容

On another occasion I found the following in an Up()

RenameTable(name: "dbo.EventResources", newName: "ResourceEvents");
// lots of other stuff
 DropPrimaryKey("dbo.ResourceEvents");
 AddPrimaryKey("dbo.ResourceEvents", new[] { "Resource_Key", "Event_ID" });

在两种情况下,创建实体的代码都是Dev Express库.

On both occasions the code that creates the entities is a Dev Express libary.

我已将此问题交叉发布到 Dev Express支持

I have cross posted this question to Dev Express Support

Dev Express业务对象在DevExpress.Persistent.BaseImpl.EF中定义;

The Dev Express business objects are defined in DevExpress.Persistent.BaseImpl.EF;

我的DbContext上下文将它们称为

My DbContext context refers to them as

public DbSet<Role> Roles { get; set; }
public DbSet<User> Users { get; set; }

角色的元数据显示

用户的元数据显示

我自己的商务舱包含

    namespace SBD.JobTalk.Module.BusinessObjects
{
    [NavigationItem("Configuration")]
    [DisplayName("Staff")]
    [DefaultProperty("Summary")]
    [ImageName("BO_Employee")]
    [Table("Staff")]
    public class Staff : BasicBo
    {
        public Staff()
        {
            Person = new Person();
        }
        public virtual Person Person { get; set; }

        [StringLength(100, ErrorMessage = "The field cannot exceed 100 characters. ")]
        [scds.Index("IX_Staff_UserName", 1, IsUnique = true)]
        public string UserName { get; set; }
        [NotMapped]
        public string Summary => $"{Person.FirstName} {Person.LastName}";

        //public virtual User User { get; set; }
    }
}

public abstract class BasicBo : IXafEntityObject  
{
    [Browsable(false)]
    [Key]
    public virtual int Id { get; set; }
    public virtual void OnCreated()
    {

    }
      public virtual void OnSaving()
    {
    }

    public virtual void OnLoaded()
    {
    }
}

如果我取消注释代码以在Staff内部使用User属性,并生成迁移,则Up迁移为

If I un-comment the code to have the User property inside Staff, and generate a migration, the migration Up is

 public override void Up()
    {
        RenameTable(name: "dbo.UserRoles", newName: "RoleUsers");
        DropPrimaryKey("dbo.RoleUsers");
        AddColumn("dbo.Staff", "User_ID", c => c.Int());
        AddPrimaryKey("dbo.RoleUsers", new[] { "Role_ID", "User_ID" });
        CreateIndex("dbo.Staff", "User_ID");
        AddForeignKey("dbo.Staff", "User_ID", "dbo.Users", "ID");
    }

[更新] 有趣的是,有更多的Dev Express表比我最初想象的要多. 主键是身份.

[Update] Interestingly there are more Dev Express tables than I first thought. The primary keys are Identity.

我认为我使用的是在Dev Express添加允许/拒绝功能(V16.1)之前创建的标准身份验证

I think am using Standard Authentication created before Dev Express added the Allow/Deny ability (V16.1)

[更新] 当我使用上述设置创建一个新项目时,这里是DbContext.

[Update] When I create a new project with the above settings, here is the DbContext.

using System;
using System.Data;
using System.Linq;
using System.Data.Entity;
using System.Data.Common;
using System.Data.Entity.Core.Objects;
using System.Data.Entity.Infrastructure;
using System.ComponentModel;
using DevExpress.ExpressApp.EF.Updating;
using DevExpress.Persistent.BaseImpl.EF;
using DevExpress.Persistent.BaseImpl.EF.PermissionPolicy;

namespace XafApplication1.Module.BusinessObjects {
    public class XafApplication1DbContext : DbContext {
        public XafApplication1DbContext(String connectionString)
            : base(connectionString) {
        }
        public XafApplication1DbContext(DbConnection connection)
            : base(connection, false) {
        }
        public XafApplication1DbContext()
            : base("name=ConnectionString") {
        }
        public DbSet<ModuleInfo> ModulesInfo { get; set; }
        public DbSet<PermissionPolicyRole> Roles { get; set; }
        public DbSet<PermissionPolicyTypePermissionObject> TypePermissionObjects { get; set; }
        public DbSet<PermissionPolicyUser> Users { get; set; }
        public DbSet<ModelDifference> ModelDifferences { get; set; }
        public DbSet<ModelDifferenceAspect> ModelDifferenceAspects { get; set; }
    }
}

推荐答案

好的,我要刺一下:)您的Up()代码试图将表UserRoles重命名为RoleUsers.这意味着您之前已经进行过迁移,其中UserRoles是表名-可能来自您的DevEx资料.如果他们在升级中更改了模型,则可能会发生这种情况.当前的模型期望使用RoleUsers等,因此您需要到达那里.

OK, I will take a stab :) Your Up() code is trying to rename the table UserRoles to RoleUsers. This means you have a prior migration where UserRoles was the table name - probably from your DevEx stuff. This could happen if they changed their models in an upgrade. The current models are expecting RoleUsers etc. so you need to get there.

因此,第一个选择是让迁移进行重命名以匹配基础模型.我认为这不起作用或导致其他问题?

So first option is let the migration do the renaming to match the underlying model. I assume this didn't work or causes other issues?

您也许可以愚弄"实体框架,使其使用带有流畅代码或注释的旧表,但是如果它具有新的列或关系不起作用的话.

You might be able to 'fool' entity framework into using the old tables with fluent code or annotations, but if it has new columns or relationships that won't work.

我要这样做的是:

1)使用您拥有的相同引用创建一个新的测试项目,并 复制您的上下文和DbSet.将连接字符串指向 新的数据库.

1) Create a new test project with the same references you had and copy your context and DbSets. Point the connection string to a new database.

2)添加迁移并将其编写为脚本: update-database -Script.

2) Add a migration and script it out: update-database -Script.

3)检查此脚本并使用它来创建 数据库中所需的对象.从旧版本迁移数据 如有需要,可以将表更改为新表.

3) Examine this script a use it to create the objects needed in your database. Migrate data from the old tables to new if needed.

4)删除旧表

5)在您的实际情况中 项目添加迁移以重新同步模型: add-migration SyncDevExUpdate -IgnoreChangeupdate-database

5) In your actual project add a migration to resync your models: add-migration SyncDevExUpdate -IgnoreChange, update-database

现在,您将拥有模型期望的表.

Now you will have the tables your models expect.

这篇关于代码迁移意外尝试重命名表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆