Linq tries to insert column which doesn't exist RRS feed

  • Question

  • I created a simple database schema in Visual Studio and deployed to SQL Server. Then I created Linq-to-SQL mappings by dragging all the tables onto my model from the server explorer.

    After some time I decided to drop one column from the link table. I modified schema, removed database and deployed it again. I recreated my mappings by removing all entities from diagram and dragging all tables again.

    The problem is, that when I try to insert new entity to that table (on which I dropped my column), Linq-to-SQL still tries to insert a value there and that triggers a SQL Exception, saying that Szkola column name is invalid. I double checked entity diagram - no sign of Szkola column there and IntelliSense also doesn't show that Szkola might exist. But after inspecting generated SQL (after calling .SubmitChanges()):

    INSERT INTO [dbo].[Uczen_Opiekun]([UczenPESEL], [OpiekunPESEL], [StopienPokrewienstwa], [Szkola])
    VALUES (@p0, @p1, @p2, @p3)
    -- @p0: Input VarChar (Size = 8000; Prec = 0; Scale = 0) [10987654321]
    -- @p1: Input VarChar (Size = 8000; Prec = 0; Scale = 0) [12345678910]
    -- @p2: Input VarChar (Size = 8000; Prec = 0; Scale = 0) [ojciec]
    -- @p3: Input VarChar (Size = 8000; Prec = 0; Scale = 0) [Null]

    I looked into  my *.dbml file, and I found several occurences of Szkola column in other tables since it's a validation table, but in Uczen_Opiekun link table (which causes problem) I didn't find any Szkola references.

    <Table Name="dbo.Uczen_Opiekun" Member="Uczen_Opiekuns">
        <Type Name="Uczen_Opiekun">
          <Column Name="UczenPESEL" Type="System.String" DbType="VarChar(11) NOT NULL" IsPrimaryKey="true" CanBeNull="false" />
          <Column Name="OpiekunPESEL" Type="System.String" DbType="VarChar(11) NOT NULL" IsPrimaryKey="true" CanBeNull="false" />
          <Column Name="StopienPokrewienstwa" Type="System.String" DbType="VarChar(64)" CanBeNull="true" />
          <Association Name="Opiekun_Uczen_Opiekun" Member="Opiekun" ThisKey="OpiekunPESEL" OtherKey="PESEL" Type="Opiekun" IsForeignKey="true" />
          <Association Name="Uczen_Uczen_Opiekun" Member="Uczen" ThisKey="UczenPESEL" OtherKey="PESEL" Type="Uczen" IsForeignKey="true" />

    I also checked generated Uczen_Opiekun entity source. There is no Szkola column, but still it's present in insert statement generated by Linq. Here is a short listing with fields generated from database schema.

        public partial class Uczen_Opiekun : INotifyPropertyChanging, INotifyPropertyChanged
            private static PropertyChangingEventArgs emptyChangingEventArgs = new PropertyChangingEventArgs(String.Empty);          
            private string _UczenPESEL;         
            private string _OpiekunPESEL;           
            private string _StopienPokrewienstwa;           
            private EntityRef<Opiekun> _Opiekun;            
            private EntityRef<Uczen> _Uczen;

    I cleaned solution couple of times, removed and recreated mappings and database several times and nothing helped. Why this is happening and how to solve this issue?




    • Edited by jjczopek Friday, January 27, 2012 11:52 AM
    Friday, January 27, 2012 11:49 AM


  • Hi jjczopek;

    Make sure that there is class in your project called Uczen_Opiekun that contains the deleted column from the designer.

    You can also try deleting the dbml file from Solution Explorer and re-creating it.

    Hope that helps.

    Fernando (MCSD)

    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Saturday, January 28, 2012 7:30 PM