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

  • Home
  • SEARCH
  • 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 8071689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T13:46:09+00:00 2026-06-05T13:46:09+00:00

MVC3 and Entity framework newbie here. I’m trying to write a controller that will

  • 0

MVC3 and Entity framework newbie here. I’m trying to write a controller that will handle both edits and creating new entries. Here is my code so far (most of it auto generated)

    //POST: /License/Save
    [HttpPost]
    public string Save(LICENSE_USER license_user)
    {
        if (license_user.License_ID != 0)
        {
            if (ModelState.IsValid)
            {
                db.Entry(license_user).State = EntityState.Modified;
                db.SaveChanges();
                return "Save Complete";
            }
        }
        else
        {

            if (ModelState.IsValid) {
                db.LICENSE_USER.Add(license_user);
                db.SaveChanges();
                return "New License created - " + license_user.License_ID;
            }
        }
        return "What's going on here?";
    }

It works great for edits since I’m including the License_ID field in the POST. For new entries, however, I’m excluding the License_ID field and it defaults to 0. What’s the most elegant way to get the next License_ID field and updating the object with that field?

  • 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-06-05T13:46:10+00:00Added an answer on June 5, 2026 at 1:46 pm

    Make the License_ID column in your database a primary autoincrement key. This way you don’t need to set it manually when inserting a new entity. The database will take care.

    In your EDMX model the StoreGeneratedPattern attribute must be set to Identity for this property.

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

Sidebar

Related Questions

I have a MVC3 project that uses the Entity Framework and Ninject v2.2, and
Assume we have an ASP.NET MVC3 Web site that makes use of Entity Framework
I am trying to setup editing functionality using MVC3 and entity framework 4. I
I'm new to asp.net, mvc3 and entity framework. I'm trying to develop a mvc3
Using MVC3 and Entity Framework. Am trying to get validation flowing from data model
I am trying to use MVC3 with the Entity framework with Code First, but
I'm using Entity Framework and MVC3, and my problem is that I can't scaffold
I'm creating an MVC3 asp.net application using Entity Framework 4 and C#. I've tried
I am using MVC3 and Entity Framework. I have a class called User with
I am using MVC3 and Entity Framework. I have a List on a page

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.