views:

310

answers:

5

Related to http://stackoverflow.com/questions/1660200/how-to-debug-a-linq-to-sql-insertonsubmit-statement

I Execute the following bit of code:

db.DBUsers.InsertOnSubmit(new DBUser
 {
  Id = id,
  BrandCode3 = brandCode3.ToLower(),
  LoweredUsername = username.ToLower(),
  Username = username,
  CreationDate = date,
  Salt = salt,
  Password = SecurityService.CreateMD5Hash(salt + password),
  PasswordQuestion = passwordQuestion,
  PasswordAnswer = passwordAnswer,
  Comment = comment,
  Email = email,
  IsApproved = isApproved,
  LastPasswordChangedDate = date,
  FailedPasswordAnswerAttemptCount = 0,
  FailedPasswordAnswerAttemptWindowStart = date,
  IsLockedOut = false,
  IsOnLine = false,
  LastActivityDate = date
 }
);
db.SubmitChanges();

UPDATE
Here's the attributes for the primary key:

[Column(Storage="_Id", DbType="UniqueIdentifier NOT NULL", IsPrimaryKey=true)]
public System.Guid Id

I noticed that the Auto-sync property has the value "Never" in the properties window. My guess would be this needs to be OnInsert. Gonna check that.

Right before I call submitchanges I check the change set and it shows no inserts, updates or deletes... SQL profiler shows me no query is sent, attaching a db.Log shows me no action is executed.
Why isn't anything submitted?

A: 

No primary key defined in the L2S model? Check so at least one column has the "primary key" attribute set to true.

KristoferA - Huagati.com
Checked that, the ID value is primary key and it's not an identity.To be sure I deleted it from the dbml and added it again. No change
borisCallens
A: 

Is ObjectTracking maybe disabled?

leppie
yes, it's enabled. Just to be sure I added a line to set it to true. No change.
borisCallens
Something weird going on with your PC :), try create a fresh DB with a simple table, and see if the behaviour stays the same.
leppie
I tested it on 4 databases (on two servers) and get the same behaviour :S
borisCallens
resorted to oldskool SqlCommands now. Kind of hard to make my case to my colleagues in favor of Linq now :S
borisCallens
Can you paste the CREATE TABLE command, and the Table XML entry in the .dbml file please.
leppie
A: 

I suggest you post the auto generated Linq annotations for the primary key and your version tracking field, then we might be able to help. Here are examples from my working entity model.

[Column(Storage="_Id", AutoSync=AutoSync.OnInsert,
DbType="Int NOT NULL IDENTITY",
IsPrimaryKey=true, IsDbGenerated=true, UpdateCheck=UpdateCheck.Never)]

And the timestamp column:

[Column(Storage="_Ts", AutoSync=AutoSync.Always, DbType="rowversion NOT NULL",
CanBeNull=false, IsDbGenerated=true, IsVersion=true, 
UpdateCheck=UpdateCheck.Never)]

public System.Data.Linq.Binary Ts

camelCase
I added a reaction to the OP. Please have a look
borisCallens
Have you actually set your Guid primary in code? My guess is that Linq does not consider the entity insertable until you trigger set on the Id property.Guid _id; does not initialise a guid variable with a meaningful value. You need:entity.Id = Guid.NewGuid();
camelCase
A: 

There's nothing I can see from the code that would indicate to me a problem. At this point, I would drag and drop a new DBUser object onto your DBML file (rename it DBUser2), execute this code, and watch it work. Having working code, I would then check for the differences between the two entities. This is going to be something non-obvious. Also, click "Run Custom Tool" on your DBML file and watch your Errors/Warnings window for any warnings -- SqlMetal might just tell you what's wrong in the form of a warning.

Dave Markle
where does sqlmetal show any feedback? There's no console window popping up nor any lines appearing in the errors list.
borisCallens
Check out your Output window. Your Warnings window will also have sqlmetal errors.
Dave Markle
A: 

If your Guid Id column is being generated by the database, then this is a bug in Linq To Sql, as documented here, because your property name is different than your column name.

From your example, it looks like you're assigning it in your application, so it may not apply.

Aaron Daniels