//新增
testEntities tt = new Models.testEntities();
var person1 = new person{value = "小名" ,id="3"};
tt.person.Add(person1);
tt.SaveChanges();
//刪除
testEntities tt = new Models.testEntities();
var person1 = new person {lessonid = "1" };
tt.Entry(person1).State = EntityState.Deleted;
tt.SaveChanges();
//修改
testEntities tt = new Models.testEntities();
var person1 = new person {value="小陳" ,lessonid="1"};
tt.Entry(person1).State = EntityState.Modified;
tt.SaveChanges();
//查詢
testEntities tt = new Models.testEntities();
var linq = from p in tt.person
join l in tt.lesson on p.lessonid equals l.id
select new AllPerson() { id = p.id , lesson1 = l.lesson1 , lessonid = p.lessonid , lid = l.id , name = p.name};
//linq.FirstOrDefault<person>();
return View(linq);
public class AllPerson
{
public int id { get; set; }
public string name { get; set; }
public Nullable<int> lessonid { get; set; }
public int lid { get; set; }
public string lesson1 { get; set; }
}
use a class to receive data
@using MVC.Models
@model IEnumerable<MVC.Models.AllPerson> /*型別要記得跟Controller傳過來的一樣*/
@{
ViewBag.Title = "Home Page";
}
<h2>This is a heading</h2>
@foreach (AllPerson allperson in Model)
{
<h2>Student Id: @allperson.id , Student Name: @allperson.name , Lesson: @allperson.lesson1</h2>
}
class to cshtml