LINQ没有提交修改
问题描述:
我使用C#和LINQ to SQLLINQ没有提交修改
我发出以下命令:
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();
但绝对正在发行的任何更改。
posts表与此SQL正是创建:
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
所以,如果我设置了breakpoing之前的的SubmitChanges调用,并在表检查this.AppManager.DB.GetChangeSet();
,它说:{插入:0,更新: 0,删除:0}
显然不是这种情况。
我产生Post对象看起来像这样(大约):
[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[...]
而且,在我的基本控制器:
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;
}
}
任何想法是什么问题呢?
答
你的错误就在于下面一行:
return (LanLordzApplicationManager)(Session["Application"] ??
new LanLordzApplicationManager(Server.MapPath("~/")));
逻辑似乎表明你应该ApplicationManager添加到会话。
答
由于它看起来像DB是AppManager上的一个属性,您能否显示属性访问器的代码?你的代码对我来说很好,所以我唯一能想到的就是通过访问器产生一个新的数据上下文。
用我的AppManager访问器更新。 – 2009-06-07 16:43:56
请使用AppManager上的数据库访问器代码更新您的问题。 – 2009-06-07 17:14:54