2017-05-17 118 views
0

我有我想轉換爲實體框架翻譯INSERT INTO選擇實體框架

我需要在一個運行中插入多行中的實體像什麼這個查詢做

INSERT INTO dbo.tbllistDepartment 
(
    department_sys_id, 
    department, 
    status, 
    client_sys_id, 
    lang_sys_id 
) 
SELECT @siDepartmentSysId,@siDepartment, @iiStatus, 
     @siClientSysId, lang_sys_id 
    FROM tblLanguageSettings 
    WHERE lang_sys_id <> 'Admin001' 
這個sql查詢

我已經試過,但其插入只有一行

public static void Insert(string langSysId, string clientSysId,string name, int status) 
    { 
     Crs2Model context = new Crs2Model(); 
     string SysId = MyLibrary.StringCreator.GetSysId(); 

     tblListDepartment deptObj = new tblListDepartment() 
     { 
      department_sys_id = SysId, 
      client_sys_id = clientSysId, 
      lang_sys_id=langSysId, 
      department=name, 
      status=status 
     }; 

     context.tblListDepartments.Add(deptObj); 
     context.SaveChanges(); 
    } 
+1

顯示您的DBContext名稱,我的朋友。 – Tomato32

+0

我的問題已更新 –

回答

0

首先,你需要從tbllistDepartment表過濾器的數據。然後循環瀏覽這些數據。 這是一個示例。您可以參考,我的朋友:

using (DBContext ctx = new DBContext()) 
{  

    var department = ctx.tbllistDepartment.Where(c => c.lang_sys_id != "Admin001"); 

    foreach(var item in department) 
{ 
     var obj = new tbllistDepartment() 
     { 
     department_sys_id = item.department_sys_id, 
     department = item.department, 
     status = item.status, 
     client_sys_id = item.client_sys_id, 
     lang_sys_id = item.lang_sys_id 
     } 
     ctx.tbllistDepartment.Add(obj); 
} 

    // Insert into the database. 
    ctx.SaveChanges();       
} 
+0

非常感謝你我的朋友,這是解決我的問題...希望我有15名聲譽,所以我可以投你:) –

+0

@ AhmadA.Al-Dwairi:是的,很高興能幫助你,我的朋友:)) – Tomato32