Comments (10)
框架是可以自动生成数据库的,请再检查一下Appsettings文件,看看连接字符串和DBType是否正确设置了
from wtm.
这是配置文件,我检查了ConnectionString和DbType,都是正确的,您帮看看还有哪里没设对?
from wtm.
配置文件看起来没有问题,你试一下命令行调试,如果没有生成数据库命令行窗口肯定会输出错误,你看看是什么错误
from wtm.
通过在命令行执行add-migration initial -verbose 得到的报错信息如下
Using project 'test'.
Using startup project 'test'.
Build started...
Build succeeded.
C:\Program Files\dotnet\dotnet.exe exec --depsfile C:\Users\Administrator\Downloads\test\test\bin\Debug\netcoreapp2.2\test.deps.json --additionalprobingpath C:\Users\Administrator.nuget\packages --additionalprobingpath "C:\Program Files\dotnet\sdk\NuGetFallbackFolder" --runtimeconfig C:\Users\Administrator\Downloads\test\test\bin\Debug\netcoreapp2.2\test.runtimeconfig.json "C:\Program Files\dotnet\sdk\NuGetFallbackFolder\microsoft.entityframeworkcore.tools\2.2.0\tools\netcoreapp2.0\any\ef.dll" migrations add initial --json --verbose --no-color --prefix-output --assembly C:\Users\Administrator\Downloads\test\test\bin\Debug\netcoreapp2.2\test.dll --startup-assembly C:\Users\Administrator\Downloads\test\test\bin\Debug\netcoreapp2.2\test.dll --project-dir C:\Users\Administrator\Downloads\test\test\ --language C# --working-dir C:\Users\Administrator\Downloads\test --root-namespace test
Using assembly 'test'.
Using startup assembly 'test'.
Using application base 'C:\Users\Administrator\Downloads\test\test\bin\Debug\netcoreapp2.2'.
Using working directory 'C:\Users\Administrator\Downloads\test\test'.
Using root namespace 'test'.
Using project directory 'C:\Users\Administrator\Downloads\test\test'.
Finding DbContext classes...
Finding IDesignTimeDbContextFactory implementations...
Finding application service provider...
Finding IWebHost accessor...
Using environment 'Development'.
Using application service provider from IWebHost accessor on 'Program'.
Finding DbContext classes in the project...
Found DbContext 'DataContext'.
Microsoft.EntityFrameworkCore.Design.OperationException: Unable to create an object of type 'DataContext'. For the different patterns supported at design time, see https://go.microsoft.com/fwlink/?linkid=851728 ---> System.MissingMethodException: No parameterless constructor defined for this object.
at System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean wrapExceptions, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor)
at System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean wrapExceptions, Boolean skipCheckThis, Boolean fillCache)
at System.Activator.CreateInstance(Type type, Boolean nonPublic, Boolean wrapExceptions)
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass12_3.b__13()
--- End of inner exception stack trace ---
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.<>c__DisplayClass12_3.b__13()
at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(Func1 factory) at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigration.<>c__DisplayClass0_1.<.ctor>b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0
1.b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
看提示信息,应该是和dbcontext的构造函数有关,请问要怎样解决呢?
from wtm.
EF core提供的add-migration,其原理是找到你代码中的dbcontext,然后找到他使用的连接字符串,从而找到最终需要操作的数据库。但是wtm框架同时支持sqlserver,mysql,pgsql等多种数据库,具体使用哪个是配置文件配置的, 所以ef core不知道到底要操作哪个数据。
解决办法你可以参考 https://docs.microsoft.com/zh-cn/ef/core/miscellaneous/cli/dbcontext-creation,就是新加一个类,直接告诉ef使用哪个数据库
例如:
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Design;
using Microsoft.EntityFrameworkCore.Infrastructure;
namespace MyProject
{
public class DataContextFactory : IDesignTimeDbContextFactory
{
public DataContext CreateDbContext(string[] args)
{
var optionsBuilder = new DbContextOptionsBuilder();
//使用UseSqlServer,UseMySql,UseNpgsql等指定使用不同的数据库
optionsBuilder.UseMySql("这里指定完整的连接字符串");
return new DataContext (optionsBuilder.Options);
}
}
}
from wtm.
按照上面的方法,可以生成数据库表了,但表里没有初始数据,所以在登陆的时候就会报“登陆失败”。我查看源码,在FrameworkContext里有个DataInit用来产生初始数据的,估计是没有执行到这个方法。请问要如何才能产生初始数据呢?如果手工在数据库产生的初始数据的话,还蛮繁琐的。
from wtm.
初始化数据是在第一次建库的时候运行的,正常逻辑是你第一次直接运行项目,他会帮你建库并且填充初始化数据,后续你对模型的更改再使用add-migration。 但是你说他不会自动给你建库很奇怪。。。
from wtm.
你把库删掉,直接在命令行运行项目,你看到底了什么错没有自动建库
from wtm.
可以跑起来了,我大概知道原因了。应该是我的服务器上原本就存在了test这个库,所以导致了框架不能自动建库和填充初始化数据。而把整个test库删掉后,再重新运行,就正常了。这个注意点可以写到框架的FAQ里了,让后来者少踩个坑,感谢作者耐心的指点!!
from wtm.
感谢使用WTM,希望能给你的开发带来帮助
from wtm.
Related Issues (20)
- 一对多 Model文件放到不同类库中引发 The XXX field is required.问题 HOT 1
- 建议:调试模式 反射所有controller下的Index方法作为左侧菜单 希望能排序
- Microsoft TrueType Core Fonts
- 关于pgsql 时间戳的问题 HOT 1
- 6.5.0下的部门管理中的新增功能排序混乱
- WTM文档中的模型层创建模型里的TreePoco无法正常使用
- 系统自带的代码生成器无法正确识别所选实体类型
- Workflow运行时部分情况无法正确读取Model的Property HOT 2
- domain还是失效了? HOT 2
- 可空外键导入的bug.
- 如何添加时间戳字段
- 升级到版本8后,Contains方法报错 HOT 1
- 图标库出错
- grid 如何设置不同按钮
- How to set different buttons in a grid.
- 新版本Blazor权限问题
- VUE的文件下载只能处理最新上传的文件
- 【问题】 使用 mysql 5.7.26 第一次启动创建数据库报错
- 代码生成器:生成blazor index页面的模板“批量删除”按钮大小写错误 HOT 1
- Bug: Elsa Workflow 没有区分Tenant
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from wtm.