System.Data.SqlClient.SqlException:无效的列名称'phone_types_phone_type_id'

时间:2014-04-28 19:42:39

标签: c# asp.net sql asp.net-mvc entity-framework

我试图从我的一些模型中获取与我的主要员工模型具有外键关系的信息。如果我单独绘制每个模型,我可以正常访问它们而没有任何问题,但我必须访问多个不同的网页才能这样做。

我试图将我的几个模型合并到一个单独的控制器中,并以这种方式使用它们。不幸的是,当我尝试访问这些模型时,我得到一个奇怪的错误:

  

System.Data.SqlClient.SqlException:列名无效' phone_types_phone_type_id'。

搜索完代码后,显然唯一的位置phone_types_phone_type_id出现在我的迁移代码中。我总体上非常擅长C#和Asp.Net,所以任何帮助都会受到赞赏。

以下是我的模型的代码:

[Table("employee.employees")]
public partial class employees1
{
    public employees1()
    {
        employee_email_manager = new List<email_manager>();
        employee_employment_history = new HashSet<employment_history>();
        employee_job_manager = new HashSet<job_manager>();
        employee_phone_manager = new HashSet<phone_manager>();
        this.salaries = new HashSet<salary>();
    }

    [Key]
    public int employee_id { get; set; }
    [Display(Name="Employee ID")]
    public int? assigned_id { get; set; }

    [Display(Name="Web User ID")]
    public int? all_id { get; set; }

    [Required]
    [StringLength(50)]
    [Display(Name="First Name")]
    public string first_name { get; set; }

    [StringLength(50)]
    [Display(Name="Last Name")]
    public string last_name { get; set; }

