请原谅我,如果这有点模糊......我正在使用MySql连接器来实现基于Entity Framework的应用程序。
我的Content表中有一条记录,我试图获取该记录,但每当它试图获取记录时,我都会遇到此异常:
对象引用未设置为对象的实例。
在这一行:
Data.Entities.Content content = this.SiteData.Content.Take(1).SingleOrDefault();
我已经检查过SiteData是一个实例(......它是),但是当我检查Content
时,它似乎没有任何记录,所以我假设Take(1)
失败......我错了。
这是堆栈跟踪:
[NullReferenceException:对象引用未设置为的实例 对象。]
MySql.Data.Entity.SelectStatement.GetDefaultColumnsForTable(TableFragment 表)+64
MySql.Data.Entity.SelectStatement.GetDefaultColumnsForFragment(InputFragment 输入)+90
MySql.Data.Entity.SelectStatement.AddDefaultColumns(范围范围)+87
MySql.Data.Entity.SelectStatement.Wrap(范围范围)+37
MySql.Data.Entity.SelectGenerator.WrapIfNotCompatible(SelectStatement select,DbExpressionKind expressionKind)+69
MySql.Data.Entity.SelectGenerator.Visit(DbLimitExpression表达式) +37 System.Data.Common.CommandTrees.DbLimitExpression.Accept(DbExpressionVisitor1 visitor) +25
1 访客)+25
MySql.Data.Entity.SqlGenerator.VisitInputExpression(DbExpression e, String name, TypeUsage type) +35
MySql.Data.Entity.SelectGenerator.VisitInputExpressionEnsureSelect(DbExpression e, String name, TypeUsage type) +21
MySql.Data.Entity.SelectGenerator.Visit(DbProjectExpression expression) +38
System.Data.Common.CommandTrees.DbProjectExpression.Accept(DbExpressionVisitor
MySql.Data.Entity.SelectGenerator.GenerateSQL(DbCommandTree树)+60 MySql.Data.MySqlClient.MySqlProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest,DbCommandTree commandTree)+329
System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree)+125
System.Data.EntityClient.EntityCommandDefinition..ctor(DbProviderFactory storeProviderFactory,DbCommandTree commandTree)+1411
System.Data.EntityClient.EntityProviderServices.CreateCommandDefinition(DbProviderFactory storeProviderFactory,DbCommandTree commandTree)+78
System.Data.EntityClient.EntityProviderServices.CreateDbCommandDefinition(DbProviderManifest providerManifest,DbCommandTree commandTree)+159
System.Data.Common.DbProviderServices.CreateCommandDefinition(DbCommandTree commandTree)+125
System.Data.Objects.Internal.ObjectQueryExecutionPlan.Prepare(ObjectContext的 context,DbQueryCommandTree tree,Type elementType,MergeOption mergeOption,Span span,ReadOnlyCollection1 compiledQueryParameters, AliasGenerator aliasGenerator) +453
1 forMergeOption)+736
System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable
System.Data.Objects.ObjectQuery1.GetResults(Nullable
1 forMergeOption)+131
System.Data.Objects.ObjectQuery1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +36 System.Linq.Enumerable.SingleOrDefault(IEnumerable
1 source)+179 System.Data.Objects.ELinq.ObjectQueryProvider.b__2(IEnumerable1 sequence) +41
1 查询,表达式queryRoot)+59
System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable
System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(表达式 表达式)+133
System.Data.Entity.Internal.Linq.DbQueryProvider.Execute(表达式 表达式)+87 System.Linq.Queryable.SingleOrDefault(IQueryable`1 来源)+251 SamsCreative.Home.Page_Load(对象发送者,EventArgs e)在g:\ Software中 开发\项目\ SamsCreative \ SamsCreative \ Home.aspx.cs:17个
System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)+51 System.Web.UI.Control.OnLoad(EventArgs e)+92
System.Web.UI.Control.LoadRecursive()+54
System.Web.UI.Page.ProcessRequestMain(布尔 includeStagesBeforeAsyncPoint,Boolean includeStagesAfterAsyncPoint) 772
我真的不知道是什么造成了这种情况......任何帮助都会非常感激!
答案 0 :(得分:1)
如“评论”中所述:
嗯......试试这个:YourDbContext xxx = new YourDbContext();
var query = (from p in xxx select p).FirstOrDefault();
首先实例化DbContext。
答案 1 :(得分:0)
您应该在dbContext上添加dbConfigurationType属性。
[DbConfigurationType(typeof(MySqlEFConfiguration))]
public class MysqlDbContext: DbContext
{
//Add your Dbsets here
public MysqlDbContext()
//Reference the name of your connection string
: base("ConnectionStringName")
{
}
}