I'm trying to use Code First and LINQ to read all rows from a table "players".
I already had this working, but then I moved some classes between namespaces (because I was told too) and it's suddenly not working anymore.
It shows a NullReferenceException on "var result = query"
Here's my code
public static List<Player> GetPlayers()
{
using (ScoresContext context = new ScoresContext())
{
var results = from s in context.players
orderby s.name ascending
select s;
List<Player> resultList = new List<Player>();
foreach (var item in results)
{
resultList.Add(new Player(item.Id, item.name, item.nickname));
}
return resultList;
}
}
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class ScoresContext : DbContext
{
public DbSet<Game> games { get; set; }
public DbSet<Player> players { get; set; }
public DbSet<Score> scores { get; set; }
}
My connectionString hasnt changed since it worked so that isn't the problem I think.
public class Player
{
[Key]
public int Id { get; set; }
public string name { get; set; }
public string nickname { get; set; }
public virtual ICollection<Score> scores { get; set; }
public Player()
{
}
public Player(int ID, string name, string nickname)
{
this.Id = ID;
this.name = name;
this.nickname = nickname;
}
}
any advice is welcome, let me know if I forgot something so I can add it
Exception information:
System.NullReferenceException was unhandled
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=MySql.Data.Entity.EF6
StackTrace:
at MySql.Data.MySqlClient.MySqlProviderServices.GetDbProviderManifestToken(DbConnection connection)
at System.Data.Entity.Core.Common.DbProviderServices.GetProviderManifestToken(DbConnection connection)
at MySql.Data.Entity.MySqlManifestTokenResolver.ResolveManifestToken(DbConnection connection)
at System.Data.Entity.Utilities.DbConnectionExtensions.GetProviderInfo(DbConnection connection, DbProviderManifest& providerManifest)
at System.Data.Entity.DbModelBuilder.Build(DbConnection providerConnection)
at System.Data.Entity.Internal.LazyInternalContext.CreateModel(LazyInternalContext internalContext)
at System.Data.Entity.Internal.RetryLazy`2.GetValue(TInput input)
at System.Data.Entity.Internal.LazyInternalContext.InitializeContext()
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.get_InternalContext()
at System.Data.Entity.Infrastructure.DbQuery`1.System.Linq.IQueryable.get_Provider()
at System.Linq.Queryable.OrderBy[TSource,TKey](IQueryable`1 source, Expression`1 keySelector)
at Logic.Handling.GetPlayers() in C:\Users\Jonathan\Source\Repos\1617_CSProgTech_VanDyckJonathan\labo03\Logic\Handling.cs:line 51
at labo03.MainForm.refresh() in C:\Users\Jonathan\Source\Repos\1617_CSProgTech_VanDyckJonathan\labo03\labo03\Form1.cs:line 54
at labo03.MainForm..ctor() in C:\Users\Jonathan\Source\Repos\1617_CSProgTech_VanDyckJonathan\labo03\labo03\Form1.cs:line 19
at labo03.Program.Main() in C:\Users\Jonathan\Source\Repos\1617_CSProgTech_VanDyckJonathan\labo03\labo03\Program.cs:line 23
at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()
InnerException: