Self-study EF some small notes

Source: Internet
Author: User

have been using Dhhelper to do mvc, feel good cottage, also not very useful. decided to start learning ef.

Don't say much nonsense to start taking notes .....

EF is the instantiation of database tables, stored procedures, views, and the manipulation of data instances by inheriting a class of DbContext .

Create EF:

The entity connection string is generally selected "yes".

Only the tables, views, and stored procedures that need to be selected are checked here.

Then the next step, the next step, is to complete the creation of EF.

EF is typically under the model folder of the MVC project, which is the model in MVC.

Using EF:

With EF, the controller can manipulate the database directly by invoking a method that inherits the DbContext class.

Inquire:

Controller section

new ksoa_lsddentities ();     // instantiating a DbContext object            list<spkfk> ls = db.spkfk.AsQueryable (). ToList ();   // querying tables, returning a collection            of tables return View (LS);

View section

@model list<eftest.models.spkfk>@{    "~/views/template/_tamplate.cshtml" ;} <table>    in  Model)     {        <tr>            <td>                @ I.spmch            </td>        </tr>    }</table>

New:

Controller section

Testefentities db =NewTestefentities ();//instantiating a DbContext objectEftable e =NewEftable ();//creating a new instance objectE.goodscode ="YC01";//New object property assignment ValueE.goodsname ="Ethanol";//New object property assignment ValueDb. Eftables.add (e);//new objects added to the instantiated objects collectionDb. SaveChanges ();//Save the modified collection to the database
SaveChanges () This method is very important , the database additions and deletions after the operation is SaveChanges () to save the operation to the database,SaveChanges () returns the number of rows affected by the database, How many data is affected by the database after this operation is executed .

Additions and deletions do not need to be in view to do the operation.

Modify:

Controller section

new testefentities ();   // instantiating a DbContext object            Eftable e = db. Eftables.find (1); // Locate and instantiate the data object you want to modify by using the primary key            " after modification ";        // Assigning values            to properties that need to modify columns Db. SaveChanges ();       // save operation to database

The modification requires the Find method to locate the data object that needs to be modified and instantiate the object.

Delete:

Controller section

new testefentities ();   // instantiating a DbContext object            Eftable e = db. Eftables.find (1);  // Locate and instantiate the data column object that you want to delete            Db. Eftables.remove (e);      // removes the specified Delete object from the collection            of data table objects Db. SaveChanges ();       //

Deleting is similar to modifying, removing the need to call the Remove method execution.

Self-study EF some small notes

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.