日本综合一区二区|亚洲中文天堂综合|日韩欧美自拍一区|男女精品天堂一区|欧美自拍第6页亚洲成人精品一区|亚洲黄色天堂一区二区成人|超碰91偷拍第一页|日韩av夜夜嗨中文字幕|久久蜜综合视频官网|精美人妻一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
淺析Linq插入數(shù)據(jù)的實(shí)現(xiàn)方法

Linq插入數(shù)據(jù)是一項(xiàng)基本的操作,雖然很基本,但是在操作的時(shí)候還是避免不了出現(xiàn)一些問題,現(xiàn)在我們就來看一個(gè)典型問題的解決辦法。

成都創(chuàng)新互聯(lián)一直通過網(wǎng)站建設(shè)和網(wǎng)站營銷幫助企業(yè)獲得更多客戶資源。 以"深度挖掘,量身打造,注重實(shí)效"的一站式服務(wù),以做網(wǎng)站、成都網(wǎng)站建設(shè)、移動(dòng)互聯(lián)產(chǎn)品、全網(wǎng)整合營銷推廣服務(wù)為核心業(yè)務(wù)。10多年網(wǎng)站制作的經(jīng)驗(yàn),使用新網(wǎng)站建設(shè)技術(shù),全新開發(fā)出的標(biāo)準(zhǔn)網(wǎng)站,不但價(jià)格便宜而且實(shí)用、靈活,特別適合中小公司網(wǎng)站制作。網(wǎng)站管理系統(tǒng)簡單易用,維護(hù)方便,您可以完全操作網(wǎng)站資料,是中小公司快速網(wǎng)站建設(shè)的選擇。

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

要插入數(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),   
  6. )  

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

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

每次Linq插入數(shù)據(jù)時(shí)都提示說FeedId 不能插入空值,郁悶的不行,分明是給了非空值的!

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

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è)置為和外鍵相同,否則就會(huì)出問題。找到問題所在,就好辦了,將表結(jié)構(gòu)進(jìn)行如下修改:

 
 
 
  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 ,  
  7. )  

Linq插入數(shù)據(jù)問題解決。如此看來,老兵會(huì)遇到新問題,技術(shù)不經(jīng)常更新就要老化。


本文名稱:淺析Linq插入數(shù)據(jù)的實(shí)現(xiàn)方法
轉(zhuǎn)載來于:http://www.dlmjj.cn/article/cdhheeh.html