Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 508803
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T06:56:43+00:00 2026-05-13T06:56:43+00:00

I am using Entity Framework with asp.net mvc, but I don’t think mvc plays

  • 0

I am using Entity Framework with asp.net mvc, but I don’t think mvc plays a big role here.

I have an object Customer, and a lookup table (there are several, and they all behave the same way; so for simplicity I’ll pick Territory). Both Customer and Territory have LastUpdated field (Datetime that is set manually in the code).

If I hardcode Territory, and get only Customer data from the View, I don’t have any problems:

public ActionResult CreateCustomer([Bind(Exclude = "CustId")] Customer cm) {
    cm.Territory = (from t in repo.Territory where t.ID == 2 select t).First();
    repo.AddToCustomer(cm);
    repo.SaveChanges();
}

As I said, no problems. However, if I use a dropdown in the view with the matching id (Territory.ID) – there is a problem. I have the following line in controller:

ViewData["territory"] = new SelectList(repo.Territory, "ID", "Name", 1);

and corresponding line in the View:

Territory: <%= Html.DropDownList("Territory.ID", (IEnumerable<SelectListItem>)ViewData["territory"])%>

I get good news and bad news: the good news is that I get the territory ID nicely assigned to the appropriate member of Customer object. The bad news is that Territory.LastUpdated value is set to 1/01/0001. Obviously, I don’t care about this value – but it looks like EF cares. When I call SaveChanges I am getting the following error:

SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM

Looks like EF is trying to retrieve the value from the database, and then compare it with EF value… or maybe something else – but the bottom line is I can’t figure out how to prevent it from trying to be so smart.

For now, I name DropDown ID to be something different (“terID” instead of “Territory.ID”), and using FormCollection:

int terID = Int32.Parse(formData["terID"]);
cm.Territory = (from d in repo.Territory
     where d.ID == terID select d).First();

This works (which makes me comfortable with my analysis) but this cannot be the best way.
I also can’t believe that nobody bumped into such problem – but I couldn’t find anything relevant… The best I could find is link text but it’s more of a hint without much details

I tried to cut out all unrelated stuff from the code – so if there are some typos, they are in the post; not necessarily in the code itself!
Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T06:56:43+00:00Added an answer on May 13, 2026 at 6:56 am

    I’m assuming you are using EF 1.0 which is not good at direct foreign key maping.

    Territory posted from view to action is not bound to Datacontext, hence when you save your customer object – EF saves attached Territory Object as well.

    You have to get Territory object from db first and then assign it to customer.

    And Your solution is perfectly fine, cos there is no other for EF 1.0 )-:

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ASP.NET MVC 2.0 application using Entity Framework. All my views use
The setup: (using Asp.Net MVC 2 RC, Entity Framework, SQL Server, VS2008) My buddy
Using ASP.NET MVC and Entity Framework, I encounter some attach/detach errors when I need
I am working in ASP.Net MVC 1.0 and SQL Server 2000 using Entity Framework.
I'm in ASP.NET MVC and am (mostly) using Entity Framework. I want to call
I crated an app with asp.net mvc using entity framework 4 and sql server
I'm running ASP.NET MVC 2 Preview 2 (With VS 2010 Beta 2) using Entity
Or I don't understand this at all. I have started my ASP.NET MVC application
I am using Entity Framework with my website. To improve performance, I have started
I have a problem with the following Linq query using Entity Framework: from o

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.