2017-05-26 16 views
1

私はMicrosoft Visual Studio 2017コミュニティをインストールしたばかりで、最新のテクノロジに目を向けようとしています。エンティティフレームワークコア1.1と移行

私はまだMVCに精通しておらず、EF Core 1.1についてよく知らない人です。

ASP.NETコアとEFコアのPluralsightでいくつかの講座を受講しましたが、これらのレコーディング以降に変更が加えられているようです。

私はデータベースに接続するには、「Inital-の移行を追加」を実行すると、私は常に取得:

のSystem.InvalidOperationException:エンティティタイプ「カテゴリー」を定義する主キーが必要です。 Microsoft.EntityFrameworkCore.Internal.RelationalModelValidator.ValidateでMicrosoft.EntityFrameworkCore.Internal.ModelValidator.Validate(IModelをモデル) でMicrosoft.EntityFrameworkCore.Internal.ModelValidator.ShowError(文字列メッセージ) (IModelをモデル) でMicrosoft.EntityFrameworkCoreでMicrosoft.EntityFrameworkCore.InternalでMicrosoft.EntityFrameworkCore.Internal.DbContextServices.CreateModelでSystem.Collections.Concurrent.ConcurrentDictionary 2.GetOrAdd(TKey key, Func 2 ValueFactoryので.Infrastructure.ModelSource.CreateModel(DbContextコンテキスト、IConventionSetBuilder conventionSetBuilder、IModelValidatorバリ) ) () 。 LazyRef 1.get_Value() at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetService[T](IServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitConstructor(ConstructorCallSite constructorCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitTransient(TransientCallSite transientCallSite, ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProvider.<>c__DisplayClass16_0.<RealizeService>b__0(ServiceProvider provider) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService(IServiceProvider provider, Type serviceType) at Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions.GetRequiredService[T](IServiceProvider provider) at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.AddMigration(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.AddMigrationImpl(String name, String outputDir, String contextType) at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.<>c__DisplayClass3_0 1.b__0() at Microsoft.EntityFrameworkCore.Design.OperationExecutor.Opera actionBase.Execute(アクションアクション) エンティティタイプ 'Category'では、主キーを定義する必要があります。ここで

は、いくつかの関連するコードです:

Category.cs

public class Category 
{ 
    [Key] 
    public int CategoryId; 
    public String CategoryName; 
} 

ICategoryRepository.cs

名前空間Dokumentbasen6.Model { IEnumerableをカテゴリーは{取得ICategoryRepository { パブリックインターフェイス。 } カテゴリGetCategoryById(int CategoryId); }}

CategoryRepository.cs

namespace Dokumentbasen6.Model 
{IEnumerableをカテゴリ{得るICategoryRepository

{ パブリックインターフェース。 } カテゴリGetCategoryById(int CategoryId); }}

DokumentbasenContext.cs

public class DokumentbasenContext : DbContext 
{ 
    public DokumentbasenContext(DbContextOptions<DokumentbasenContext> options) : base(options) 
    { 

    } 
    public DbSet<Document> Dokuments { get; set; } 
    public DbSet<Category> Categories { get; set; } 
    public DbSet<Standard> Standards { get; set; } 
    public DbSet<Member> Members { get; set; } 
} 

私は.NETCoreApp 1.0ターゲットフレームワークを使用しますが、私は今.NetCoreApp 1.1にターゲットフレームワークを変更したプロジェクトをビルドします。

事前にサポートいただきありがとうございます。

敬具、

ジョンホーコンAriansen

答えて

0

Microsoft.EntityFrameworkCore.Tools.DotNetがプロジェクトに追加する必要があります。プロジェクトを右クリックし、Edit *.csprojを選択します。次に、以下を追加してください:

<ItemGroup> 
    <DotNetCliToolReference Include="Microsoft.EntityFrameworkCore.Tools.DotNet" Version="2.0.0-preview2-final" /> 
    </ItemGroup> 

注:このバージョンはこの記事の時点で最新のものであり、今後変更される可能性があります。

これで、移行の作成を開始できます。プロジェクトのフォルダに移動します。プロジェクトを右クリックする最も簡単な方法は、Open Folder in File Explorerです。 File Explorerのアドレスバーにcmdと入力して、そのフォルダー内のコマンドプロンプトを開きます。

今すぐ移行を作成するには、次のコマンドを使用します。

dotnet ef migrations add InitialCreate -c DokumentbasenContext

あなたは今、あなたのプロジェクトにMigrationsフォルダが表示されるはずです。

関連する問題