0

如果这有点含糊,请原谅我......我正在将 MySql 连接器用于基于实体框架的应用程序。

我的 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 选择,DbExpressionKind expressionKind)+69
MySql.Data.Entity.SelectGenerator .Visit(DbLimitExpression 表达式) +37 System.Data.Common.CommandTrees.DbLimitExpression.Accept(DbExpressionVisitor 1 访问者) +251 visitor) +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 上下文,DbQueryCommandTree 树,类型 elementType,MergeOption mergeOption,Span span,ReadOnlyCollection 1 forMergeOption)+736 系统。 Data.Objects.ObjectQuery 1 forMergeOption) +131 System.Data.Objects.ObjectQuery 1 源) +179 System.Data.Objects.ELinq.ObjectQueryProvider.b__2(IEnumerable 1 查询,表达式 queryRoot) +59 System.Data.Objects.ELinq .ObjectQueryProvider.System.Linq.IQueryProvider.Execute(表达式表达式)+1331 compiledQueryParameters, AliasGenerator aliasGenerator) +453
System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable

1.GetResults(Nullable
1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +36 System.Linq.Enumerable.SingleOrDefault(IEnumerable1 sequence) +41
System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable


System.Data.Entity.Internal.Linq.DbQueryProvider.Execute(Expression expression) +87 System.Linq.Queryable.SingleOrDefault(IQueryable`1 source) +251 SamsCreative.Home.Page_Load(Object sender, EventArgs e) in g:\软件开发\Projects\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,布尔includeStagesAfterAsyncPoint) +772

我真的不知道是什么原因造成的......任何帮助将不胜感激!

4

2 回答 2

1

如“评论”中所述:

嗯...试试这个:

YourDbContext xxx = new YourDbContext();
var query = (from p in xxx select p).FirstOrDefault(); 

首先实例化你的 DbContext。

于 2013-10-06T20:54:54.713 回答
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")
        {
        }
    }
于 2020-08-16T06:53:23.733 回答