校园春色亚洲色图_亚洲视频分类_中文字幕精品一区二区精品_麻豆一区区三区四区产品精品蜜桃

主頁 > 知識庫 > Linq to SQL 插入數據時的一個問題

Linq to SQL 插入數據時的一個問題

熱門標簽:廣東防封卡外呼系統(tǒng)原理是什么 電銷機器人公司 需要哪些牌照 分享百度地圖標注多個位置 長沙智能外呼系統(tǒng) 外呼系統(tǒng)改進 菏澤語音電銷機器人加盟公司 地圖標注牌 湖南電腦外呼系統(tǒng)平臺 知名電銷機器人價格

復制代碼 代碼如下:

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

插入數據的代碼

RSSFeedRight feedRight = new RSSFeedRight();
feedRight.UserId = userId;
feedRight.FeedId = feedId;
feedRight.RightValue = 0 ;
_Db.RSSFeedRights.InsertOnSubmit(feedRight);
_Db.SubmitChanges();
每次插入時都提示說FeedId 不能插入空值,郁悶的不行,分明是給了非空值的!
后來仔細檢查,發(fā)現(xiàn)這個RSSFeedRight 實體類中居然還有兩個指向UserInfo 和 RSSFeed 表的字段,后來逐漸感覺到是外鍵設置問題引起的。立即通過google 搜 "linq foreign key insert"
發(fā)現(xiàn)有不少人遇到相同問題
找到其中一篇帖子
http://social.msdn.microsoft.com/Forums/en-US/linqprojectgeneral/thread/e14f76ec-0ffe-4dd1-893c-6a4c8440c54a
其中關于這個問題是這樣描述的
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.
也就是說我們不能將主鍵設置為和外鍵相同,否則就會出問題。找到問題所在,就好辦了,將表結構進行如下修改

復制代碼 代碼如下:

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

問題解決。
老兵遇到新問題,技術不經常更新就要老化。

標簽:泉州 天水 商洛 呼和浩特 西寧 美容院 珠海 福建

巨人網絡通訊聲明:本文標題《Linq to SQL 插入數據時的一個問題》,本文關鍵詞  Linq,SQL,插入,數據,時,的,;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《Linq to SQL 插入數據時的一個問題》相關的同類信息!
  • 本頁收集關于Linq to SQL 插入數據時的一個問題的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 迁安市| 炎陵县| 筠连县| 高陵县| 原平市| 永安市| 长岛县| 林芝县| 封开县| 兴山县| 民县| 南宁市| 宜兰市| 红河县| 尼玛县| 称多县| 武川县| 金山区| 峡江县| 师宗县| 汉川市| 天镇县| 竹溪县| 汽车| 喀喇沁旗| 车致| 卢龙县| 灵山县| 博爱县| 弋阳县| 商洛市| 濮阳县| 营山县| 茌平县| 乐亭县| 黄山市| 理塘县| 莱西市| 林州市| 金堂县| 张掖市|