In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to solve LINQ DataContext problems". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
LINQ DataContext class
Represents the main entry point of the LINQ to SQL framework.
DataContext is lightweight and does not require a lot of overhead to create it. A typical LINQ to SQL application creates DataContext instances within the scope of a method, or creates those instances as members of short-lived classes that represent logical collections of related database operations.
DataContext is the primary channel for connecting to the database, retrieving objects from it, and committing changes back to the database. Using DataContext is like using ADO.NET SqlConnection. In fact, DataContext is initialized with the connection or connection string you provided.
The purpose of DataContext is to convert your request for an object into an SQL query to be executed against the database, and then assemble the query results into an object. DataContext implements language-integrated queries (LINQ) by implementing the same operator patterns as standard query operators such as Where and Select.
/ / entity class
[Table (Name = "Student")]
Public class Student
{
[Column (IsPrimaryKey = true)]
Public int ID
[Column]
Public string StuName
[Column]
Public bool Sex
[Column]
Public int Age
}
/ / strongly typed DataContext
Public class TestDB: DataContext
{
Public TestDB (string constr)
: base (constr) {
}
Public Table Student
Public Table Scores
}
/ / call
TestDB Test = new TestDB (constr)
Var stu = from student in Test.Student
Select student
Foreach (var st in stu)
{
Console.WriteLine ("serial number: {0}, sexual name: {1}, age: {2}, gender: {3}"
St.ID, st.StuName, st.Sex, st.Age)
}
Each database table is represented as a collection of Table that can be used by the GetTable method (by identifying it by using an entity class).
The way to do this is to declare a strongly typed DataContext instead of relying on the basic LINQ DataContext class and GetTable methods. The strongly typed DataContext declares all Table collections as members of the context, as shown in the following example.
Strongly typed DataContext add
/ / entity class [Table (Name = "Student")] public class Student {[Column (IsPrimaryKey = true)] public int ID; [Column] public string StuName; [Column] public bool Sex; [Column] public int Age;} / / strongly typed DataContext public class TestDB: DataContext {public TestDB (string constr): base (constr) {} public Table Student; public Table Scores;} / / add TestDB Test = new TestDB (constr) Student student = new Student (); student.StuName = "Big sheet"; student.Sex = false; student. Age = 34; Test.Student.InsertOnSubmit (student); Test.SubmitChanges (); "how to solve LINQ DataContext problems" ends here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.