MVC 3 EF Code-first 到 webhost 数据库的问题
我对 ASP.NET MVC 3 以及一般编码相当陌生。
我有一个非常非常小的应用程序,我想上传到我的虚拟主机域。
我正在使用实体框架,它在我的本地计算机上运行良好。 我已经输入了一个新的连接字符串来使用我的远程数据库,但它实际上不起作用,首先我有 1 个 MSSQL 数据库,无法删除和重新创建该数据库,因此我无法在初始化程序中使用该策略,我尝试在策略中提供 null,但无济于事,我的表根本没有在我的数据库中创建,这就是问题所在,我不知道如何使用实体框架来做到这一点。
当我运行应用程序时,它尝试从数据库中选择数据,该部分工作正常,我只是不知道如何通过 codefirst 在我的数据库中创建这些选项卡。
我可能可以通过手动重新创建表来使其工作,但我想通过 codefirst 了解解决方案。
这是我的初始化类
public class EntityInit : DropCreateDatabaseIfModelChanges<NewsContext>
{
private NewsContext _db = new NewsContext();
protected override void Seed(NewsContext context)
{
new List<News>
{
new News{ Author="Michael Brandt", Title="Test News 1 ", NewsBody="Bblablabalblaaaaa1" },
new News{ Author="Michael Brandt", Title="Test News 2 ", NewsBody="Bblablabalblaaaaa2" },
new News{ Author="Michael Brandt", Title="Test News 3 ", NewsBody="Bblablabalblaaaaa3" },
new News{ Author="Michael Brandt", Title="Test News 4 ", NewsBody="Bblablabalblaaaaa4" },
}.ForEach(a => context.News.Add(a));
base.Seed(context);
}
}
正如我所说,我对这一切都很陌生,所以请原谅,如果我无法提供您需要回答我的问题的正确信息,只有我知道,我会回答它
Im fairly new to ASP.NET MVC 3, and to coding in general really.
I have a very very small application i want to upload to my webhosting domain.
I am using entity framework, and it works fine on my local machine.
I've entered a new connection string to use my remote database instead however it dosen't really work, first of all i have 1 single MSSQL database, which cannot be de dropped and recreated, so i cannot use that strategy in my initializer, i tried to supply null in the strategy, but to no avail, my tables simply does not get created in my database and thats the problem, i don't know how i am to do that with entity framework.
When i run the application, it tries to select the data from the database, that part works fine, i just dont know how to be able to create those tabes in my database through codefirst.
I could probaly get it to work through manually recreating the tables, but i want to know the solution through codefirst.
This is my initializer class
public class EntityInit : DropCreateDatabaseIfModelChanges<NewsContext>
{
private NewsContext _db = new NewsContext();
protected override void Seed(NewsContext context)
{
new List<News>
{
new News{ Author="Michael Brandt", Title="Test News 1 ", NewsBody="Bblablabalblaaaaa1" },
new News{ Author="Michael Brandt", Title="Test News 2 ", NewsBody="Bblablabalblaaaaa2" },
new News{ Author="Michael Brandt", Title="Test News 3 ", NewsBody="Bblablabalblaaaaa3" },
new News{ Author="Michael Brandt", Title="Test News 4 ", NewsBody="Bblablabalblaaaaa4" },
}.ForEach(a => context.News.Add(a));
base.Seed(context);
}
}
As i said, im really new to all this, so excuse me, if im lacking to provide the proper information you need to answer my question, just me know and i will answer it
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
初始化策略暂不支持升级策略。
应使用初始化策略来初始化新数据库。目前所有后续更改都应使用脚本完成。
我们所说的最佳实践是使用脚本修改数据库,然后手动调整代码以反映此更改。
在未来的版本中,将提供升级/迁移策略。
尝试从自定义 IDatabaseInitializer 逐条语句执行脚本
然后您可以从中读取数据库中的数据库版本并将缺少的脚本应用到您的数据库。只需将数据库版本存储在表中即可。然后通过更改脚本升级。
您是否尝试过使用
CreateDatabaseOnlyIfNotExists
- 每次初始化上下文时,如果数据库不存在,都会重新创建数据库。
可以使用Database 类的SetInitializer 方法设置数据库初始化程序。如果未指定任何内容,它将使用CreateDatabaseOnlyIfNotExists 类来初始化数据库。
-
我不确定这是否是确切的语法,因为我有一段时间没有写这个了。但应该是非常相似的。
Initialization strategies do not support upgrade strategies at the moment.
Initialization strategies should be used to initialise a new database. all subsequent changes should be done using scripts at the moment.
the best practice as we speak is to modify the database with a script, and then adjust by hand the code to reflect this change.
in future releases, upgrade / migration strategies will be available.
try to execute the scripts statement by statement from a custom IDatabaseInitializer
then from this you can read the database version in the db and apply the missing scripts to your database. simply store a db version in a table. then level up with change scripts.
Have you tried to use the
CreateDatabaseOnlyIfNotExists
– Every time the context is initialized, database will be recreated if it does not exist.
The database initializer can be set using the SetInitializer method of the Database class.If nothing is specified it will use the CreateDatabaseOnlyIfNotExists class to initialize the database.
-
I'm not sure if this is the exact syntax as I have not written this in a while. But it should be very similar.
如果您使用的是非常小的应用程序,您也许可以选择 SQL CE 4.0。
bin 部署应该允许您运行 SQL CE 4.0,即使您的提供程序没有为其安装二进制文件。您可以 在此处了解更多信息。
我们实际上可以使用您想要的任何初始化程序,因为您现在不存在无法删除数据库和删除表的问题。
这有什么帮助吗?If you are using a very small application, you maybe could go for SQL CE 4.0.
The bin-deployment should allow you to run SQL CE 4.0 even if your provider doesn't have the binaries installed for it. You can read more here.
That we you can actually use whatever initializer you want, since you now don't have the problem of not being able to drop databases and delete tables.
could this be of any help?