Updating records using linq

08-Nov-2015 13:35 posted by spyroduels | Leave a comment

Read Key(true); } //static string Get Connection String(string server Name) //{ // System.

Connection String; //} static void Save Person(Person p) { if (p. If you provide person Id as 0 this function will insert a new row else it will update existing row. Now, Office Assignment = new Office Assignment { Location = "Jamshedpur" } } ); context. Read Key(true); //Person p = new Person {First Name ="xuz", Last Name = "abdefg" }; //p. When I tried to call above function to update Person with ID 1, I encountered with below exception To get rid of above exception, you need to call attach with original entity as well. Text; namespace Relatedtable { class Program { static Data Classes1Data Context context; static void Main(string[] args) { context = new Data Classes1Data Context(); context. Office Assignment = new Office Assignment { Location = "Mumbai" }; //p. Office Assignments select r; foreach (var a in res1) { Console. We need to pass modified entity as well as original entity to make a call. Office Assignment = new Office Assignment { Location = "Delhi" }; //p. Submit Changes(); Person p = new Person { Person ID = 1, First Name = "Dhananjay " , Last Name="Kumar"}; Save Person(p); var result = from r in context. So I created a function as below, And while attaching you need to call above function as below Full source code is as below, using System; using System. Persons select r; foreach (var r in result) { Console. Submit Changes(); } } static Person Get Original(int id) { Data Classes1Data Context db = new Data Classes1Data Context(); return db.

Recently, I have discussed about getting started linq to sql in c#

That articles also covered for creating select query for retrieving both single and multiple results.

Scott Stanfield – Scott Stanfield is the CEO of Vertigo Software, Inc., the 2008 Microsoft "Partner of the Year". Scott is a proud member of the Microsoft Regional Director community and participates in the Microsoft Partner Architect Council.

Now you can ask questions directly to me on my new site ASPForums.

Net Question Answer site, specially started to allow visitors of this site ask their doubts and queries. Just click the Ask Now button and ask your question now.

If you have requirement to insert a Person without any Office Assignment then it is quiet straight forward task like below If you need to insert Office Assignment while inserting Person, you can create instance of office assignment and insert along with Person like below, Now I have created function to perform both Insert and update operation. Insert On Submit( new Person { First Name = "The ", Last Name = "Poet", Hire Date = Date Time.

Leave a Reply

  1. princess diana dating 02-Nov-2015 14:37

    We lived in eastern Nebraska, in an area of mid-sized farms.

  2. Naked missouri no e mail required 13-Nov-2015 12:35

    Even though this will not have a big impact, it is still something we (webmasters) should really look into.

panama ladies dating