Skip to main content
 首页 » 编程设计

c#之如何使用 Entity Framework 和 Asp.Net 为 CRUD 操作编写 Linq 查询

2024年02月20日17thcjp

我将 EntityFramework(EF) 与 Asp.Net 结合使用来创建一个网站,因为我已经创建了 .edmx 和 .tt 以及 DBContext。

此外,我在我的 repo 类中使用此方法完成了获取所有记录,

    StudentManagementEntities _db; 
    public Repo() 
    { 
        _db = new StudentManagementEntities(); 
    } 
 
    public object GetAllStudents() 
    { 
        return _db.People.Select(s => s).ToList(); 
    } 

我不知道如何进行插入、更新、删除等其他操作,

有人会告诉我 linq 或给我任何示例链接...

请您参考如下方法:

Add, Update and Delete Objects in Entity Framework 4.0

//插入

public void AddStudent(People s) 
{ 
    _db.People.Add(s); 
    _db.SaveChanges(); 
} 

//删除

public void DeleteStudent(People s) 
{ 
    _db.People.Remove(s); 
    _db.SaveChanges(); 
} 

//编辑

public void EditStudent(People s) 
{ 
    var people = _db.People.First( p=> p.ID == s.ID); // Replace ID with primary key 
 
  // Copy all properties from s to people 
 
    _db.SaveChanges(); 
}