2017-02-23 77 views
2

我想實現一個更改日誌作爲建議在 Dev Express XAF T474899代碼遷移意外嘗試重命名錶

我使用由XAF新的解決方案嚮導生成的安全系統

我已經定義一些業務對象存儲更改日誌信息。

其中一個對象存儲指向用戶的鏈接

public virtual User User {get;組; }

在生成的代碼移植,我驚訝地看到向上()方法中添加以下

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

在另一個場合,我發現在一個向上()

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

在以下兩種情況下創建實體的代碼都是Dev Express庫。

我交張貼了這個問題Dev Express Support

的開發快遞業務對象在DevExpress.Persistent.BaseImpl.EF定義;

我的DbContext語境指的是他們作爲

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

對角色的元數據顯示 enter image description here

用戶的元數據顯示 enter image description here

我自己的業務類包含

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() 
    { 
    } 
} 

如果我取消註釋代碼有內部員工的用戶屬性,併產生遷移,遷移最多是

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

[更新] 有趣的是有更多的開發快速的表比我第一個念頭。主鍵是Identity。

enter image description here 我覺得現在用標準認證創建之前開發快速添加允許/拒絕的能力(V16.1)

[更新] 當我創建了上述設置一個新的項目,這裏是的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; } 
    } 
} 
+1

Cross發佈到Dev Express https://www.devexpress.com/Support/Center/Question/Details/T486359 –

+1

顯示用戶類。那是身份嗎? Context和DbSets可能也有幫助。 –

+0

謝謝@SteveGreene我已更新問題 –

回答

1

好的,我會採取一個措施:)你的Up()代碼試圖將UserRoles重命名爲RoleUsers。這意味着你有一個事先的遷移UserRoles是表名 - 可能來自你的DevEx的東西。如果他們在升級中更改模型,則可能會發生這種情況。目前的模型正在期待RoleUsers等,所以你需要去那裏。

因此,第一個選項是讓遷移進行重命名以匹配基礎模型。我認爲這不起作用或導致其他問題?

您可能會將實體框架'愚弄'爲使用流利的代碼或註釋的舊錶,但是如果它有新的列或關係不起作用。

我會做的是這樣的:

1)創建與你有和 複製你的背景和DbSets同樣引用了新的測試項目。將連接字符串指向 新數據庫。

2)添加一個遷移並編寫腳本: update-database -Script

3)檢查這個腳本,用它來創建數據庫中需要的對象 。如果需要,將舊數據表 中的數據遷移到新數據庫。

4)刪除舊錶

5)在您的實際 項目添加遷移到重新同步您的機型: add-migration SyncDevExUpdate -IgnoreChangeupdate-database

現在你將有你的模型預期的表。