[英]Why do I get Object reference not set to an instance of an object when running Update-Database
First time working with entity framework, I've been testing with an in memory database which has been working fine but wanted to start working on a persistent database using sqlite.第一次使用实体框架时,我一直在使用 memory 数据库进行测试,该数据库运行良好,但想开始使用 sqlite 处理持久性数据库。
I've got the model and context created, when I run Add-Migration in the Package Manager Console it seems to work fine but when I then run the Update-Database function I always get a 'Object reference not set to an instance of an object' error but I can't work out why, any help really appreciated, been searching around for a while and can't see anything.我已经创建了 model 并创建了上下文,当我在 Package 管理器控制台中运行 Add-Migration 时,它似乎工作正常,但是当我然后运行更新数据库 ZC1C425268E68385D1AB5074C17A94 的实例时,我总是不设置为object' 错误,但我不知道为什么,任何帮助真的很感激,一直在寻找一段时间,但什么也看不到。
Screenshot of the reported error报错截图
I've got four model classes and the context class so not sure I can include all the code snippets here but happy to if folk think it's needed to get anywhere.我有四个 model 类和上下文 class 所以不确定我可以在这里包含所有代码片段,但如果人们认为它需要到达任何地方,我很高兴。
Thanks.谢谢。
OK so I found out what the issue was.好的,所以我发现了问题所在。 The "Object reference not set to an instance of an object" in this case was due to the fact that I didn't have a reference to Microsoft.EntityFrameworkCore.Sqlite.在这种情况下,“对象引用未设置为对象的实例”是因为我没有对 Microsoft.EntityFrameworkCore.Sqlite 的引用。 I did have a reference to Microsoft.EntityFrameworkCore.Sqlite.Core which is what allowed the project to build OK but apparently this is required for the Update-Database call.我确实引用了 Microsoft.EntityFrameworkCore.Sqlite.Core,这使项目可以正常构建,但显然这是 Update-Database 调用所必需的。
Also for anyone else needing to take a step back and just confirm they aren't going crazy, this is a good website describing what is needed.对于其他需要退后一步并确认他们没有发疯的人来说,这是一个很好的网站,描述了需要什么。 I used this just to confirm everything worked OK and I hadn't missed something (which I obviously had).我用它只是为了确认一切正常并且我没有错过任何东西(我显然有)。
TL:DR - Make sure you have a reference to Microsoft.EntityFrameworkCore.Sqlite TL:DR - 确保您参考了 Microsoft.EntityFrameworkCore.Sqlite
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.