2009-06-07 5 views
1

j'utilise C# et LINQ to SQLLINQ ne présente des modifications

je lance la commande suivante:

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(); 

Mais absolument aucun changement sont émis.

Le tableau de messages a été créé avec ce SQL exactement:

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 

Donc, si je mets un breakpoing juste avant la SubmitChanges appeler et vérifier this.AppManager.DB.GetChangeSet(); dans une montre, il est dit: {Inserts: 0, Mises à jour: 0, Supprime: 0}

Ce qui ne devrait clairement pas être le cas.

Mon produit objet Post ressemble à ceci (environ):

[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[...] 

Et, dans mon contrôleur de base:

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; 
    } 
} 

Toutes les idées à ce qui se passe mal?

Répondre

1

Votre erreur est dans la ligne suivante:

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

Logic semble indiquer que vous devez ajouter le ApplicationManager à la session.

+0

Vous avez raison. Comment pourrais-je être si aveugle? Merci, je suppose que c'est ce que j'ai pour utiliser un raccourci. –

2

Puisqu'il semble que DB est une propriété sur AppManager, pourriez-vous afficher le code des accesseurs de propriété? Votre code me semble correct, donc la seule chose que je peux penser est qu'un nouveau contexte de données est généré via l'accesseur.

+0

Mise à jour avec mon accesseur AppManager. –

+0

Veuillez également mettre à jour votre question avec le code d'accès DB sur AppManager. –

+0

+1, vous aviez raison. J'ai donné la réponse à Leppie, parce qu'il a trouvé la ligne. –

Questions connexes