简体   繁体   中英

Linq's InsertOnSubmit not added to changeset

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

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

ObjectTracking可能被禁用吗?

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM