.net coe 类库中使用ef core遇到的问题

1.提示”dotnet-ef未知命令”:
在csproj文件中新增itemgroup,加入如下:

<DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0" />

2.dotnet ef migrations add xxx 时提示如下信息
The specified framework version ‘2.0’ could not be parsed
The specified framework ‘Microsoft.NETCore.App’, version ‘2.0’ was not found.
– Check application dependencies and target a framework version installed at:
\
– Alternatively, install the framework version ‘2.0’.
此信息的错误原因为未指定runtime版本号. 在 节点下加入如下:

<RuntimeFrameworkVersion>2.0.3</RuntimeFrameworkVersion>

版本号试项目而定
3. dotnet ef migrations add xxx时提示如下错误:
Unable to create an object of type ‘xxxDbContext’. Add an implementation of ‘IDesignTimeDbContextFactory‘ to the project, or see https://go.microsoft.com/fwlink/?linkid=851728 for additional patterns supported at design time.
此错误为,没有找到dbcontext的初始化信息,未指定所用数据库以及数据库链接字符串.
解决此问题需要在项目中 dbcontext同目录下增加 IDesignTimeDbContextFactory 接口的实现类

    public class DesignTimeDbContextFactory : IDesignTimeDbContextFactory<CHCmsDbContext>

    {
       

        public CHCmsDbContext CreateDbContext(string[] args)

        {

            var builder = new DbContextOptionsBuilder<CHCmsDbContext>();
           
            builder.UseMySql("Server=localhost;User=chcms;Password=123456;DataBase=CHCms;");

            return new CHCmsDbContext(builder.Options);

        }

    }

发表评论

电子邮件地址不会被公开。 必填项已用*标注