11

I have two tables, Quote and Agent. Quote has a column called AgentID that is a foreign key in Agent.

When adding the tables to my model in the VS the Quote class has a reference to Agent.

When attempting to add a new quote I create a new quote entity and set the Agent like this:

entity.Agent = (from x in entities.AgentEntities 
    where x.AgentID == quote.AgentID select x).FirstOrDefault();

Right before SaveChanges is called I examine the object and see that all of the values are set. The Agent object has all of its values set. I even checked the EntityKey property and it is set.

Despite the values being there I am getting this error:

Cannot insert the value NULL into column 'AgentID', table 'Database.dbo.Quote'; 
column does not allow nulls. INSERT fails.

I am not sure what else to check, perhaps there is a way to view the SQL?

EDIT: I am using the repository pattern in my application. I use PONO's in my application then create new entity objects. When I save a new quote I call this method:

public override void CreateQuote(Quote quoteToCreate)
{
  var entity = ConvertQuoteToQuoteEntity(quoteToCreate);
  entities.AddToQuoteEntities(entity);
  entities.SaveChanges();  //Error is thrown here
}

private QuoteEntity ConvertQuoteToQuoteEntity(Quote quote)
        {
            var entity = new QuoteEntity();

            if (quote != null)
            {
                entity.QuoteID = quote.QuoteID;
                entity.DiscoveryMethod = quote.DiscoveryMethod;
                entity.CompletedDateTimeStamp = quote.CompletedDateTimeStamp;
                entity.CommisionAmount = quote.CommisionAmount;
                entity.QuoteKey = quote.QuoteKey;
                entity.SelectedOption = quote.SelectedOption;
                entity.SentDateTimeStamp = quote.SentDateTimeStamp;
                entity.CustomerName = quote.CustomerName;
                entity.CustomerEmail = quote.CustomerEmail;
                entity.CustomerPrimaryPhone = quote.CustomerPrimaryPhone;
                entity.CustomerAlternatePhone = quote.CustomerAlternatePhone;
                entity.Agent = (from x in entities.AgentEntities where x.AgentID == quote.AgentID select x).First<AgentEntity>();
            }
            return entity;  //Everything looks good here (Agent is fully populated)
        }

Here is something odd. I was able to see the SQL generated and it looks strange to me:

insert [dbo].[Quote]([QuoteKey], [CommisionAmount], [QuoteRequestID], [DiscoveryMethod], [SelectedOption], [CreatedDateTimeStamp], [SentDateTimeStamp], [CompletedDateTimeStamp], [CustomerName], [CustomerEmail], [CustomerPrimaryPhone], [CustomerAlternatePhone])
values (@0, null, null, @1, null, @2, null, null, @3, @4, @5, @6)
select [QuoteID], [AgentID]
from [dbo].[Quote]
where @@ROWCOUNT > 0 and [QuoteID] = scope_identity()
5
  • Can you show more of the code? You can view the SQL with SQL Profiler. Feb 16, 2010 at 20:29
  • 1
    Based on the INSERT, it's not setting the Quote.AgentId field at all, hence the NULL. Since you say the entity.Agent is non-NULL, this means that likely either the Agent property is mapped wrong or the EF doesn't think you've changed it. The former seems more likely. I'd suggest double-checking the mapping. Feb 17, 2010 at 21:43
  • 1
    Well, I have decided to 'punt' and switch over to Linq 2 SQL. From my experience EF is to complex or just not quite ready. Or there is something fundamental that I just don't get. Feb 18, 2010 at 19:45
  • Usually, when people have a hard time getting the EF to work they are writing way too much code -- using multiple ObjectContexts concurrently, fiddling with the mapping more than necessary, etc. My best advice is to start with very simple things which work and build from there. Feb 19, 2010 at 15:31
  • 1
    UPDATE: I have since upgraded to .NET 4 and everything works very well there - I am not having the issue described above. Jul 19, 2011 at 21:20

2 Answers 2

6

Let me preface by saying that I'm using ASP.NET MVC - if you're writing a single-user desktop-application, your problem may be completely different from mine.

In my case, I had apparently come up with a bad solution to a problem I had earlier.

I was creating multiple instances of my object-context - and when you create entities with one instance, and try to associate them with an entity created by another instance, you get weird errors.

To work around that, I thought, well, I'll just make sure I have only one object-context. So I created a class with a public getter, which would create the instance if not already created, and return it. Kind of a singleton pattern, ensuring that I had one object-context for the entire application.

In my app, I occasionally create "throw-away" objects - e.g. a temporary entity, sometimes pre-populated with a few default values, just so I can render a form. When the form is submitted, a new object is created, populated, validated, and then saved.

The save would fail though - giving the error described on this page, about some attribute being empty, even though I had populated all the fields, and the entity passed validation.

The problem was, it wasn't trying to save the object I just created - it was hanging on to the "throw-away" object from the previous request, and trying to save that one first.

Coming from PHP, what threw me off here was the realization that ASP.NET MVC apps have a very different lifecycle from PHP apps. In PHP, scripts start, process a request, end then they end - whereas in ASP.NET, they start, run for a while, serving many requests, and then, eventually, they end and restart.

By creating my object-context in a static method, I was creating not one instance per request - but one instance per application. Because the object-context persists between requests, my "throw-away" entities will pile up - and eventually, when I try to SaveChanges(), it will of course fail.

This confusion stems in part from the fact that the Entity Framework was written with desktop applications in mind - it was not designed for the life-cycle of a web application.

You can work around this, and here's the solution:

public class App
{
    public static MyEntities DB
    {
        get {
            // Create (as needed) and return an object context for the current Request:

            string ocKey = "MyEntities_" + HttpContext.Current.GetHashCode().ToString("x");

            if (!HttpContext.Current.Items.Contains(ocKey))
                HttpContext.Current.Items.Add(ocKey, new MyEntities());

            return HttpContext.Current.Items[ocKey] as MyEntities;
        }
    }
}

You can now the your object-context from anywhere:

MyEntities DB = MyNamespace.App.DB;

I found the solution in this lengthy article, which explores several (right and wrong) ways to manage the lifecycle of an object-context:

http://dotnetslackers.com/articles/ado_net/Managing-Entity-Framework-ObjectContext-lifespan-and-scope-in-n-layered-ASP-NET-applications.aspx

1
  • Although it is exceedingly unlikely, .GetHashCode() is not guaranteed to return a unique value for each instance of an object. It would be better to use a session ID. Another solution is to add a property to the HttpApplication object, write a static property which can get it with something like HttpContext.Current.Application as Global, and use events to set/unset the property. (any given HttpApplication instance will only be associated with a single request at a time, but may be reused for another later; hence why unsetting/disposing is important) Or just use an IoC container :)
    – Taudris
    Apr 26, 2013 at 23:29
0

Are you checking the value of entity.Agent? I suspect that the null comes from FirstOrDefault() when it encounters a query that returns no records.

Even if the agent object has all of its values set, that won't matter if entity doesn't have a reference to the agent object.

1
  • entity.Agent is fully populated. Feb 16, 2010 at 20:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.