av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

技巧:輕松搞定Linq插入數(shù)據(jù)問題

Linq 插入數(shù)據(jù)是一個很常規(guī)的操作,既然是常規(guī)操作,也就意味著會更容易出現(xiàn)一些稀奇古怪的問題,今天就讓我們來看一個典型問題的典型解決辦法。

今天用Linq插入數(shù)據(jù),總是插入錯誤,說某個主鍵字段不能為空,我檢查了半天感覺主鍵字段沒有賦空值啊,實在是郁悶。

要插入數(shù)據(jù)的表結(jié)構(gòu)是:

 
 
 
  1. create table RSSFeedRight  
  2. (  
  3. FeedId int Foreign Key (FeedId) References RSSFeed(FeedId) NOT NULL , -- FeedId ,   
  4. UserId int Foreign Key (UserId) References UserInfo(UserId) NOT NULL , -- UserId ,   
  5. RightValue bigint NOT NULL Primary key (UserId, FeedId),  

插入數(shù)據(jù)的代碼:

 
 
 
  1. RSSFeedRight feedRight = new RSSFeedRight();  
  2. feedRight.UserId = userId;  
  3. feedRight.FeedId = feedId;  
  4. feedRight.RightValue = 0 ;  
  5.  
  6. _Db.RSSFeedRights.InsertOnSubmit(feedRight);  
  7. _Db.SubmitChanges(); 

每次插入時都提示說FeedId 不能插入空值,郁悶的不行,分明是給了非空值的!

后來仔細檢查,發(fā)現(xiàn)這個RSSFeedRight 實體類中居然還有兩個指向UserInfo 和 RSSFeed 表的字段,后來逐漸感覺到是外鍵設(shè)置問題引起的。立即通過google 搜 "linq foreign key insert",發(fā)現(xiàn)有不少人遇到相同問題,找到其中一篇帖子,其中關(guān)于這個問題是這樣描述的:

The mapping information (Assocation attribute on Table1 & Table2) has the foreign key dependency going in the wrong direction. It's claiming that the primary-key in table1 (the one that is auto-incremented) is a foreign key to the primary key in table2. You want that just the opposite. You can change this in the designer, DBML file or directly in the code (for a quick test) by changing IsForeignKey value for both associations.

也就是說我們不能將主鍵設(shè)置為和外鍵相同,否則就會出問題。找到問題所在,就好辦了,將表結(jié)構(gòu)進行如下修改:

 
 
 
  1. create table RSSFeedRight  
  2. (  
  3. Id int identity ( 1 , 1 ) NOT NULL Primary Key ,  
  4. FeedId int Foreign Key (FeedId) References RSSFeed(FeedId) NOT NULL , -- FeedId ,   
  5. UserId int Foreign Key (UserId) References UserInfo(UserId) NOT NULL , -- UserId ,   
  6. RightValue bigint NOT NULL ,  

問題解決。

老兵遇到新問題,技術(shù)不經(jīng)常更新就要老化。

【編輯推薦】

  1. LINQ——語言級集成查詢?nèi)腴T指南
  2. LINQ查詢的目的與實現(xiàn)手段
  3. LINQ查詢表達式深入剖析
  4. 實例二:綁定到LINQ查詢的結(jié)果
  5. LINQ的演變及其對C#設(shè)計的影響

文章標題:技巧:輕松搞定Linq插入數(shù)據(jù)問題
分享鏈接:http://uogjgqi.cn/article/djihcei.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流