我正在使用C#和LINQ to SQL
我发出以下命令:
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
所以,如果我在SubmitChanges调用之前设置了一个breakpoing并在watch中检查this.AppManager.DB.GetChangeSet();
,它会说:{Inserts:0,Updates:0,Deletes: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;
}
}
关于出了什么问题的任何想法?
答案 0 :(得分:2)
因为看起来DB是AppManager上的属性,你能展示属性访问器的代码吗?您的代码对我来说没问题,所以我唯一能想到的是通过访问器生成新的数据上下文。
答案 1 :(得分:1)
您的错误位于以下行:
return (LanLordzApplicationManager)(Session["Application"] ??
new LanLordzApplicationManager(Server.MapPath("~/")));
逻辑似乎表明你应该将ApplicationManager添加到Session。