首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在运行到SQL Azure DB的EF5数据迁移后,查询失败并出现"invalid column“错误

在运行到SQL Azure DB的EF5数据迁移后,查询失败并出现"invalid column“错误
EN

Stack Overflow用户
提问于 2012-12-18 14:29:35
回答 1查看 1.1K关注 0票数 1

我修改了user类,并在Visual Studio 2012的包管理器控制台中创建了数据迁移。数据迁移包括以下脚本:

代码语言:javascript
复制
            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);

运行迁移后,成功地在数据库中创建了用户表,其中指示了所有列。但是,对数据库的后续查询会生成以下错误:

代码语言:javascript
复制
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'.

user类继承自AuditableClass,其定义如下:

代码语言:javascript
复制
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; }
    }

下面是user类:

代码语言:javascript
复制
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继承的。

如何让实体框架识别继承的列?

EN

回答 1

Stack Overflow用户

发布于 2012-12-19 08:19:17

发现问题的原因是,User对象包含一个Roles集合,该集合是在实例化用户时在运行时动态加载的。与角色类对应的Roles表最初也是安全架构的一部分,并且在移动Users表的同时被移到了事务性架构中。Roles表是使用Package Manager控制台的自动数据迁移功能创建的。此功能创建了表,但缺少继承的列(角色也继承自AuditableClass)。

创建数据迁移,然后输入代码以手动创建自动迁移遗漏的列,从而解决了问题。

以下是解决该问题的迁移代码:

代码语言:javascript
复制
    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()
    {
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/13927508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档