ASP. net mvc Entity Framework getting started tutorial and source code, mvcentity

Source: Internet
Author: User

ASP. net mvc Entity Framework getting started tutorial and source code, mvcentity
The main purpose of this article is

1. describes how to use Entity Framework Power Tools.

2. Entity Framework quick door

Lab environment:

OS: Windows Server 2012, Windows 7

DE: VS2013 + MVC 6.0 + Entity Framework + SQL Server 2012

Preparations

To generate a POCO data class and database context based on an existing database, you must use the Entity Framework Power Tools (a Code First reverse engineering tool) Extension of Visual Studio ). Enter "Entity Framework Power" in the Visual Studio extension to search for the latest extension.

 

Var Courses = db. courses. where (c => c. title = "Physics "). orderBy (c => c. title); foreach (var c in Courses) {Console. writeLine (c. title );}Add

Models.Course model = new Course();model.Title = "test";model.DepartmentID = 1;model.CourseID = 4;db.Courses.Add(model);db.SaveChanges();
Modify
Models.Course model = new Course();var result = (from r in db.Courses          where r.Title.StartsWith("test")          orderby r.Title descending          select r).FirstOrDefault();model = (Course)result;model.Credits = 4;model.Title = "good job";db.SaveChanges();
Delete
var result = from r in db.Courses      where r.CourseID == 4      select r;foreach (var c in result)    {     db.Courses.Remove(c);    }db.SaveChanges();

 

Simple Function compute (count, min, max, sum)
var result = (from r in db.StudentGrades        where r.StudentID == 2        select r).Sum(p=>p.Grade);Console.WriteLine(result);var result = (from r in db.StudentGrades        where r.StudentID == 2        select r).Max(p => p.Grade); Console.WriteLine(result);

 

Paging Data Query
var result = (from r in db.People    orderby r.LastName ascending    select r).Skip(5).Take(5);foreach(var c in result)     {      Console.WriteLine(c.LastName);     }

 

Group
var ss = from r in db.StudentGrades    orderby r.StudentID descending    group r by new { r.StudentID, r.CourseID } into g    select new    {        g.Key,        grade = g.Sum(r => r.Grade)    };foreach (var r in ss)    {    string output = "StudentID:" + r.Key.StudentID + " CourseID:" + r.Key.CourseID+" grade"+r.grade;    Console.WriteLine(output);    }

 

Complex association query
var d = db.Database.SqlQueryForDynamic(@"SELECT  c.CourseID,d.[Name] as department,c.TitleFROM  Course c INNER JOIN Department d ON c.DepartmentID = d.DepartmentID");   foreach (dynamic item in d)   {    var s = item.Title;Console.WriteLine(s);    }Console.ReadLine();

Welcome to The Penguin Group 238473238

Http://files.cnblogs.com/williamzhu/EFPowerToolsSample.zip

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.