国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 數(shù)據(jù)庫(kù) > SQL Server > 正文

Linq to SQL 插入數(shù)據(jù)時(shí)的一個(gè)問(wèn)題

2024-08-31 00:59:23
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

復(fù)制代碼 代碼如下:


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


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

RSSFeedRight feedRight = new RSSFeedRight();
feedRight.UserId = userId;
feedRight.FeedId = feedId;
feedRight.RightValue = 0 ;
_Db.RSSFeedRights.InsertOnSubmit(feedRight);
_Db.SubmitChanges();
每次插入時(shí)都提示說(shuō)FeedId 不能插入空值,郁悶的不行,分明是給了非空值的!
后來(lái)仔細(xì)檢查,發(fā)現(xiàn)這個(gè)RSSFeedRight 實(shí)體類中居然還有兩個(gè)指向UserInfo 和 RSSFeed 表的字段,后來(lái)逐漸感覺到是外鍵設(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)行如下修改

復(fù)制代碼 代碼如下:


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


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

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 桐庐县| 沂源县| 广饶县| 缙云县| 乐东| 新余市| 曲松县| 如皋市| 英山县| 大渡口区| 双鸭山市| 江安县| 宝鸡市| 蓬安县| 东兰县| 九江县| 花莲县| 广元市| 阿拉尔市| 玛曲县| 绥芬河市| 忻城县| 万盛区| 临潭县| 阿尔山市| 秀山| 桃园市| 张北县| 曲沃县| 邵阳县| 若尔盖县| 广州市| 兴海县| 嵩明县| 和田县| 法库县| 宝应县| 沽源县| 洛川县| 平乐县| 湖口县|