views:

149

answers:

2

I'm using C# and LINQ to SQL

I'm issuing the following command:

User cu = CurrentUser;

Post newPost = new Post
{
    CreateDate = now,
    ModifyDate = now,
    User = cu,
    ModifyUser = cu,
    Text = text,
    Title = title,
    Thread = t,
    ResponseToPostID = null
};

this.AppManager.DB.Posts.InsertOnSubmit(newPost);
this.AppManager.DB.SubmitChanges();

But absolutely no changes are being issued.

The Posts table was created with this SQL exactly:

CREATE TABLE forum.Posts (
    PostID bigint NOT NULL IDENTITY,
    ThreadID bigint NOT NULL,
    ResponseToPostID bigint NULL,
    UserID bigint NOT NULL,
    Title varchar(60) NOT NULL,
    [Text] text NOT NULL,
    CreateDate datetime NOT NULL,
    ModifyDate datetime NOT NULL,
    ModifyUserID bigint NULL,
    CONSTRAINT PK_Posts PRIMARY KEY CLUSTERED (PostID ASC)
)
GO

ALTER TABLE forum.Posts
    WITH CHECK
    ADD CONSTRAINT FK_Posts_Threads
    FOREIGN KEY (ThreadID)
    REFERENCES forum.Threads (ThreadID)
    ON UPDATE CASCADE
    ON DELETE CASCADE
GO

ALTER TABLE forum.Posts
    WITH CHECK
    ADD CONSTRAINT FK_Posts_Users
    FOREIGN KEY (UserID)
    REFERENCES dbo.Users (UserID)
    ON UPDATE CASCADE
    ON DELETE CASCADE
GO

ALTER TABLE forum.Posts
    WITH CHECK
    ADD CONSTRAINT FK_Posts_ModifyUsers
    FOREIGN KEY (ModifyUserID)
    REFERENCES dbo.Users (UserID)
    ON UPDATE NO ACTION
    ON DELETE NO ACTION
GO

So, if I set a breakpoing just before the SubmitChanges call and check this.AppManager.DB.GetChangeSet(); in a watch, it says: {Inserts: 0, Updates: 0, Deletes: 0}

Which should clearly not be the case.

My generated Post object looks like this (about):

[Column(Storage="_PostID", AutoSync=AutoSync.OnInsert, DbType="BigInt NOT NULL IDENTITY", IsPrimaryKey=true, IsDbGenerated=true)]
public long PostID[...]

[Column(Storage="_ThreadID", DbType="BigInt NOT NULL")]
public long ThreadID[...]

[Column(Storage="_ResponseToPostID", DbType="BigInt")]
public System.Nullable<long> ResponseToPostID[...]

[Column(Storage="_UserID", DbType="BigInt NOT NULL")]
public long UserID[...]

[Column(Storage="_Title", DbType="VarChar(60) NOT NULL", CanBeNull=false)]
public string Title[...]

[Column(Storage="_Text", DbType="Text NOT NULL", CanBeNull=false, UpdateCheck=UpdateCheck.Never)]
public string Text[...]

[Column(Storage="_CreateDate", DbType="DateTime NOT NULL")]
public System.DateTime CreateDate[...]

[Column(Storage="_ModifyDate", DbType="DateTime NOT NULL")]
public System.DateTime ModifyDate[...]

[Column(Storage="_ModifyUserID", DbType="BigInt")]
public System.Nullable<long> ModifyUserID[...]

[Association(Name="User_Post", Storage="_User", ThisKey="ModifyUserID", OtherKey="UserID", IsForeignKey=true)]
public User ModifyUser[...]

[Association(Name="User_Post1", Storage="_User1", ThisKey="UserID", OtherKey="UserID", IsForeignKey=true, DeleteOnNull=true, DeleteRule="CASCADE")]
public User User[...]

[Association(Name="Thread_Post", Storage="_Thread", ThisKey="ThreadID", OtherKey="ThreadID", IsForeignKey=true, DeleteOnNull=true, DeleteRule="CASCADE")]
public Thread Thread[...]

And, in my base controller:

protected LanLordzApplicationManager AppManager
{
    get
    {
        return (LanLordzApplicationManager)(Session["Application"] ?? new LanLordzApplicationManager(Server.MapPath("~/")));
    }
}

protected User CurrentUser
{
    get
    {
        if (Session["UserID"] == null)
        {
            return null;
        }
        else
        {
            return this.AppManager.GetUserByUserID((long)Session["UserID"]);
        }
    }
    set
    {
        Session["UserID"] = value.UserID;
    }
}

// In LanLordzAppManager:

public LanLordzDataContext DB
{
    get
    {
        return this.db;
    }
}

Any ideas as to what is going wrong?

+2  A: 

Since it looks like DB is a property on AppManager, could you show the code for the property accessors? Your code looks ok to me so the only thing that I can think is that a new data context is being generated via the accessor.

tvanfosson
Updated with my AppManager accessor.
John Gietzen
Please update your question with the DB accessor code on the AppManager also.
Eric King
+1, You were right. I gave the answer to leppie, 'cause he found the line.
John Gietzen
+1  A: 

Your error lies in the following line:

return (LanLordzApplicationManager)(Session["Application"] ?? 
  new LanLordzApplicationManager(Server.MapPath("~/")));

Logic seems to indicate you should add the ApplicationManager to the Session.

leppie
You are right. How could I be so blind? Thanks, I guess that's what I get for using a shortcut.
John Gietzen