linq结果null引用

时间:2017-04-26 13:48:59

标签: c# mysql linq

我正在尝试使用Code First和LINQ来读取表“player”中的所有行。

我已经有了这个工作,但后来我在命名空间之间移动了一些类(因为我也被告知)并且它突然不再工作了。

它在“var result = query ”上显示NullReferenceException

这是我的代码

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; }
    }

我的connectionString没有改变,因为它工作,所以这不是我想的问题。

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; 
    }
}

欢迎任何建议,如果我忘记了某些内容,请告诉我,以便我可以添加

异常信息:

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:

0 个答案:

没有答案