有没有办法我可以在SQL Server中创建一个表,然后更新到我的项目的迁移? [英] Is there any way i can just create a table in SQL Server and then update the migration to my project?

查看:179
本文介绍了有没有办法我可以在SQL Server中创建一个表,然后更新到我的项目的迁移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经从MS SQL Server 2012导入数据库表,并将它们迁移到Visual Studio 2015,使用这个例子。它的工作,但刚刚我刚刚在SQL Server中添加了一个新表,我想用新的表更新项目中的DBContext。



我尝试在包管理器控制台中输入add-migration new_table_name,但是没有起作用。我明白,我必须在我的项目的模型文件夹中手动编写一个新的模型,那么只有该迁移才能正常工作。但我觉得我更喜欢在SQL Server中创建一个表。那么,有没有办法我可以在SQL Server中创建一个表,然后更新迁移到我的项目?



如果我创建另一个ado.net,这次我先选择DB?之后,我将首先删除代码DBContext,configuration.vb和模型。稍后会影响我的项目吗?

解决方案

如果您的主要目标是在数据库中首先创建表,然后自动更新您的项目,那么您应该使用数据库优先



那就是说,你必须考虑数据库优先的缺点:在我个人经验中,我停止使用这种方法,主要是由于两个原因:




  • 据我所知,数据库首次支持将被停用。 EF Core不包括编辑器工具。有关这方面的一些链接:来自朱莉的帖子Lerman EF核心路线图 Microsoft的早期公告

  • 模型编辑器有几个错误和怪癖,导致代码随时随地破裂。这些错误很可能不会被修复(参见上一点)。像改变现有字段的类型,改变外键等等。

  • 由于自动生成的实体文件的源代码存储库合并,我遇到了很多问题。特别是(但不仅仅是)当几个人正在使用相同的实体,所以我们在自动生成的代码中得到合并冲突。此外,自动生成的代码有时没有正确检出,所以它与edmx失去同步。我不知道这对其他人也是如此,但似乎有时Visual Studio,编辑器,后台自动代码生成工具和TFS源代码管理器不能很好地协同工作。



所以如果你真的不能在没有创建数据库的情况下无法生成数据库中的数据库,那么你必须考虑如果您不使用代码优先,您将失去什么。这种方法被广泛推荐的原因。



通常,现在使用Database First的人的主要原因是不可能将旧版代码迁移到Code First方法。据我所知,这是广泛接受的,Code First是否是正确的方法。在这里,您有关于此的一个有趣的帖子(即使它有点旧,为EF 4.1撰写,当Code First被引入时,它涉及到每个方法的主要优点和缺点)。



解决方法可能是继续使用Code First,但也可以使用可用的工具,通过从数据库表进行逆向工程自动生成您的Code First实体。这样,您仍然可以在数据库中直接生成表,但是继续使用代码优先与迁移和所有内容。在这里,您有 Julie Lerman发表的关于其中一些工具的信息。可能有更多的工具,但我没有使用它们,我不知道他们。



注意:我对Database First的个人经验是糟糕的并没有持续太久。也许在这种方法中有更积极的经验的人可能会给出更有用的见解。我一直在使用Code First,现在真的很喜欢这种方法。我的答案可能有点偏见。


I have imported database tables from MS SQL Server 2012 and migrated them to Visual Studio 2015 using code-first concept shown in this example. Its working but just now i just added a new table in SQL Server and i wanted to update the DBContext in my project with the new table.

I tried entering "add-migration new_table_name" in the Package Manager Console but it didn't work. I understand that i have to code a new model manually in the Models folder of my project, then only that migration will work. But i feel like i'm more comfortable creating a table in SQL Server. So, is there any way i can just create a table in SQL Server and then update the migration to my project?

Will it do if i create another ado.net but this time i choose DB first? After that i'll delete the code first DBContext, configuration.vb and the models. Will it affect my project later on?

解决方案

If your main goal is to create the tables first in the database and then automatically update your project then you should use Database First.

That said, you have to consider the drawbacks of Database First: in my personal experience I stopped using that approach mainly because of two reasons:

  • Database First support will be discontinued, as far as I know. EF Core does not include the editor tool. Some links about this: a post from Julie Lerman, the EF Core roadmap, and an early announcement from Microsoft.
  • The model editor had several bugs and quirks that caused the code to get broken every now and then. These bugs are most likely just not going to be fixed (see previous point). Things like changing the type of an existing field, changing foreign keys, etc.
  • I had a lot of problems because of source code repository merges of the auto-generated entity files. Especially (but not only) when several people were working with the same entities so we were getting merge conflicts in the auto-generated code. Also, the auto-generated code sometimes was not being checked-out correctly, so it got out-of-sync with the edmx. I'm not sure it this happens also to other people, but it seems that sometimes Visual Studio, the editor, the background auto-code generation tool and the TFS source code manager just don't work well together.

So if you really can't live without creating first the tables in the database go on with Database First, but you have to consider what you are losing if you don't use Code First. This approach is widely recommended by a reason.

Usually the main reason for people using Database First nowadays is the impossibility to migrate legacy code to the Code First approach. As far as I know it is widely accepted that Code First is the right way to go otherwise. Here you have an interesting post about this (even if it is a bit old, written for EF 4.1, when Code First was introduced, it deals with the main pros and cons of each approach).

A workaround for you could be to keep using Code First but also use the available tools that automatically generate your Code First entities by doing reverse engineering from the database tables. With this you can still generate your tables directly in the database, but keep on using Code First with migrations and everything. Here you have a post from Julie Lerman about some of those tools. There may be more recent tools, but I haven't used them and I don't know about them.

Note: my personal experience with Database First was kind of bad and didn't last too long. Perhaps someone with more positive experience in this approach could give more useful insight about it. I've been using Code First for a time now and really prefer this approach. My answer may be a bit biased.

这篇关于有没有办法我可以在SQL Server中创建一个表,然后更新到我的项目的迁移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