4

I've been working with LINQ To SQL for some time now, and what I usually do in a solution is the following:

  • In a project I create a dbml schema.
  • In another project I create a simple DataAccessLayer (DAL) that knows my first project, and instantiates a DataContext.
  • In the 3rd project (Business logic) I instantiate my DAL.

This usually works well. However, this time, I don't know why, but "It" doesn't work. "It" being "Me updating the database". I changed my code around to do some tests, and I get a result I don't understand.

MyDataContext dataContext = new MyDataContext(MyConnectionString);
DataBaseItem dbi = (from item in dataContext.DataBaseItems
    where item.ID == 1
    select item).First();
dbi.Name= "toto";
// dataContext.GetChangeSet() tells me nothing changed.

I dug deeper by breaking into the bdi.Name = "toto"; and compared it with a similar value assignment in a project where it works (both are designer generated code) and saw that some code was missing (I wrote them down there, but I commented them so you see what is missing) :

[Column(Storage="_Name", DbType="NVarChar(250)")]
public string Name
{
    get
    {
        return this._Name;
    }
    set
    {
        if ((this._Name!= value))
        {
            //this.OnLayoutChanging(value);
            //this.SendPropertyChanging();
            this._Name= value;
            //this.SendPropertyChanged("Name");
            //this.OnLayoutChanged();
        }
    }
}

Anyone can tell me how come these lines are missing, and where did I messed up?

When I do dataContext.Refresh(RefreshMode.KeepChanges, dataContext.DataBaseItems);, I get an error:

An object specified for refresh is not recognized.

p.campbell
  • 91,713
  • 61
  • 243
  • 314
Tipx
  • 6,738
  • 3
  • 32
  • 58
  • did you try clean & rebuild on your solution to make sure that all of your dependencies update? Also, if you change the underlying DB you need to update your dbml file by dropping & re-adding the tables on the dbml surface. – Brian Driscoll Nov 15 '10 at 17:37
  • Yes, I did try to do that. When I saw it wasn't working, I deleted my whole .dbml and created a new one, with a different name just to be sure, and I get the same thing. I forgot to mention another "weird" thing I realized. I'm editing my post right now to include that observation. – Tipx Nov 15 '10 at 17:40
  • It finally worked... I deleted my linq-to-sql objects, cleaned my solution, closed my IDE, re-opened the solution, recreated the objects, and now they generated properly (and the error I wrote in the Edit disappeared). What should I do with my question? Just a plain delete since it was not a programming error, but an IDE glitch? – Tipx Nov 15 '10 at 19:04
  • 1
    you should add an answer to your own question in enough detail that someone who has the same problem can learn from it. – Dour High Arch Nov 15 '10 at 19:41

2 Answers2

3

If your objects do not have a primary key, then the objects will not be tracked for changes. It is probable ID was not set as primary key in the dbml.

Amy B
  • 100,846
  • 20
  • 127
  • 174
  • Yes, the designer was glitching and every times I generated my object from the DB, it wouldn't set any constraints. – Tipx Nov 17 '10 at 14:01
1

The error was within the IDE (VS2008). The generation, or regeneration, of any Linq-to-Sql object by the IDE was faulty. To fix the problem, I had to :

  1. Remove the objects that were erroneous.
  2. Close Visual Studio.
  3. Reopen Visual Studio.
  4. Recreate the objects.

Not doing the first stop and instead have the object regenerated by changing a parameter could have worked, but I can't test it, since I don't know how to reproduce the problem.

Tipx
  • 6,738
  • 3
  • 32
  • 58