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