Giter VIP home page Giter VIP logo

Comments (10)

liuliang-wt avatar liuliang-wt commented on May 19, 2024

框架是可以自动生成数据库的,请再检查一下Appsettings文件,看看连接字符串和DBType是否正确设置了

from wtm.

wanbolantian avatar wanbolantian commented on May 19, 2024

WX20190605-212345 这是配置文件,我检查了ConnectionString和DbType,都是正确的,您帮看看还有哪里没设对?

from wtm.

liuliang-wt avatar liuliang-wt commented on May 19, 2024

配置文件看起来没有问题,你试一下命令行调试,如果没有生成数据库命令行窗口肯定会输出错误,你看看是什么错误

from wtm.

wanbolantian avatar wanbolantian commented on May 19, 2024

通过在命令行执行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_01.b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)

看提示信息,应该是和dbcontext的构造函数有关,请问要怎样解决呢?

from wtm.

liuliang-wt avatar liuliang-wt commented on May 19, 2024

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.

wanbolantian avatar wanbolantian commented on May 19, 2024

按照上面的方法,可以生成数据库表了,但表里没有初始数据,所以在登陆的时候就会报“登陆失败”。我查看源码,在FrameworkContext里有个DataInit用来产生初始数据的,估计是没有执行到这个方法。请问要如何才能产生初始数据呢?如果手工在数据库产生的初始数据的话,还蛮繁琐的。

from wtm.

liuliang-wt avatar liuliang-wt commented on May 19, 2024

初始化数据是在第一次建库的时候运行的,正常逻辑是你第一次直接运行项目,他会帮你建库并且填充初始化数据,后续你对模型的更改再使用add-migration。 但是你说他不会自动给你建库很奇怪。。。

from wtm.

liuliang-wt avatar liuliang-wt commented on May 19, 2024

你把库删掉,直接在命令行运行项目,你看到底了什么错没有自动建库

from wtm.

wanbolantian avatar wanbolantian commented on May 19, 2024

可以跑起来了,我大概知道原因了。应该是我的服务器上原本就存在了test这个库,所以导致了框架不能自动建库和填充初始化数据。而把整个test库删掉后,再重新运行,就正常了。这个注意点可以写到框架的FAQ里了,让后来者少踩个坑,感谢作者耐心的指点!!

from wtm.

liuliang-wt avatar liuliang-wt commented on May 19, 2024

感谢使用WTM,希望能给你的开发带来帮助

from wtm.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.