使用标记方式entry方法进行删除和修改

Dbcontext 有Entry方法 用他来改变集合里面每一项的状态

   //增   不推荐
            PeopleEntities context = new PeopleEntities();
            //context.Entry<Student>(new Student() { Name = "heyuke", Age = 18 }).State = EntityState.Added;
            //context.SaveChanges();
            //删   推荐 但必须标明主键
            //context.Entry<Student>(new Student() { Id=5,Name = "heyuke", Age = 18 }).State = EntityState.Deleted;
            //context.SaveChanges();
            //改 推荐
            context.Entry<Student>(new Student() { Id=1,Name = "hehe"}).State = EntityState.Modified;
            context.SaveChanges();
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个基于Java的简单命令行文件管理系统,支持MD、RD、MK、DEL等基本文件操作: ```java import java.util.*; public class FileManager { private static final int BLOCK_SIZE = 16; // 每个磁盘块的大小 private static final int BLOCK_NUM = 1024; // 磁盘块的总数 private static final int FAT_SIZE = BLOCK_NUM; // FAT表的大小与磁盘块总数相同 private static final Map<String, Integer> commands = new HashMap<>(); // 命令列表 private static String currentDir = "/"; // 当前目录 private static int[] fat = new int[FAT_SIZE]; // FAT表 private static List<FileEntry> directory = new ArrayList<>(); // 目录 static { commands.put("MD", 1); commands.put("RD", 1); commands.put("MK", 2); commands.put("DEL", 1); commands.put("DIR", 0); commands.put("CD", 1); commands.put("EXIT", 0); } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); init(); while (true) { System.out.print("$"); String input = scanner.nextLine().trim(); String[] tokens = input.split(" "); String command = tokens[0].toUpperCase(); if (!commands.containsKey(command)) { System.out.println("Invalid command."); continue; } int argNum = commands.get(command); if (tokens.length - 1 != argNum) { System.out.println("Invalid arguments."); continue; } switch (command) { case "MD": createDir(tokens[1]); break; case "RD": deleteDir(tokens[1]); break; case "MK": createFile(tokens[1], Integer.parseInt(tokens[2])); break; case "DEL": deleteFile(tokens[1]); break; case "DIR": showDir(); break; case "CD": changeDir(tokens[1]); break; case "EXIT": return; } } } // 初始化文件系统,包括FAT表和根目录 private static void init() { Arrays.fill(fat, -1); directory.add(new FileEntry("/", true, 0, 0)); } // 创建子目录 private static void createDir(String name) { if (directory.stream().anyMatch(f -> f.getName().equals(name))) { System.out.println("Directory already exists."); return; } int block = findFreeBlock(); if (block == -1) { System.out.println("No free space left on disk."); return; } directory.add(new FileEntry(name, true, block, directory.size() - 1)); fat[block] = -2; // -2表示目录文件 } // 删除子目录 private static void deleteDir(String name) { Optional<FileEntry> entry = directory.stream().filter(f -> !f.isDeleted() && f.getName().equals(name)).findFirst(); if (!entry.isPresent()) { System.out.println("Directory not found."); return; } FileEntry dir = entry.get(); if (!dir.isDirectory()) { System.out.println("Not a directory."); return; } if (dir.getSize() > 0) { System.out.println("Directory not empty."); return; } for (int i = 0; i < BLOCK_SIZE; i++) { fat[dir.getFirstBlock() + i] = -1; // 释放磁盘块 } dir.setDeleted(true); } // 创建空文件 private static void createFile(String name, int size) { if (directory.stream().anyMatch(f -> f.getName().equals(name))) { System.out.println("File already exists."); return; } if (size <= 0 || size > BLOCK_SIZE) { System.out.println("Invalid file size."); return; } int[] blocks = new int[size]; int prevBlock = -1; for (int i = 0; i < size; i++) { int block = findFreeBlock(); if (block == -1) { System.out.println("No free space left on disk."); // 回滚之前的分配 for (int j = 0; j < i; j++) { fat[blocks[j]] = -1; } return; } if (prevBlock != -1) { fat[prevBlock] = block; } blocks[i] = block; prevBlock = block; } directory.add(new FileEntry(name, false, blocks[0], directory.size() - 1)); for (int block : blocks) { fat[block] = -3; // -3表示数据块 } } // 删除文件 private static void deleteFile(String name) { Optional<FileEntry> entry = directory.stream().filter(f -> !f.isDeleted() && f.getName().equals(name)).findFirst(); if (!entry.isPresent()) { System.out.println("File not found."); return; } FileEntry file = entry.get(); if (file.isDirectory()) { System.out.println("Not a file."); return; } for (int i = 0; i < BLOCK_SIZE; i++) { fat[file.getFirstBlock() + i] = -1; // 释放磁盘块 } file.setDeleted(true); } // 显示目录内容 private static void showDir() { System.out.println("Directory of " + currentDir); for (FileEntry entry : directory) { if (!entry.isDeleted() && entry.getParent().equals(currentDir)) { System.out.print(entry.isDirectory() ? "<DIR>" : " "); System.out.printf(" %s\n", entry.getName()); } } } // 切换当前目录 private static void changeDir(String name) { if (name.equals("..")) { int index = currentDir.lastIndexOf("/"); currentDir = currentDir.substring(0, index); } else { Optional<FileEntry> entry = directory.stream().filter(f -> !f.isDeleted() && f.isDirectory() && f.getName().equals(name) && f.getParent().equals(currentDir)).findFirst(); if (!entry.isPresent()) { System.out.println("Directory not found."); return; } currentDir += "/" + name; } } // 查找空闲的磁盘块 private static int findFreeBlock() { for (int i = 0; i < BLOCK_NUM; i++) { if (fat[i] == -1) { return i; } } return -1; } // 文件目录项 private static class FileEntry { private String name; private boolean isDirectory; private boolean deleted; private int firstBlock; private int parent; public FileEntry(String name, boolean isDirectory, int firstBlock, int parent) { this.name = name; this.isDirectory = isDirectory; this.firstBlock = firstBlock; this.parent = parent; } public String getName() { return name; } public boolean isDirectory() { return isDirectory; } public boolean isDeleted() { return deleted; } public void setDeleted(boolean deleted) { this.deleted = deleted; } public int getFirstBlock() { return firstBlock; } public String getParent() { return directory.get(parent).getName(); } public int getSize() { int size = 0; if (!isDeleted()) { if (isDirectory()) { for (FileEntry entry : directory) { if (!entry.isDeleted() && entry.isDirectory() && entry.getParent().equals(getName())) { size++; } } } else { size = 1; } } return size; } } } ``` 该文件管理系统采用FAT表管理磁盘空间,目录和文件信息保存在一个列表中。每个目录项包括文件名、是否为目录、第一个磁盘块的编号和父目录的索引。删除文件或目录时,只是将对应的目录项标记为已删除,实际的数据块并没有被清空,这样可以避免频繁地修改FAT表。每个文件的数据块根据大小动态分配,如果无法分配足够的空间,则分配失败。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值