You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用EF Core工具创建迁移时抛出权限异常问题求助

EF Core CLI在MacOS Sequoia上的权限错误排查与替代方案

问题背景

环境配置:MacOS Sequoia 15.3(M1 Arm)、.NET SDK 9.0.102、Rider 2024.3.5
EF Core项目运行正常,但执行dotnet ef migrations add Initial --project App -s App创建迁移时,抛出权限错误:

System.UnauthorizedAccessException: Access to the path '/Users/myuser/RiderProjects/resilient/App/obj/App.csproj.EntityFrameworkCore.targets' is denied.
---> System.IO.IOException: Operation not permitted
--- End of inner exception stack trace ---
at Interop.ThrowExceptionForIoErrno(ErrorInfo errorInfo, String path, Boolean isDirError)
...

已全局安装dotnet-ef,尝试sudo执行命令无效,此前该工具在本机可正常使用。需求:排查权限问题,同时了解创建迁移的其他方式。

排查与解决步骤

1. 修复文件/目录权限

  • 先查看报错文件或obj目录的权限:
    # 检查目标文件权限(如果存在)
    ls -la /Users/myuser/RiderProjects/resilient/App/obj/App.csproj.EntityFrameworkCore.targets
    # 检查obj目录权限
    ls -ld /Users/myuser/RiderProjects/resilient/App/obj
    
  • 把目录权限重置为当前用户所属(替换myuser为你的实际用户名):
    sudo chown -R myuser:staff /Users/myuser/RiderProjects/resilient/App/obj
    

2. 检查MacOS隐私安全限制

MacOS的隐私权限可能阻止dotnet进程访问项目目录:

  • 打开「系统设置」→「隐私与安全性」→「文件与文件夹」
  • 找到dotnet或Rider应用,确保已勾选项目所在的RiderProjects目录访问权限
  • 若找不到对应应用,点击「+」手动添加/usr/local/share/dotnet/dotnet可执行文件,并授予文件夹访问权限

3. 清理项目重建

  • 删除项目下的bin和obj目录,在Rider中执行「Clean Solution」再「Rebuild Solution」
  • 重新尝试执行迁移命令

创建迁移的替代方式

如果CLI工具问题无法快速解决,可选择以下两种方式:

方式1:使用Rider内置工具

打开Rider的「Database」工具窗口,找到你的DbContext类,右键选择「EF Core → Add Migration」,输入迁移名称即可自动生成迁移文件。

方式2:代码生成迁移

在项目中添加临时启动代码(执行后记得删除),直接生成迁移:

using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;

// 替换为你的DbContext类型和连接字符串
var options = new DbContextOptionsBuilder<YourDbContext>()
    .UseSqlServer("Your_Connection_String")
    .Options;

using var context = new YourDbContext(options);
// 生成迁移脚本
var migrationScript = context.Database.GenerateCreateScript();
// 或者直接创建迁移(需确保已安装Microsoft.EntityFrameworkCore.Design包)
// var migrator = context.GetService<IMigrator>();
// migrator.Migrate();

环境兼容性说明

已有相同环境(M1 Arm + MacOS Sequoia + .NET 9)的用户反馈dotnet-ef工具可正常运行,本次问题大概率是本机权限或隐私设置异常导致,而非工具本身兼容性问题。

内容的提问来源于stack exchange,提问作者tonsteri

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 10:33:13