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 7888943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T05:58:05+00:00 2026-06-03T05:58:05+00:00

I am adding a row to a table, MyModel, in a database and then

  • 0

I am adding a row to a table, MyModel, in a database and then creating a notification regarding that new row to another table. I need the ID of the newly created MyModel row to properly create my notification.

This is my code from my controller:

MyModel newMyModel = new MyModel
{
    Title = appliedCourse.Title,
};
db.MyModel.Add();

// I need code here that finds the MyModel.ID - but I do not know how to search for this MyModel 
// because MyModel.Title is not necessarily unique - the only necessarily unique piece of data 
// would be the MyModel.ID
new MyModel MyModelWithId = db.MyModel.Find(MyModel) // <- that does not work

Notification newNotification = new Notification
{
    Time = DateTime.Now,
    Link = Url.Action("Details", "MyModel", newMyModelWithId.MyModelID),
    ViewableBy = "Admin",
    Complete = false
};
db.Notification.Add(newNotification);
db.SaveChanges();

Is there any way to retrieve the newly created MyModel’s ID?

I can think of possibly searching for the ‘largest ID value’ and returning that – but I’m not sure if that’s the most effective way.

Is the ID even created at the point when I need it, or do I need to do a db.SaveChanges(); call before searching for it?

Would it be necessary to add a unique value to the MyModel so that I can query the database for it to acquire it’s ID that way? (although the ID is already my unique value for future queries) I was thinking a DateTimeCreated value and I would search for it like this:

new MyModel MyModelWithId = db.MyModel.FirstOrDefault(
                o => o.TimeDateCreated == MyModel.TimeDateCreated);

Not sure if the DateTime method is very efficient either.

  • 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-03T05:58:07+00:00Added an answer on June 3, 2026 at 5:58 am

    You won’t get the ID of the row until after you’ve save the changes.

    In your example you’d have to hit SaveChanges twice. Once to save MyModel and generate the ID, then to save newNotification with the URL.Action.

    MyModel newMyModel = new MyModel
    {
        Title = appliedCourse.Title,
    };
    db.MyModel.Add();
    
    // Save first, let the DB generate the ID.  
    // The DB generated ID will automatically load into the object you just saved.
    db.SaveChanges(); 
    
    Notification newNotification = new Notification
    {
        Time = DateTime.Now,
        Link = Url.Action("Details", "MyModel",new { id = newMyModel.ID }),
        ViewableBy = "Admin",
        Complete = false
    };
    db.Notification.Add(newNotification);
    db.SaveChanges();
    

    Mind you, unless you’ve got a really compelling reason, I might generate the the Url.Action when the new notification is being displayed and not as part of the model itself. Instead, you could hold a reference and that way only save once. That being said, that’s a rough guess without knowing your use case.

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

Sidebar

Related Questions

I am trying to animate a table row sliding up, then adding a new
I'm adding a new Row to a BindingSource that is Bound to a DataGridView
My problem is that I am adding a row to a HTML table dynamically
I'm programmatically adding a row to a System.Web.UI.WebControls.Table inside an UpdatePanel. I then add
Should be easy. I'm adding a button to each table row, that gets information
Which method is preferred for adding a row to a table? var tr =
I'm adding a select box to a form by cloning the current table row,
I have trouble with adding new row to my UITableView. I read similar questions
I am adding a new blank row to a dojox.grid.DataGrid, and want to focus
I am adding an activity indicator in each row of table. The issue is

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.