Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Enity Framework Core 1.1 and migration

$
0
0

<p>Hi fellow programmers! :)</p>
<p>I've just installed Microsoft Visual Studio 2017 Community and try to get heads up on the newest technology.</p>
<p>I'm not that familiar with MVC yet and definitely not familiar with EF Core 1.1.</p>
<p>I've taken a couple of courses on Pluralsight on ASP.NET Core and EF Core, but it seems there have been changes since these recordings where made.</p>
<p>When I run &quot;Add-Migration Inital&quot; to connect with the database, I always get :</p>
<pre class="prettyprint ">System.InvalidOperationException: The entity type 'Category' requires a primary key to be defined.
at Microsoft.EntityFrameworkCore.Internal.ModelValidator.ShowError(String message)
at Microsoft.EntityFrameworkCore.Internal.ModelValidator.Validate(IModel model)
at Microsoft.EntityFrameworkCore.Internal.RelationalModelValidator.Validate(IModel model)
at Microsoft.EntityFrameworkCore.Infrastructure.ModelSource.CreateModel(DbContext context, IConventionSetBuilder conventionSetBuilder, IModelValidator validator)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Microsoft.EntityFrameworkCore.Internal.DbContextServices.CreateModel()
at Microsoft.EntityFrameworkCore.Internal.LazyRef`1.get_Value()
at Microsoft.Extensions.DependencyInjection.ServiceLookup.CallSiteRuntimeResolver.VisitScoped(ScopedCallSite scopedCallSite, ServiceProvider provider)
at Microsoft.Extensions.DependencyInjection.ServiceProvider.&lt;&gt;c__DisplayClass16_0.&lt;RealizeService&gt;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.&lt;&gt;c__DisplayClass16_0.&lt;RealizeService&gt;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.&lt;&gt;c__DisplayClass3_0`1.&lt;Execute&gt;b__0()
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
The entity type 'Category' requires a primary key to be defined.</pre>
<p>Here are some relevant code:</p>
<p><strong>Category.cs</strong></p>
<pre class="prettyprint "> public class Category
{
[Key]
public int CategoryId;
public String CategoryName;
}</pre>
<p><strong>ICategoryRepository.cs</strong></p>
<pre class="prettyprint ">namespace Dokumentbasen6.Model
{
public interface ICategoryRepository
{
IEnumerable&lt;Category&gt; Categories { get; }
Category GetCategoryById(int CategoryId);
}
}</pre>
<p>CategoryRepository.cs</p>
<pre class="prettyprint "> public class CategoryRepository : ICategoryRepository
{
private readonly DokumentbasenContext _context;

public CategoryRepository(DokumentbasenContext context)
{
_context = context;
}

public IEnumerable&lt;Category&gt; Categories =&gt; _context.Categories;

public Category GetCategoryById(int CategoryId)
{
return _context.Categories.FirstOrDefault(c =&gt; c.CategoryId == CategoryId);
}
}</pre>
<p></p>
<pre class="prettyprint "><strong>DokumentbasenContext.cs</strong></pre>
<pre class="prettyprint "> public class DokumentbasenContext : DbContext
{
public DokumentbasenContext(DbContextOptions&lt;DokumentbasenContext&gt; options) : base(options)
{

}
public DbSet&lt;Document&gt; Dokuments { get; set; }
public DbSet&lt;Category&gt; Categories { get; set; }
public DbSet&lt;Standard&gt; Standards { get; set; }
public DbSet&lt;Member&gt; Members { get; set; }
}</pre>
<p></p>
<p>When I build the project I used the target framework .NETCoreApp 1.0, but I have now changed target Framework to .NetCoreApp 1.1.</p>
<p></p>
<p>In advance, thank you very much for your support!</p>
<p></p>
<p>Kind regards,</p>
<p>Jon Haakon Ariansen</p>

Viewing all articles
Browse latest Browse all 9386

Trending Articles