I
I
Iqbol2017-05-08 14:39:41
ASP.NET
Iqbol, 2017-05-08 14:39:41

How to detect an error when initializing a database in Asp.Net core & ef.core?

Good afternoon.
Please tell me what could be the problem.
Do not go through the process of initializing the database in EntityFramework.Core, despite the fact that migrations are created without any errors.

>dotnet ef database update

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.76
System.ArgumentException: GenericArguments[0], 'MobileStore.Migrations.MobileContext', on 'Microsoft.EntityFrame
workCore.Infrastructure.IDbContextFactory`1[TContext]' violates the constraint of type 'TContext'. ---> System.T
ypeLoadException: GenericArguments[0], 'MobileStore.Migrations.MobileContext', on 'Microsoft.EntityFrameworkCore
.Infrastructure.IDbContextFactory`1[TContext]' violates the constraint of type parameter 'TContext'.
   at System.RuntimeTypeHandle.Instantiate(Type[] inst)
   at System.RuntimeType.MakeGenericType(Type[] instantiation)
   --- End of inner exception stack trace ---
   at System.RuntimeType.ValidateGenericArguments(MemberInfo definition, RuntimeType[] genericArguments, Excepti
on e)
at System.RuntimeType.MakeGenericType(Type[] instantiation)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.FindContextFactory(Type contextType)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.FindContextTypes()
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.FindContextType(String name)
   at Microsoft.EntityFrameworkCore.Design.Internal.DbContextOperations.CreateContext(String contextType)
   at Microsoft.EntityFrameworkCore.Design.Internal.MigrationsOperations.UpdateDatabase(String targetMigration,
String contextType)
at Microsoft.EntityFrameworkCore.Design.OperationExecutor.UpdateDatabase.<>c__DisplayClass0_1.<.ctor>b__0()
   at Microsoft.EntityFrameworkCore.Design.OperationExecutor.OperationBase.Execute(Action action)
GenericArguments[0], 'MobileStore.Migrations.MobileContext', on 'Microsoft.EntityFrameworkCore.Infrastructure.ID
bContextFactory`1[TContext]' violates the constraint of type 'TContext'.

Data context class:
using Microsoft.EntityFrameworkCore;
 
namespace MobileStore.Models
{
    public class MobileContext : DbContext
    {
        public DbSet<Phone> Phones { get; set; }
        public DbSet<Order> Orders { get; set; }
 
        public MobileContext(DbContextOptions<MobileContext> options)
            : base(options)
        {
        }
    }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Iqbol, 2017-05-10
@Ikbolnav

The question is closed !
It turned out that I did not specify the data context file name during the migration:

>dotnet ef migrations add MobileStore --context MobileContext
Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:02.14
Done. To undo this action, use 'ef migrations remove'

>dotnet ef database update

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:03.89
Done.

F
FreeBa, 2017-05-09
@FreeBa

Are migrations applied to the database itself? Show Initializer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question