本篇文章為大家展示了Linq中怎么插入數(shù)據(jù),內(nèi)容簡(jiǎn)明扼要并且容易理解,絕對(duì)能使你眼前一亮,通過(guò)這篇文章的詳細(xì)介紹希望你能有所收獲。
讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來(lái)自于我們對(duì)這個(gè)行業(yè)的熱愛(ài)。我們立志把好的技術(shù)通過(guò)有效、簡(jiǎn)單的方式提供給客戶,將通過(guò)不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:域名注冊(cè)、網(wǎng)絡(luò)空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、燈塔網(wǎng)站維護(hù)、網(wǎng)站推廣。
要插入數(shù)據(jù)的表結(jié)構(gòu)是:
create table RSSFeedRight ( FeedId int Foreign Key (FeedId) References RSSFeed(FeedId) NOT NULL , -- FeedId , UserId int Foreign Key (UserId) References UserInfo(UserId) NOT NULL , -- UserId , RightValue bigint NOT NULL Primary key (UserId, FeedId), )
Linq插入數(shù)據(jù)的代碼:
RSSFeedRight feedRight = new RSSFeedRight(); feedRight.UserId = userId; feedRight.FeedId = feedId; feedRight.RightValue = 0 ; _Db.RSSFeedRights.InsertOnSubmit(feedRight); _Db.SubmitChanges();
每次Linq插入數(shù)據(jù)時(shí)都提示說(shuō)FeedId 不能插入空值,郁悶的不行,分明是給了非空值的!
后來(lái)仔細(xì)檢查,發(fā)現(xiàn)這個(gè)RSSFeedRight 實(shí)體類中居然還有兩個(gè)指向UserInfo 和 RSSFeed 表的字段,后來(lái)逐漸感覺(jué)到是外鍵設(shè)置問(wèn)題引起的。立即通過(guò)google 搜 "linq foreign key insert",發(fā)現(xiàn)有不少人遇到相同問(wèn)題,找到其中一篇帖子,其中關(guān)于這個(gè)問(wè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.
也就是說(shuō)我們不能將主鍵設(shè)置為和外鍵相同,否則就會(huì)出問(wèn)題。找到問(wèn)題所在,就好辦了,將表結(jié)構(gòu)進(jìn)行如下修改:
create table RSSFeedRight ( Id int identity ( 1 , 1 ) NOT NULL Primary Key , FeedId int Foreign Key (FeedId) References RSSFeed(FeedId) NOT NULL , -- FeedId , UserId int Foreign Key (UserId) References UserInfo(UserId) NOT NULL , -- UserId , RightValue bigint NOT NULL , )
上述內(nèi)容就是Linq中怎么插入數(shù)據(jù),你們學(xué)到知識(shí)或技能了嗎?如果還想學(xué)到更多技能或者豐富自己的知識(shí)儲(chǔ)備,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
新聞名稱:Linq中怎么插入數(shù)據(jù)
當(dāng)前URL:http://www.rwnh.cn/article20/jiedjo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、虛擬主機(jī)、商城網(wǎng)站、App設(shè)計(jì)、網(wǎng)站策劃、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)