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

前端 未结 7 596
野的像风
野的像风 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 02:49

    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.

    0 讨论(0)
  • 2020-12-10 02:49

    You are right. I had similar issue. Something like this

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

    So as you can see, I had kept name 'StateTbl' in the last line instead of 'StatesTbl' and app kept looking for StateTblID. Then I had to change name to 'StatesTbl' instead. And then it started working well. So now, my changed lines were:

       [ForeignKey("StatesTbl")] <== 'StatesTbl' is my original States table
       public int? State { get; set; }
       public StatesTbl StatesTbl { get; set; }
    

    These are in the AppDbContext.cs class file

    0 讨论(0)
  • 2020-12-10 02:51

    You have specify the Database Table using [Table("employee.employees")]. Check your database Table is there have a column that name is phone_types_phone_type_id .It Try to find data of that column but It did not find column then throw this Message. My Problem has solve Check my database database Table.

    0 讨论(0)
  • 2020-12-10 02:56

    Your issue is that your connection string in data layer and connection string in web layer are pointing to different databases.

    e.g. data layer reading dev database webapp pointing to test database.

    Either update connection strings to point to the same database.

    or

    Make sure your both database have same tables and columns.

    0 讨论(0)
  • 2020-12-10 02:56

    I came across the same kind of exception. My solution is to go to the model class and verify the exception given property definition/type where it defines. In here better check the Model class/classes where you define 'phone_types_phone_type_id'.

    0 讨论(0)
  • 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<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; }
        }
    
    0 讨论(0)
提交回复
热议问题