3

So I've run into something and was wondering if you could help me out. I am very new (this is my first project in these languages) to MS MVC 2 and Entity Framework 4.0. I've drawn up a model using the Model First approach and the graphical designer. I didn't have a SQL database for it yet and I figured it was just working disconnected, but maybe I just forget that I had specified a connection string somewhere. So I go about my business getting the MVC components up and running using the newly created types from EF. I am attempting to using the Repository pattern as well as Ninject for dependency injection. So I had a lot of time before I had to worry about actual data storage. Which brings me to my issue: I'm now at that point.

.mdf ファイルにアタッチし、それを SQLExpress インスタンスから実行していると思いましたが、それをサポートするものを実際に見つけることができませんでした。SSMS をインストールしていなかったので、適切に確認する方法がわかりませんでした。最終的にそれを起動して実行し、データベースはありませんでした。だから今、私はこれが実際にどこに配線されているかについて混乱しています。私はそれが奇妙すぎると判断し、少しやり直したいと思いました. モデルの SQL スクリプトを生成し、スクリプトを実行するために SSMS で新しいデータベースを作成しました。スクリプトを実行すると、すべて問題ありません。次に、VS に入り、データベース ファーストのシナリオを実行します。したがって、モデルはすべてのテーブルを表示し、いくつかのフィールドをキーとして指定してはならないときにキーとして指定することで、いくつかのフィールドを効果的に台無しにします。それを修正したので、最初に私を苛立たせた問題を理解することに戻りました。

I have a User table that has a userId field that is supposed to be a Guid, but during the original modeling I missed it and it was a varchar field. I've not been able to fix the mapping underneath it to change the underlying ( not sure the actual terminology for it ) field to a Guid. I changed the Model's type but nothing updated the database. This has basically put me in an odd position. I believe I'm supposed to allow EF to manage the SQL for me without my involvement, but if this is true how can I push a change like that. Am I restricted to dropping the entire database everytime I need to make a change to the schema?

Sorry if this is the noobest of noob questions, but I am trying to figure all this stuff out and could really use some input. Last language set I used was ASP.Net 2.0 with ADO.Net typed datasets. Am I better off switching to a Code First approach? If I were to do that do I have to start managing the SQL myself again?

I believe this is my first question here so, my apologies if I violated anything; I think I'm clear though.

4

1 に答える 1

1

まあ-EFは接続文字列をWeb.configファイルに保存します。モデルを最初に使用するときにEFがデータベースを削除して新しいデータベースを作成するようにプッシュするというあなたの疑いは、部分的に正しいです。幸いなことに、このプロセス全体は、変更または置換可能なワークフローによって推進されます。自分でやる必要はありません。Entity Designer Database Generation Power Packをダウンロードできます。これには、モデル ファースト アプローチによるデータベースの増分構築を可能にするいくつかの新しいワークフローとテンプレートが含まれています。これには 2 つの欠点があります。

  • これらのエディションのデータベース機能を使用するため、VS 2010 Premium または Ultimate のいずれかが必要です。
  • モデルへのすべての変更を段階的に処理できるわけではありません。データの損失につながる可能性のあるものはすべて例外で終了します。stringに変更する場合、これが当てはまると思いGuidます。このような場合の解決策は、DB を手動で変更するか、DB を削除して最初から再作成することです。

このアプローチには現在データベースの進化が欠けているため、コードファーストはおそらく役に立ちませんが、モデルが変更された場合にデータベースを再作成するプロセスが完全に自動化されています。

ところで。データベースをチェックするのに SSMS は必要ありません。SQL サーバーには、データベースにアクセスするためのコマンド ライン ユーティリティ (osql または sqlcmd) を備えたいくつかのツール (個別にインストールする必要があります) も用意されています。また、Visual Studio Professional 以降でサーバー エクスプローラーを使用することもできます。

于 2011-04-18T20:50:44.450 に答える