Is there any way i can just create a table in SQL Server and then update the migration to my project?

瘦欲@ 提交于 2019-12-25 18:38:32

问题


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?


回答1:


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.




回答2:


AFAIK,

EF migrations is supposed to serve the Code-First approach whereas you want to use database first, which means migrations is something that's not intended for you.

You can achieve most DB schematic features and customize the tables and its columns attributes by EF via data annotation attributes and EF fluent API by overriding the OnModelCreating method in your DbContext subclass.



来源:https://stackoverflow.com/questions/42048555/is-there-any-way-i-can-just-create-a-table-in-sql-server-and-then-update-the-mig

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!