System.Data.SqlClient.SqlException: Invalid column name 'phone_types_phone_type_id'

前端 未结 7 625
野的像风
野的像风 2020-12-10 02:28

I\'m trying to get information from some of my models that have a foreign key relationships to my main employee model. If I map out each model individually, I can access the

7条回答
  •  暖寄归人
    2020-12-10 03:08

    After doing quite a bit more research, it seems like I had a fairly unique issue. I attempted several of the fixes listed both on here and many other sites, but almost nothing seemed to fix the issue.

    However, the solution I listed at the bottom of my original post seems to be working, and holding up well, so I believe it to be a fairly adequate solution to my problem.

    To somewhat outline what was occurring, MVC EF was attempting to find a fk/pk relationship across two models, but since the column names across the models were different, it wasn't able to map them properly. If I were to trying to get all the emails from email_manager by using the email_types table, it wasn't an issue, but moving backwards, and grabbing the information from email_types from email_manager threw errors.

    Since the column names between the two tables are different, EF tried to create a column to house the relationship, but since no such column existed, an error was thrown. To correct this, all that's necessary is to tell EF what the foreign key column actually is, and that is done by using [ForeignKey("email_type")] above the collection that houses the parent model.

    So for example, my new email_types and email_manager models were as follows:

        [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();
            }
    
            [Key]
            public int email_type_id { get; set; }
    
            [Required]
            [StringLength(50)]
            public string email_type_name { get; set; }
    
            public virtual ICollection email_manager { get; set; }
        }
    

提交回复
热议问题