Create DbContext and dbcontext

Source: Internet
Author: User

Create DbContext and dbcontext
Go back to the General Directory, "Step by Step Using the ABP framework to build a formal project series tutorial"

The previous article introduced "create entity". This article describes how to create DbContext. Gentle story:

When talking about DbContext, developers who often use the DbFirst mode are no longer familiar with it. EntityFramework depends on this role. It represents a session connected to a database and provides functions such as query, status tracking, and storage.

Another important object is DbSet, which provides set operations for object types, such as Add, Attach, and Remove. It inherits DbQuery, so it can provide the query function.

Zhixin:

The ABP framework creates a DbContext template for us. This template is in my projectInfrastructure LayerThe ChargeStationDbContext. cs file (of course, if your project name is XX, its name is XXDbContext. cs), for example:

Start:

Double-click the file to start editing.

 

After opening the file, a todo prompt is displayed, indicating "define IDbset for your entities ". Okay. Next, Let's define the following code here.

        public virtual IDbSet<Cities> Cities { set; get; }        public virtual IDbSet<CoinsCollectionDataLogs> CoinsCollectionDataLogs { set; get; }        public virtual IDbSet<DeviceCoinsRecords> DeviceCoinsRecords { set; get; }        public virtual IDbSet<DeviceNoDataSets> DeviceNoDataSets { set; get; }        public virtual IDbSet<MoneyBoxRecycleRecords> MoneyBoxRecycleRecord { set; get; }        public virtual IDbSet<MonthEndReports> MonthEndReports { set; get; }        public virtual IDbSet<Operators> Operators { set; get; }        public virtual IDbSet<Orders> Orders { set; get; }        public virtual IDbSet<Provinces> Provinces { set; get; }        public virtual IDbSet<RefundOrders> RefundOrders { set; get; }        public virtual IDbSet<Stations> Stations { set; get; }        public virtual IDbSet<TerminalAlertLogs> TerminalAlertLogs { set; get; }        public virtual IDbSet<TerminalDevices> TerminalDevices { set; get; }        public virtual IDbSet<TerminalMaintainLogs> TerminalMaintainLogs { set; get; }        public virtual IDbSet<TransDetails> TransDetails { set; get; }

Again, pay attention to the prompt "define IDbset for your entities", because some Users and Roles starting with "ABC" are also defined in my entities, so I will continue to define their IDbSet here. After definition, send

An error is reported. The original statement is as follows: "The key word 'new' is required on 'roles' because it hides property IDbSet <TRole> AbpZeroDbContext <TTenant, TRole, TU

If you want to define this attribute in the parent class, you must add a new keyword to hide the attributes of the parent class with the same name ". It can be seen that the parent class has defined tenant, user, and

Role, so we can use it directly. Let's take a look at the source code in the ABP module-Zero:

It has helped us so much, that is where it is powerful. Therefore, we can infer that we do not need to define the IDbSet of these items here. We only need to define the entities of our own business. Correspondingly, these entities created in the previous article should also be deleted.

 

Okay, here we have defined IDbSet, so how does AbpZeroDbContext connect to the database? Where is its connection string?

Note that the constructor of this class calls the constructor of the parent class and transmits the "Default" string to the constructor of the parent class. In fact, this string is transmitted from the AbpZeroDbContext class to its parent class AbpContext, and then to the parent class DbContex for database connection. Finally, you can connect to the database by reading the connection string whose name attribute value is "Default" in Web. Config.

<connectionStrings>    <add name="Default" connectionString="Server=localhost; Database=ChargeStation; Trusted_Connection=True;" providerName="System.Data.SqlClient" />  </connectionStrings>

Now, let's talk about creating DbContext today. It's time to have lunch! Next we will talk about "create database migration". Let's unveil the secrets of the ABP framework in a small step. Stay tuned!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.