    [Column(TypeName = "date")]
    [Display(Name="Birthday")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime birth_day { get; set; }

    [Required]
    [StringLength(1)]
    [Display(Name="Gender")]
    public string gender { get; set; }

    [Required]
    [StringLength(128)]
    [Display(Name="Social")]
    public string social { get; set; }

    [Required]
    [StringLength(128)]
    [Display(Name="Address")]
    public string address_line_1 { get; set; }

    [StringLength(50)]
    [Display(Name="Suite/Apt#")]
    public string address_line_2 { get; set; }

    [Required]
    [StringLength(40)]
    [Display(Name="City")]
    public string city { get; set; }

    [Required]
    [StringLength(20)]
    [Display(Name="State")]
    public string state { get; set; }

    [Required]
    [StringLength(11)]
    [Display(Name="Zip")]
    public string zip { get; set; }

    [Column(TypeName = "date")]
    [Display(Name="Hire Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime hire_date { get; set; }

    [Column(TypeName = "date")]
    [Display(Name="Separation Date")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:MM/dd/yyyy}")]
    public DateTime? termination_date { get; set; }

    [StringLength(70)]
    [Display(Name="Emergency Contact Name")]
    public string emergency_contact_name { get; set; }

    [StringLength(15)]
    [Display(Name = "Emergency Contact Number")]
    public string emergency_contact_phone { get; set; }

    [Display(Name = "Notes")]
    public string notes { get; set; }

    public virtual ICollection<phone_manager> employee_phone_manager { get; set; }

    [Table("employee.phone_manager")]
    public partial class phone_manager
    {
        [Key]
        public int phone_id { get; set; }

        public int employee_id { get; set; }

        [Required]
        [StringLength(15)]
        public string phone_number { get; set; }

        [StringLength(5)]
        public string phone_extension { get; set; }

        public int phone_type { get; set; }

        [Column(TypeName = "date")]
        public DateTime date_added { get; set; }

        public bool deleted { get; set; }

        public virtual employees1 employees1 { get; set; }

        public virtual phone_types phone_types { get; set; }
    }

    [Table("employee.phone_types")]
    public partial class phone_types
    {
        public phone_types()
        {
            phone_manager = new HashSet<phone_manager>();
        }

        [Key]
        public int phone_type_id { get; set; }

        [Required]
        [StringLength(50)]
        public string phone_type_name { get; set; }

        public virtual ICollection<phone_manager> phone_manager { get; set; }
    }
}

我认为的相关代码:

        @foreach (var item in Model.employee_phone_manager)
        {
            @Html.DisplayFor(modelItem => item.phone_number); 
            @: - 
            @Html.DisplayFor(modelItem => item.phone_type);
            <br />
        }

编辑我可能已经发现了这个问题,但如果有其他选择,我肯定会收到更多的输入。我的解决方案是在我的[ForeignKey("phone_type")]类中直接在此行上方添加以下内容:public virtual phone_types phone_types { get; set; }phone_manager

7 个答案:

答案 0 :(得分:12)

您的问题,数据层中的连接字符串和Web层中的连接字符串指向不同的数据库。

e.g。 数据层读取开发数据库 webapp指向测试数据库。

更新连接字符串以指向同一数据库

确保您的两个数据库都有相同的表和列

答案 1 :(得分:6)

经过相当多的研究后,似乎我有一个相当独特的问题。我尝试了在这里和许多其他网站上列出的几个修复程序,但似乎没有什么能解决这个问题。

但是,我在原帖的底部列出的解决方案似乎正在运行,并且保持良好,所以我认为这是解决我问题的一个相当充分的解决方案。

为了概述发生了什么,MVC EF试图在两个模型中找到fk / pk关系,但由于模型中的列名不同,因此无法正确映射它们。如果我要使用email_manager表格尝试从email_types收到所有电子邮件,那么这不是问题,而是向后移动,并从email_types获取信息email_manager犯了错误。

由于两个表之间的列名不同,EF尝试创建一个列来保存关系,但由于没有这样的列,因此抛出了错误。要纠正这个问题,所有必要的是告诉EF外键列实际是什么,并且通过在容纳父模型的集合上方使用[ForeignKey("email_type")]来完成。

例如,我的新email_typesemail_manager模型如下:

    [Table("employee.email_manager")]
    public partial class email_manager
    {
        [Key]
        public int email_id { get; set; }

        public int employee_id { get; set; }

        [Required]
        [StringLength(255)]
        public string email { get; set; }

        public int email_type { get; set; }

        [Column(TypeName = "date")]
        public DateTime date_added { get; set; }

        public bool deleted { get; set; }
        [ForeignKey("email_type")]
        public virtual email_types email_types { get; set; }

        public virtual employees1 employees1 { get; set; }
    }

    [Table("employee.email_types")]
    public partial class email_types
    {
        public email_types()
        {
            email_manager = new HashSet<email_manager>();
        }

        [Key]
        public int email_type_id { get; set; }

        [Required]
        [StringLength(50)]
        public string email_type_name { get; set; }

        public virtual ICollection<email_manager> email_manager { get; set; }
    }

答案 2 :(得分:0)

您已使用[Table("employee.employees")]指定数据库表。检查数据库表是否有一个名为phone_types_phone_type_id的列。尝试查找该列的数据,但它找不到列然后抛出此消息。我的问题已解决检查我的数据库数据库表。

答案 3 :(得分:0)

我正在使用nop Commerce,为了解决我的问题,我不得不在数据库映射中使用ignore

Ignore(p => p.CategoryAttachmentType);

在我拥有的域中

/// <summary>
    /// Gets or sets the category attachment type
    /// </summary>
    public CategoryAttachmentType CategoryAttachmentType
    {
        get
        {
            return (CategoryAttachmentType)this.CategoryAttachmentTypeId;
        }
        set
        {
            this.CategoryAttachmentTypeId = (int)value;
        }
    }

答案 4 :(得分:0)

我遇到了同样的异常。我的解决方案是转到模型类,并在给定的属性定义/类型定义的地方验证异常。在这里最好检查定义“ phone_types_phone_type_id”的Model类。

答案 5 :(得分:0)

I had the similar issue. What happens is that in the database foreign keys are created and it starts mapping both the models and then throws an exception. Best way is to avoid foreign key creation by using [NotMapped] as you could use complex models and also avoid creation of Foreign Key.

答案 6 :(得分:0)

您是对的。 我有类似的问题。 像这样

m

如您所见,我在最后一行保留了名称“ StateTbl”,而不是“ StatesTbl” 和应用程序一直在寻找StateTblID。然后,我不得不将名称更改为“ StatesTbl”。然后它开始运作良好。 所以现在,我更改的行是:

   [ForeignKey("StatesTbl")]
   public int? State { get; set; }
   public StatesTbl StateTbl { get; set; }

这些位于AppDbContext.cs类文件中

相关问题