Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to add the sql command with entity using personentities and entity.How do I add the following query entity?

What I have tried:

SQL
INSERT INTO [dbo].[PersonBranch](BRANCHNAME,BRANCHCODE,BRANCHDEFINE,DEFINENUMBER)SELECT DISTINCT(BRANCHNAME,BRANCHCODE,1,0) FROM [dbo].[PersonBranchDefine] WHERE BRANCHCODE NOT IN ('100','200')
Posted
Updated 26-Aug-19 10:51am
v3
Comments
[no name] 25-Aug-19 8:33am    
Is it code first or database first?
Member 14169626 25-Aug-19 11:15am    
Code First
[no name] 25-Aug-19 17:18pm    
https://stackoverflow.com/questions/8835434/insert-data-using-entity-framework-model

1 solution

For entity:
C#
int[] codestofind = new {100, 200}
PersonBranch[] pbs = context.PersonBranchDefine
    .Where(pbd=> pbd.BranchCodes.Any(bc=>codestofind.Contains(bc.BranchCode))
    .Select(pbd=>new PersonBranch(pbd.BranchName, pbd.BranchCode,1,0))
    .ToList();
context.PersonBranches.AddRange(pbs);
context.SaveChanges();


See:
DbSet<TEntity>.Add(TEntity) Method (System.Data.Entity) | Microsoft Docs[^]
DbContext.SaveChanges Method (System.Data.Entity) | Microsoft Docs[^]

[EDIT]
It might be helpful too: Tip 8 – How to write ‘WHERE IN’ style queries using LINQ to Entities – Meta-Me[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900