-1
\$\begingroup\$

I just got my question closed

How get all id from the db insert

Accordingly to message my question isn't on topic because my code doesn't work. But my code does work I just don't want to send so many separated request to db.

Was that a generic message or a personalized one. Because I dont understand how can improve my question. I read https://codereview.stackexchange.com/help/on-topic and feel like match all conditions.

\$\endgroup\$
7
\$\begingroup\$

There are several problems with your question:

  • There is no language tag. Is it ?
  • It lacks context. What does the code for db.images.Add(…) and db.SaveChanges() do, exactly? We can't see the code.
  • It does not conform to the How to Ask guidelines. The title, in particular, should state the task that the code performs.

Since your question omitted the contextual code, and the title asked the specific question "How get all id from the db insert", I could only assume that the code was not working correctly as intended, and that you were asking a specific question about how to accomplish the task of getting all IDs from inserted rows. Such a "how do I…?" question would be off-topic for Code Review.

I suggest that you edit the question to clarify what the code is doing, and retitle the question to conform to site standards.

\$\endgroup\$
  • \$\begingroup\$ Ok, I forgot the language tag. Is C#. But the function are the autogenerated function from C# entity framework to handle the table object. And in the question text already explain what is the problem and what want to improve. I already explain what the code is doing, but not sure how improve the title. \$\endgroup\$ – Juan Carlos Oropeza Aug 21 '17 at 22:16
  • \$\begingroup\$ I'm not sure if I really need to explain what db.images.Add(…). Those are function created for the EF wizards when building the connection to the DB. \$\endgroup\$ – Juan Carlos Oropeza Aug 21 '17 at 22:21
  • 1
    \$\begingroup\$ If that the question is about autogenerated code, then that is also problematic, as you would be asking us to review code that you didn't write yourself. What is the real source of the code, then? \$\endgroup\$ – 200_success Aug 21 '17 at 22:23
  • \$\begingroup\$ Nope my question is about the algorithm. Right now I have to Insert the image row in the db to get one image_id, and then can use the id to create the image image_id.jpeg the problem is I have to save in db (send a round trip to the db) for each image. \$\endgroup\$ – Juan Carlos Oropeza Aug 21 '17 at 22:30
  • \$\begingroup\$ Actually those are basic stuff for C# and EF problems. But those are questions for the actual question. Let me know what else you think I neet to add for reopen it. \$\endgroup\$ – Juan Carlos Oropeza Aug 21 '17 at 22:37
4
\$\begingroup\$
public DataTable dtImages = new DataTable();

// EF Context for the DB
public CameraCollectorEntities db = new CameraCollectorEntities();

These are public fields, class-level scope.

....

foreach (DataRow userRow in dtImages.Rows)
{
    image newImage = new image

Then you have a .... and then suddenly we're in method-level scope, in some nameless method that has this foreach loop.

The mixed scopes make the code block confusing. The comments seem to have been added for the sake of the post, for Code Review readers - don't do that: on this site everything in a code block is up for review - that includes class and method names; it also includes the fact that your class is exposing public disposable fields, and the fact that you have comments describing what the code does and/or what the variables are: what reviewers want to see, is the code exactly as (or as close as possible to) the way you have it in your IDE.

This means reviewers need to see what's behind these .... dots, and what you're not showing them, e.g. the class definition and the method's signature:

public class WhateverTheClassNameIs
{
    public DataTable dtImages = new DataTable();
    public CameraCollectorEntities db = new CameraCollectorEntities();

    public void WhateverTheMethodNameIs()
    {
        // where's dtImages coming from? include that code!
        foreach (DataRow userRow in dtImages.Rows)
        {
            /* the **real, actual** code */
        }
    }
}

Why?

You're asking a rather specific question, a question which is in itself a better fit for Stack Overflow. "How to get ID from inserted entity" is a specific programming issue that's answered on Stack Overflow.

Code Review isn't Stack Overflow.

On this site you'll get answers that will show you better ways to solve the problem you're solving, but also extremely useful feedback about things you thought were irrelevant to your question.

For example, these public fields are IDisposable objects, and that is a problem, because the object that's newing them up is (apparently) not the object that's Disposeing them: there are serious implications to this, and learning to properly Dispose disposable types is essential to writing quality .NET/C# code, and therefore it will be brought up in a peer review on this site.

As I tried to explain yesterday, giving us a tiny little out-of-context snippet does not allow reviewers to properly review any cringe-worthy parts of your code; the presence of added-in-the-post-but-not-in-the-real-code comments and .... strongly suggests that reviewers are looking at a block of code that may likely look completely different in your IDE, with different identifier names, different comments, more fields; if these .... dots stand for 400 lines of code, you'll never get feedback pointing out how much the Single Responsibility Principle is taking a beating with that code.

Indeed, leaving out parts of the code isn't a problem. But IMO your post simply doesn't include enough of the real code to be clearly reviewable - and worth the potential reviewers' time.

Your question will be reopened when it includes enough information & contextual code to be reviewable. As it stands it looks like a question that should be trimmed down to a proper MCVE and posted on Stack Overflow.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .