0

Forgive me if this is somewhat vague...I am using MySql connector for an Entity Framework based application.

I have a record in my Content table, which I am trying to fetch, but Whenever it tries to get the record, I'm getting this exception:

Object reference not set to an instance of an object.

at this line:

Data.Entities.Content content = this.SiteData.Content.Take(1).SingleOrDefault();

I've checked that SiteData is an instance (...it is), however when I inspected Content, it appeared not to have any records, so I'm assuming that Take(1) fails...I could be wrong.

Here is the stack trace:

[NullReferenceException: Object reference not set to an instance of an object.]
MySql.Data.Entity.SelectStatement.GetDefaultColumnsForTable(TableFragment table) +64
MySql.Data.Entity.SelectStatement.GetDefaultColumnsForFragment(InputFragment input) +90
MySql.Data.Entity.SelectStatement.AddDefaultColumns(Scope scope) +87
MySql.Data.Entity.SelectStatement.Wrap(Scope scope) +37
MySql.Data.Entity.SelectGenerator.WrapIfNotCompatible(SelectStatement select, DbExpressionKind expressionKind) +69
MySql.Data.Entity.SelectGenerator.Visit(DbLimitExpression expression) +37 System.Data.Common.CommandTrees.DbLimitExpression.Accept(DbExpressionVisitor1 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
1 visitor) +25
MySql.Data.Entity.SelectGenerator.GenerateSQL(DbCommandTree tree) +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
System.Data.Objects.ELinq.ELinqQueryState.GetExecutionPlan(Nullable
1 forMergeOption) +736
System.Data.Objects.ObjectQuery1.GetResults(Nullable1 forMergeOption) +131
System.Data.Objects.ObjectQuery1.System.Collections.Generic.IEnumerable<T>.GetEnumerator() +36 System.Linq.Enumerable.SingleOrDefault(IEnumerable1 source) +179 System.Data.Objects.ELinq.ObjectQueryProvider.b__2(IEnumerable1 sequence) +41
System.Data.Objects.ELinq.ObjectQueryProvider.ExecuteSingle(IEnumerable
1 query, Expression queryRoot) +59
System.Data.Objects.ELinq.ObjectQueryProvider.System.Linq.IQueryProvider.Execute(Expression expression) +133
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:\Software Development\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(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +772

I'm really at a loss as to what is causing this...any help will be much appreciated!

5
  • Try var content = this.SiteData.Content.Take(1).SingleOrDefault();; Another thing, what's the content of this? Commented Oct 6, 2013 at 20:35
  • @Tico...Okay, I've tried using var instead of specifying the exact type...same problem!. Content "should" refer to a simple entity consisting of some strings and a few decimals. this.SiteData is a reference to the DbContext instance Commented Oct 6, 2013 at 20:40
  • Hum...Try this: YourDbContext xxx = new YourDbContext(); var query = (from p in xxx select p).FirstOrDefault(); Instantiate you DbCOntext first. Commented Oct 6, 2013 at 20:46
  • Ah! FirstOrDefault(); did the trick! :-) Thank you! Commented Oct 6, 2013 at 20:51
  • Nice! Glad I could help! Commented Oct 6, 2013 at 20:52

2 Answers 2

1

As stated in the "comments":

Hum...Try this:

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

Instantiate you DbContext first.

Sign up to request clarification or add additional context in comments.

Comments

0

You should add dbConfigurationType attribute on your dbContext.

    [DbConfigurationType(typeof(MySqlEFConfiguration))]
    public class MysqlDbContext: DbContext
    {
        //Add your Dbsets here

        public MysqlDbContext()

            //Reference the name of your connection string
            : base("ConnectionStringName")
        {
        }
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.