2012-12-18 20 views
1

我在Visual Studio 2012的程序包管理器控制檯中修改了用戶類並創建了數據遷移。數據遷移包括以下腳本:將EF5數據遷移到SQL Azure數據庫後,查詢失敗並出現「無效列」錯誤

  CreateTable(
      "dbo.Users", 
      c => new 
       { 
        Id = c.Int(nullable: false, identity: true), 
        Username = c.String(nullable: false), 
        Comment = c.String(), 
        EmailAddress = c.String(maxLength: 64), 
        Identifier = c.String(), 
        IsApproved = c.Boolean(nullable: false), 
        PasswordFailuresSinceLastSuccess = c.Int(nullable: false), 
        LastPasswordFailureDate = c.DateTime(), 
        LastActivityDate = c.DateTime(), 
        LastLockoutDate = c.DateTime(), 
        LastLoginDate = c.DateTime(), 
        ConfirmationToken = c.String(), 
        CreateDate = c.DateTime(), 
        IsLockedOut = c.Boolean(nullable: false), 
        LastPasswordChangedDate = c.DateTime(), 
        PasswordVerificationToken = c.String(), 
        PasswordVerificationTokenExpirationDate = c.DateTime(), 
        PersonId = c.Int(nullable: false), 
        OwnerId = c.Int(nullable: false), 
        EffectiveDate = c.DateTime(nullable: false), 
        ExpirationDate = c.DateTime(), 
        CreationDate = c.DateTime(nullable: false), 
        UpdatedDate = c.DateTime(nullable: false), 
        Creator = c.String(nullable: false), 
        Updater = c.String(nullable: false), 
        AlertConfiguration_Id = c.Int(), 
       }) 
      .PrimaryKey(t => t.Id) 
      .ForeignKey("dbo.People", t => t.PersonId, cascadeDelete: false) 
      .ForeignKey("dbo.AlertConfigurations", t => t.AlertConfiguration_Id) 
      .Index(t => t.PersonId) 
      .Index(t => t.AlertConfiguration_Id); 

運行遷移後,在數據庫中成功創建了用戶表,並指定了所有列。然而,隨後的查詢到數據庫生成以下錯誤:

Exception Details: System.Data.SqlClient.SqlException: Invalid column name 'OwnerId'. 
Invalid column name 'EffectiveDate'. 
Invalid column name 'ExpirationDate'. 
Invalid column name 'CreationDate'. 
Invalid column name 'UpdatedDate'. 
Invalid column name 'Creator'. 
Invalid column name 'Updater'. 

用戶類從AuditableClass,這是這樣定義繼承:

public abstract class AuditableClass 
    { 
     public AuditableClass() 
     { 
      this.CreationDate = System.DateTime.Now; 
     } 

     [ScaffoldColumn(false)] 
     public int Id { get; set; } 

     [Required(ErrorMessage = "An owner ID is required")] 
     [ScaffoldColumn(false)] 
     public int OwnerId { get; set; } 

     [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
     [Display(Name = "Effective Date")] 
     [Required(ErrorMessage = "An effective date is required")] 
     [DataType(DataType.Date)] 
     public DateTime? EffectiveDate { get; set; } 

     [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
     [Display(Name = "Expiration Date")] 
     [DataType(DataType.Date)] 
     public DateTime? ExpirationDate { get; set; } 

     [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
     [Required(ErrorMessage = "An creation date is required")] 
     [ScaffoldColumn(false)] 
     public DateTime? CreationDate { get; set; } 

     [DisplayFormat(DataFormatString = "{0:yyyy-MM-dd}", ApplyFormatInEditMode = true)] 
     [Required(ErrorMessage = "A last update date is required")] 
     [ScaffoldColumn(false)] 
     public DateTime? UpdatedDate { get; set; } 

     [Required(ErrorMessage = "An creator is required")] 
     [ScaffoldColumn(false)] 
     public string Creator { get; set; } 

     [Required(ErrorMessage = "A last updater is required")] 
     [ScaffoldColumn(false)] 
     public string Updater { get; set; } 
    } 

這裏是用戶類:

public class User : AuditableClass 
    { 
     [Required] 
     public virtual String Username { get; set; } 

     [DataType(DataType.MultilineText)] 
     public virtual String Comment { get; set; } 

     [Display(Name = "Email Address")] 
     [StringLength(64)] 
     public string EmailAddress { get; set; } 

     public string Identifier { get; set; } 
     public Boolean IsApproved { get; set; } 
     public int PasswordFailuresSinceLastSuccess { get; set; } 
     public DateTime? LastPasswordFailureDate { get; set; } 
     public DateTime? LastActivityDate { get; set; } 
     public DateTime? LastLockoutDate { get; set; } 
     public DateTime? LastLoginDate { get; set; } 
     public String ConfirmationToken { get; set; } 
     public DateTime? CreateDate { get; set; } 
     public Boolean IsLockedOut { get; set; } 
     public DateTime? LastPasswordChangedDate { get; set; } 
     public String PasswordVerificationToken { get; set; } 
     public DateTime? PasswordVerificationTokenExpirationDate { get; set; } 
     public virtual List<Role> Roles { get; set; } 
     public virtual List<myApp.Models.Parties.Org> AuthorizedOrgs { get; set; } 
     public virtual List<myApp.Models.Security.Permission> Permissions { get; set; } 

     public int PersonId { get; set; } 
     public virtual Person Person { get; set; } 
} 

作爲System.Data.SqlClient.SqlException異常主題的列都從AuditableClass繼承。

如何讓實體框架識別繼承的列?

+0

更新 - 我刪除了繼承關係和錯誤仍然存​​在,即使「無效」欄目不再有用戶相應的屬性類。 – spadelives

回答

0

發現問題,它是User對象包含一個Roles集合,它在用戶實例化時在運行時動態加載。與Role類相對應的Roles表最初也是安全性模式的一部分,並且在Users表被移動的同時已被移至事務模式。 Roles表是使用軟件包管理器控制檯的自動數據遷移功能創建的。此功能創建了表,但錯過了繼承的列(角色也從AuditableClass繼承)。

創建數據遷移,然後輸入代碼以手動創建自動遷移錯過的列解決了問題。

下面是遷移的代碼,解決了這個問題:

public partial class UpdateRoles : DbMigration 
{ 
    public override void Up() 
    { 
     AddColumn("dbo.Roles", "OwnerId", c => c.Int(nullable: false)); 
     AddColumn("dbo.Roles", "EffectiveDate", c => c.DateTime(nullable: false)); 
     AddColumn("dbo.Roles", "ExpirationDate", c => c.DateTime()); 
     AddColumn("dbo.Roles", "CreationDate", c => c.DateTime(nullable: false)); 
     AddColumn("dbo.Roles", "UpdatedDate", c => c.DateTime(nullable: false)); 
     AddColumn("dbo.Roles", "Creator", c => c.String(nullable: false, defaultValue: "myusername")); 
     AddColumn("dbo.Roles", "Updater", c => c.String(nullable: false, defaultValue: "myusername")); 
    } 

    public override void Down() 
    { 
    } 
} 
相關問題