自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

開發(fā) 后端
本文介紹了一種Linq 插入數(shù)據(jù)的典型問題與它的解決辦法,如果大家有更好的方法,也歡迎拿出來一起討論。

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

今天用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),  

插入數(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(); 

每次插入時(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 ,  

問題解決。

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

【編輯推薦】

  1. LINQ——語言級(jí)集成查詢?nèi)腴T指南
  2. LINQ查詢的目的與實(shí)現(xiàn)手段
  3. LINQ查詢表達(dá)式深入剖析
  4. 實(shí)例二:綁定到LINQ查詢的結(jié)果
  5. LINQ的演變及其對(duì)C#設(shè)計(jì)的影響
責(zé)任編輯:林琳 來源: e800技術(shù)客
相關(guān)推薦

2016-03-17 17:35:15

云容器虛擬化管理Docker

2009-09-17 08:47:00

Linq插入數(shù)據(jù)

2024-09-10 10:04:47

2023-11-13 08:16:08

MySQL數(shù)據(jù)數(shù)據(jù)庫

2016-09-09 01:07:06

數(shù)據(jù)中心容量規(guī)劃數(shù)據(jù)中心

2009-11-13 17:32:37

2017-05-11 15:01:43

Androidweb布局

2009-12-11 15:37:58

Linux日志處理

2022-09-16 08:04:25

阿里云權(quán)限網(wǎng)絡(luò)

2009-09-15 23:21:17

Linq插入數(shù)據(jù)

2015-03-10 11:34:22

SQL Server數(shù)據(jù)匯總ROUPBY

2022-04-28 18:47:04

Pandas函數(shù)Python

2009-04-27 11:17:51

網(wǎng)絡(luò)管理子網(wǎng)劃分

2009-10-23 17:51:51

Oracle用戶密碼

2010-09-17 14:04:14

JVM內(nèi)存設(shè)置

2025-02-07 08:39:32

Shell部署測(cè)試

2019-07-09 08:23:07

數(shù)據(jù)安全旅游網(wǎng)絡(luò)安全

2024-12-27 08:39:10

2019-12-30 16:20:43

微信視頻移動(dòng)應(yīng)用

2024-09-09 16:50:21

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)