linq
  1. linq-datacontext-and-entity-classes

DataContext and Entity Classes in LINQ to SQL

DataContext is the core component of LINQ to SQL, and it represents the connection to the database and the Relational Data Mapper that maps relational data to LINQ entity objects. Entity classes are the objects that map to database tables.

Syntax

To create a DataContext instance, we need to create a new class that derives from the DataContext class and set the connection string in the constructor.

public class MyDataContext : DataContext {
    public MyDataContext(string connectionString) : base(connectionString) {}
    public Table<MyEntity> MyTable { get { return GetTable<MyEntity>(); } }
}

To create an entity class, we create a new class and add attributes to properties that map to database fields.

[Table(Name="myTable")]
public class MyEntity {
    [Column(Name="id", IsPrimaryKey=true)]
    public int Id { get; set; }
    
    [Column(Name="name")]
    public string Name { get; set; }
}

Example

Here's an example of using DataContext and Entity Classes in a LINQ to SQL application.

// Create DataContext object
MyDataContext context = new MyDataContext("Data Source=myServer;Initial Catalog=myDatabase;Integrated Security=True");

// Query data from database
var items = from item in context.MyTable
            where item.Name.Contains("foo") && item.Id > 10
            select item;

// Insert data into database
MyEntity newItem = new MyEntity { Id = 20, Name = "bar" };
context.MyTable.InsertOnSubmit(newItem);
context.SubmitChanges();

In the above example, we create a new MyDataContext object with a connection string. We then query data from the MyTable table using LINQ to SQL. Finally, we insert a new MyEntity object into the MyTable table using DataContext's InsertOnSubmit() method and submit changes to the database using the SubmitChanges() method.

Output

The output of the above code depends on the data in the MyTable table. The query will return a collection of MyEntity objects that match the condition specified in the LINQ query. The insert statement will add a new row to the MyTable table with the Id and Name specified in the newItem object.

Explanation

DataContext is responsible for creating a connection to the database and translating LINQ to SQL queries into SQL statements. The Entity classes represent the tables of the database, columns, and their data types.

In the example above, we created a MyDataContext object with a connection string and queried data from the MyTable table using a LINQ to SQL query. We then inserted a new object into the MyTable table using the InsertOnSubmit() method. Finally, we submitted the changes to the database using the SubmitChanges() method.

Use

DataContext and Entity Classes in LINQ to SQL provide an easy way to map database tables to classes in C#. This mapping makes it easier to perform SQL queries using LINQ syntax compared to writing raw SQL. The DataContext object also manages the database connection and automatically generates the SQL statements needed to execute the LINQ to SQL queries.

Important Points

  • DataContext represents the connection to the database and Relational Data Mapper.
  • Entity classes are objects that map to database tables.
  • Entity classes must include attributes to map their properties to the database fields.
  • DataContext must be created with a connection string.
  • Inserting, updating, and deleting data should be done through the DataContext object.

Summary

DataContext and Entity Classes are essential components of LINQ to SQL and provide an easy way to map database tables to classes in C#. DataContext represents the connection to the database and Relational Data Mapper, while Entity classes map to database tables, and their properties to the database fields. We've seen how to create a DataContext object and Entity class and how to perform database operations using LINQ to SQL. By using DataContext and Entity Classes, we can simplify database access in our applications and make it easier to perform complex SQL queries.

Published on: