0

I have added a new column IsForceLogOff (datatype = Bit). When I update the table in the usual way, everything updates except the newly added bool column.

public static UserErrorStatus UserUpdate(User user, Company company)
{
    UserErrorStatus status = UserErrorStatus.Error;

    using (OAPDataLayerEntities DbEntity = GetDBContext())
    {
        try
        {
            using (TransactionScope transaction = new TransactionScope())
            {
                user.IsForceLogOff = true;
                DbEntity.Users.Attach(user);
                DbEntity.ObjectStateManager.ChangeObjectState(user, EntityState.Modified);

                DbEntity.SaveChanges();
                transaction.Complete();

                DbEntity.AcceptAllChanges();
                status = UserErrorStatus.Success;
            }
        }
    }
}

enter image description here

Create table statement:

CREATE TABLE [dbo].[User]
(
    [UserID] [int] IDENTITY(1,1) NOT NULL,
    [AddressID] [int] NULL,
    [AccountTypeID] [int] NOT NULL,
    [StaffID] [int] NULL,
    [SalutationID] [int] NULL,
    [FirstName] [nvarchar](50) NOT NULL,
    [LastName] [nvarchar](50) NOT NULL,
    [EmailAddress] [nvarchar](100) NOT NULL,
    [Password] [nvarchar](50) NOT NULL,
    [SecurityQuestionID] [int] NOT NULL,
    [SecurityAnswer] [nvarchar](50) NOT NULL,
    [PhoneNumber1] [nvarchar](50) NULL,
    [PhoneNumber2] [nvarchar](50) NULL,
    [Fax] [nvarchar](50) NULL,
    [CompanyID] [int] NULL,
    [DateCreated] [smalldatetime] NOT NULL,
    [DateModified] [smalldatetime] NOT NULL,
    [DateLastLogin] [smalldatetime] NOT NULL,
    [UserIDModified] [int] NULL,
    [StatusID] [int] NOT NULL,
    [Notes] [ntext] NULL,
    [IsForceLogOff] [bit] NOT NULL
)

Refer to the above sql

8
  • Show us the CREATE TABLE table definition, user entity and the onconfiguring for a user Commented Nov 13, 2021 at 5:13
  • @CaiusJard Updated the question with screenshot of entity details Commented Nov 13, 2021 at 5:36
  • I was more hoping for a right click table>>script as>>create>>to clipboard, and then paste text.. also, I asked for 3 things and you provided 1 Commented Nov 13, 2021 at 6:10
  • @CaiusJard I have updated the question. Please note that there is an issue with how I am updating the table and not in the table structure. Appreciate your answer on the C# code that I have written to update Commented Nov 13, 2021 at 6:30
  • Yes.. Providing the table create allows us to a) see if something like a default could be giving trouble and b) exactly replicate your table so we can easily make one in our machine and try to reproduce/fix your issue Commented Nov 13, 2021 at 6:39

1 Answer 1

1

The typical suspect is that at runtime the application is using a different database instance than the one you are checking. With a breakpoint verify the connection string used /w var conn = DbEntity.Database.Connection.ConnectionString (EF6) or DbEntity.Database.GetDbConnection().ConnectionString for pre-EFCore 5, or DbEntity.Database.ConnectionString for EF Core 5. (thank you Microsoft...)

In your example, the use of the TransactionScope is completely unnecessary, and in cases where a TransactionScope would be practical, you have them the wrong way around. TransactionScope should be the outer-most boundary, with the using (var DbEntity = ... being declared inside the scope.

TransactionScopes add an additional cost to processing and would be used to coordinate transactions between the DbContext operations and other external operations, such as operations with other DbContexts or other services etc. that comply with transaction commit/rollback strategies. DbContexts inherently operate with a Transaction internally so it doesn't matter how many entities you update within the scope of a DbContext and it's SaveChanges call, they will be saved within a single transaction.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.