0

Here are my models

public class Game
{
     [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    [Required]
    public string  GameName { get; set; }
    public virtual ICollection<GameImages> GameImage { get; set; }


}
 public class GameImages
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }
    [Required]
    [DataType(DataType.ImageUrl)]
    public string GameImageUrl { get; set; }
    public int Game_Id { get; set; }

    [ForeignKey("Game_Id")]
    public virtual Game games { get; set; }

}
 public class GameDbContext:DbContext
    {
        public GameDbContext():base("name=DefaultConnection") { }
        public DbSet<Game> Games { get; set; }
        public DbSet<GameImages> Images { get; set; }

    }

I have two controller with read/write actions using Entity Framework . I add another web api controller with read/write actions using EF . When try to access the GET api I got the following error

<Error>
<Message>An error has occurred.</Message>
<ExceptionMessage>
The context cannot be used while the model is being created.
</ExceptionMessage>
<ExceptionType>System.InvalidOperationException</ExceptionType>
<StackTrace>
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext() at System.Data.Entity.Internal.InternalContext.Initialize() at System.Data.Entity.Internal.InternalContext.GetEntitySetAndBaseTypeForType(Type entityType) at System.Data.Entity.Internal.Linq.InternalSet`1.Initialize() at System.Data.Entity.Internal.Linq.InternalSet`1.ToString() at System.Data.Entity.Infrastructure.DbQuery`1.ToString() at System.Convert.ToString(Object value, IFormatProvider provider) at System.Web.Http.Tracing.FormattingUtilities.ValueToString(Object value, CultureInfo cultureInfo) at System.Web.Http.Tracing.Tracers.HttpActionDescriptorTracer.<ExecuteAsync>b__2(TraceRecord tr, Object value) at System.Web.Http.Tracing.ITraceWriterExtensions.<>c__DisplayClass1b`1.<>c__DisplayClass1f.<TraceBeginEndAsync>b__13(TraceRecord traceRecord) at System.Web.Http.Tracing.SystemDiagnosticsTraceWriter.Trace(HttpRequestMessage request, String category, TraceLevel level, Action`1 traceAction) at System.Web.Http.Tracing.ITraceWriterExtensions.<>c__DisplayClass1b`1.<TraceBeginEndAsync>b__12(TResult result) at System.Threading.Tasks.TaskHelpersExtensions.<>c__DisplayClass3b`2.<Then>b__3a(Task`1 t) at System.Threading.Tasks.TaskHelpersExtensions.ThenImpl[TTask,TOuterResult](TTask task, Func`2 continuation, CancellationToken cancellationToken, Boolean runSynchronously)
</StackTrace>
</Error>

Need some help what is happening here ?

Ghazanfar Khan
  • 3,648
  • 8
  • 44
  • 89

1 Answers1

0

1- Make sure that all your models are mapped and added to the DbContext.

2- Add an initializer to your DBContext and make try to debug the creation of your database.

3- Check this question, or this maybe it helps you.

Community
  • 1
  • 1
Omar.Alani
  • 4,050
  • 2
  • 20
  • 31