This article describes the use of LINQ to SQL model classes
EntityFramework is a heavyweight database model, and if you want a simple, lightweight database model, it is recommended that you use the LINQ to SQL class
To create a model class
在项目中添加linq to sql类,会生成dbml文件,将你需要的表拖到此文件中即可
Querying data
="server=localhost;database=my_db;uid=sa;pwd=123";=newDataClasses1DataContext(str);// 创建数据库对象var=fromindb.Productselectitem;// 查询数据
New data
String str= "Server=localhost;database=my_db;uid=sa;pwd=123";Dataclasses1datacontext DB= New Dataclasses1datacontext(str);Product TB= New Product(); //CREATE Table ObjectTB.ProductName = "Charger";TB.Productprice = -;TB.Produceadressid = 4;DB.Product.InsertOnSubmit(TB); //Generate SQL ScriptDB.SubmitChanges(); //Execute SQL script, save data
modifying data
String str= "Server=localhost;database=my_db;uid=sa;pwd=123";Dataclasses1datacontext DB= New Dataclasses1datacontext(str);varItem= fromSinch DB.Product where s.ProductID == 7Select S;Item.FirstOrDefault().ProductName = "mobile phone charger";DB.SubmitChanges(); //Execute SQL script, save data
Delete data
String str= "Server=localhost;database=my_db;uid=sa;pwd=123";Dataclasses1datacontext DB= New Dataclasses1datacontext(str);varItem= fromSinch DB.Product where s.ProductID == 7Select S;DB.Product.DeleteOnSubmit(Item.FirstOrDefault());DB.SubmitChanges(); //Execute SQL script, save data
Use of LINQ to SQL classes