jjczopek
jjczopek

Reputation: 3379

LINQ to SQL - Linq tries to insert column which doesn't exist

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 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?

EDIT 1:

I followed dasblinkenlight instructions and looked into *.dbml file via text editor. 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" />
    </Type>
  </Table>

Edit 2:

Following suggestion from comments, I checked generated Uczen_Opiekun entity. 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.

    [global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.Uczen_Opiekun")]
    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;
        (...)
    }

Upvotes: 0

Views: 795

Answers (1)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726639

There is no magic: Szkola must be somewhere in your dbml file. Open that file in a text editor (if you use Visual Studio, right-click and choose "Open With...", then "Text Editor"). Now search for Szkola, and remove it from the XML.

Upvotes: 2

Related Questions