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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:22:38+00:00 2026-06-04T00:22:38+00:00

I am trying to update an object of class Transaction public int Id {

  • 0

I am trying to update an object of class Transaction

    public int Id { get; set; }
    public System.DateTime Date { get; set; }
    public string Remarks { get; set; }
    public double Amount { get; set; }
    public Nullable<long> GroupId { get; set; }
    public long UserId { get; set; }

    public virtual ICollection<User> PaidForUsers { get; set; }
    public virtual User PaidByUser { get; set; }

The element PaidForUsers is a collection of the User object

    public long Id { get; set; }
    public long UserId { get; set; }

Here Id is auto generated and UserId is to be supplied by code.

What I am trying to do is, whenever I create a new transaction, I should create a new User only if the user is not present already in the database.

I am currently using DbContext.Transactions.Add(new transaction). This obviously creates a new user in the database everytime I create a new transaction.

The solution that I have adopted currently is to use a custom stored procedure only for the User Entity which tries to do the same thing. But I get the following error in that also

The changes to the database were committed successfully, but an error occurred while updating the object context. The ObjectContext might be in an inconsistent state. Inner exception message: AcceptChanges cannot continue because the object’s key values conflict with another object in the ObjectStateManager. Make sure that the key values are unique before calling AcceptChanges.

Here is the stored procedure –

ALTER PROCEDURE [dbo].[InsertUser]
    (@UserId bigint )
AS
declare @val bigint
declare @countza int

SELECT @val= Id  FROM dbo.Users1 where UserId = @UserId
set @countza = @@Rowcount

    if @countza = 0
    begin
    insert into dbo.Users1 (UserId) values (@UserId)
    select [Id] from [dbo].[Users1] 
            where @@ROWCOUNT > 0 and [Id] = scope_identity() 

    end
    else
    begin
        SELECT  'Id' = @val
    end

Can some one tell what is the best way to do this? Is creating a stored procedure the right way to go about it?

  • 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-04T00:22:41+00:00Added an answer on June 4, 2026 at 12:22 am

    I’d say the right way – under “normal” circumstances – is to use LINQ:

    (I’m assuming that UserId must be unique, but is not the primary key (which is Id))

    var transaction = context.Transactions.Create();
    var user = context.Users.SingleOrDefault(u => u.UserId == userId);
    if (user != null)
        transaction.PaidForUsers.Add(user);
    else
        transaction.PaidForUsers.Add(new User { UserId = userId });
    context.Transactions.Add(transaction);
    context.SaveChanges();
    

    If your operation is very performance critical, then the right way might be a Stored Procedure.

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

Sidebar

Related Questions

I have an object: public class TestEvent : IEvent { private string _id; public
I am trying to get definition of method :foo from Class object. class Bar
I have two classes: class Object { public: Object(); virtual void update(); virtual void
I am trying to update user in my model object public ActionResult AddJob(JobQueue job,HttpPostedFileBase
So I'm trying to update an object in my MS SQL 2005 database using
I am getting the following error when trying to update an object using nhibernate.
I'm trying to find the best way to update an object which could be
I am trying to write a page in asp.net to update an user object
When trying to update a subversion working copy from Netbeans, I get the following
I'm trying to attach a ADO EF object class (Materials) to a ListBox, and

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.