What is the difference between creating a Context with Dbcontext and Datacontext?

Asked

Viewed 1,303 times

3

What is the difference between creating a Context with Dbcontext and Datacontext? Is there any difference in performance or best practice between one or the other?

See the examples below;

namespace _DBContext.DBase.Banco
{
    public class dbContext : using System.Data.Entity.DbContext
    {
        public dbContext()
            : base("EntityConn")
        {
            Database.SetInitializer<dbContext>(null);
        }

        public DbSet<Tabela_DocsIten> Tabela_DocsIten { get; set; }
    }
}

[global::System.Data.Linq.Mapping.DatabaseAttribute(Name="MeuBD")]
public partial class ModeloColDataContext : System.Data.Linq.DataContext
{
    public System.Data.Linq.Table<Tabela_DocsIten> Tabela_DocsItens
    {
        get
        {
            return this.GetTable<Tabela_DocsIten>();
        }
    }
}

2 answers

2

The Dbcontext is a newer class and represents a combination of Work Unit and Repository standards and can be used to perform queries in the Database.

The Datacontext is an older class and represents the entry point for performing LINQ to SQL.

We can use both, but as Voce this using EF Voce must use Dbcontext. In practice auto complete gets much more simplified (fewer attributes than Datacontext + its domain attributes) using Dbcontext compared to Datacontext. Reference

2


DbContext represents a combination of standards Unit-Of-Work and Repository and allows you to query a database and group the changes that will be saved back to storage as a unit. DbContext is conceptually similar to ObjectContext.

Commonly used with a derivative type containing DbSet<TEntity>, these sets are automatically initialized when the derived class instance is created. This behavior can be modified by applying the attribute SuppressDbSetInitializationAttribute any derivative context class or individual properties in the class.


DataContext represents the main input point for the LINQ to SQL framework. O DataContext is the source of all entities mapped into a database connection. It controls the changes made to all recovered entities and maintains an "identity cache" ensures that the recovered entities more than once are represented by using the same object instance.

In general, an instance DataContext is designed to last for a "working drive" however, the application defines this term. DataContext is lightweight and not expensive to create. A typical LINQ to SQL application creates DataContext instances in the scope of method or as a member of the short-term classes representing a logical set of related database operations.


References:

  • Then it is better to use Datacontext than to use Dbcontxt ?

  • 1

    @Gokussjgod, it depends. No one can definitely say that one is better than the other, because it depends on your project, questions like: "What is the size/budget of your project? How many developers will work on it?" should be taken into account. I left in the reply, what is written in the documentation =)

  • 1

    Very interesting your last reference.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.