C#四种方式的增删改查

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ConsoleApp6
{
   
    class Program
    {
   
        static void Main(string[] args)
        {
   
            RbacDBEntities db = new RbacDBEntities();
            //方式1 Add
            var roles1 = from r in db.Roles
                         where r.Name.Contains("主任")
                         select r;
            //方式2
            var roles2 = db.Roles
                .Where(p => p.Name.Contains("主任"))
                .Select(p => p).ToList();
            //方式3调用方sql
            string sql = @"select * from roles  where name like '%主任%'
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
C#中使用Entity Framework (EF)进行多表的增删操作,可以按照以下步骤进行: 1. 创建实体类:首先,创建对应数据库表的实体类。每个表对应一个实体类,并在类中定义属性来映射表中的字段。 2. 创建上下文类:使用EF的Code First方式,创建一个继承自`DbContext`的上下文类。在上下文类中,通过`DbSet`属性定义每个实体类对应的数据库表。 3. 连接数据库:在应用程序的配置文件中,配置数据库连接字符串,指定要连接的数据库。 4. 增加数据:使用上下文类创建实体对象,并将其添加到相应的`DbSet`属性中,然后调用上下文类的`SaveChanges`方法来保存更。 ```csharp using (var context = new YourDbContext()) { var entity = new YourEntity() { // 设置实体属性 }; context.YourEntities.Add(entity); context.SaveChanges(); } ``` 5. 询数据:使用LINQ询语句来从数据库中检索数据。 ```csharp using (var context = new YourDbContext()) { var result = context.YourEntities .Where(e => e.SomeProperty == someValue) .ToList(); } ``` 6. 更新数据:首先询要更新的实体,然后修实体的属性,并调用上下文类的`SaveChanges`方法保存更。 ```csharp using (var context = new YourDbContext()) { var entity = context.YourEntities.Find(id); if (entity != null) { // 修实体属性 context.SaveChanges(); } } ``` 7. 删除数据:首先询要删除的实体,然后调用上下文类的`Remove`方法将实体从`DbSet`属性中移除,并调用`SaveChanges`方法保存更。 ```csharp using (var context = new YourDbContext()) { var entity = context.YourEntities.Find(id); if (entity != null) { context.YourEntities.Remove(entity); context.SaveChanges(); } } ``` 以上是使用EF进行多表增删的基本步骤,具体操作还需要根据你的实际需求和数据库结构进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值