2012-06-23 69 views
0

我在使用linq插入多行時遇到了問題。 我有2個表,,Linq多行插入

[訂閱表

enter image description here

和 [信息]表

  • 的MessageID(INT,isIdentity)
  • 發件人
  • 接收機
  • 內容

我想在SUBCRIBE表中的所有用戶名,其SUBCRIBE爲「Rock」的範疇 從我這裏得到一個相同的消息.. 我現在的代碼是給一個錯誤

var subcribe = from s in database.Subscribes 
        where s.SubscribeCategory == "Rock" 
        select s.UserName; 


    foreach (string s in subcribe) 
    { 
     Message msg = new Message(); 
     msg.MsgContent = "Text Here"; 
     msg.Sender = this.User.Identity.Name; 
     msg.Receiver = s; 
     database.Messages.InsertOnSubmit(msg); 
    } 
    database.SubmitChanges(); 

任何人都可以在這方面幫助查詢? 感謝前..

問候..

+5

可以包括錯誤嗎? –

+1

@yuma什麼是錯誤? – freebird

+0

frl​​llow:http://stackoverflow.com/questions/854971/how-to-insert-multiple-rows-in-a-foreach-command-with-linq – KF2

回答

2

我可以看到你沒有設置主鍵列在表Message所以有可能是一個機會,你收到此錯誤:

Can't perform Create, Update or Delete operations on Table(Message) because it has no primary key.

爲此,您必須在Message表中添加主鍵,然後重構DataClasses.dbml