C #------entityframework entity Load Database

Source: Internet
Author: User

I. SQL Server database creation table company, containing id,cname,isenabled three columns

Two. (1) vs Create a new Dxapplication project, named Companymanagesystem

(2) Under Project, right click "Manage NuGet Package", "Browse", "EntityFramework", click "Install"

(3) Create a new class PSContext.cs

Namespace companymanagesystem{    class Pscontext:dbcontext    {public        pscontext (): Base ("Name= Pscontext ") {} public        virtual dbset<company> companys {get; set;}        protected override void onmodelcreating (dbmodelbuilder modelBuilder)        {        }}    }    

(4) Create a new class Company.cs

namespace companymanagesystem.model{    [Table ("Company")] public    class company    {        public Guid ID {get; set;}        Public String CName {get; set;}        public bool IsEnabled {get; set;      }}}

(5) Code to add a database in app. Config

 <?xml version= 1.0   " Encoding="  utf-8   "?><configuration> <connectionStrings> <add name="  pscontext   " connectionstring="  data source=x450v-pc;initial catalog=date;integrated security=true; Multipleactiveresultsets=true; App=entityframework   providername="   system.data.sqlclient   "/> </connectionstrings></configuration> 

(6) Add the form Frm_showinfo.cs (note: The name in the Filedname is the same as the column name of the database, (name) inside the name to try not to take name)

To add a control:

 namespace   companymanagesystem{ Span style= "color: #0000ff;" >public  partial  class   Frm_showinfo:form {Pscontext _db  = new   Pscontext ();  public   Frm_showinfo () {Initia        Lizecomponent ();  private  void  frm_showinfo_load (object   sender, EventArgs e) {Compan Ygrid.datasource  = _db.companys.asnotracking ().        ToList (); }    }}

(7) Start Frm_showinfo.cs window in Program.cs

namespacecompanymanagesystem{Static classProgram {/// <summary>        ///The main entry point for the application. /// </summary>[STAThread]Static voidMain () {application.enablevisualstyles (); Application.setcompatibletextrenderingdefault (false);            Bonusskins.register ();            Skinmanager.enableformskins (); UserLookAndFeel.Default.SetSkinStyle ("DevExpress Style"); Application.Run (Newfrm_showinfo ()); }    }}

C #------entityframework entity Load Database

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.