What are the ways to use LINQ to SQL statements?
This article mainly introduces "what are the methods of using LINQ to SQL sentences". In daily operation, I believe that many people have doubts about the use of LINQ to SQL sentences. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "what is the use of LINQ to SQL sentences?" Next, please follow the editor to study!
Simple form of LINQ to SQL statement
Description: new an object, add it to the corresponding collection using the InsertOnSubmit method, and submit it to the database using SubmitChanges ().
NorthwindDataContext db = new NorthwindDataContext (); var newnewCustomer = newCustomer {CustomerID = "MCSFT", CompanyName = "Microsoft", ContactName = "John Doe", ContactTitle = "Sales Manager", Address = "1 Microsoft Way", City = "Redmond", Region = "WA", PostalCode = "98052", Country = "USA", Phone = "(425) 555-1234", Fax = null}
Statement description: use the InsertOnSubmit method to add a new customer to the Customers table object. Call SubmitChanges to save this new Customer to the database.
LINQ to SQL statement one-to-many relationship
Note: Category and Product have an one-to-many relationship. When submitting data from Category (one side), LINQ to SQL will automatically submit data from Product (multiple terminals) together.
Var newnewCategory = newCategory {CategoryName = "Widgets", Description = "Widgets are the." }; var newnewProduct = newProduct {ProductName = "Blue Widget", UnitPrice = 34.56m, Category = newCategory}
Statement description: use the InsertOnSubmit method to add a new category to the Categories table and add a new Product object to the Products table that has a foreign key relationship with this new Category. Call SubmitChanges to save these new objects and their relationships to the database.
Many-to-many relationship of LINQ to SQL statement
Description: in a many-to-many relationship, we need to submit in turn.
Var newnewEmployee = newEmployee {FirstName = "Kira", LastName = "Smith"}; var newnewTerritory = newTerritory {TerritoryID = "12345", TerritoryDescription = "Anytown", Region = db.Regions.First ()}; var newnewEmployeeTerritory = newEmployeeTerritory {Employee = newEmployee, Territory = newTerritory}; at this point, the study of "what is the use of LINQ to SQL sentences" is over, hoping to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!