2
votes

Im using EF Core and Im trying to update a column/property of an entity.

The column has a foreign key constraint...is nullable and is an int.

The same table/entity has three or four other columns/properties the same datatype also foreign key constrained...and nullable

When I update the values of any of these column using the Update command...It works perfectly fine...EXCEPT for one column. When I try to update that one column and i process the update it will save all changes...but that column it just reverts it back to its previous value.

It throws no exceptions..no errors of any kind...just reverts it back to the original value and proceeds.

Below is the context entry for the entity....the column in question is the car_app_id

there is nothing in the context file that differentiates that specific column as being different from the other...nothing in the foreign key constraints that differs from the other columns...

            {
            entity.HasKey(e => e.AppId)
                .HasName("PK_tbl_apps");

    entity.ToTable("tbl_apps");

            entity.Property(e => e.AppId).HasColumnName("app_id");

    entity.Property(e => e.Active).HasColumnName("active");

    entity.Property(e => e.AppAcro)
                .HasColumnName("app_acro")
                .HasColumnType("varchar(50)");

    entity.Property(e => e.AppDesc)
                .HasColumnName("app_desc")
                .HasColumnType("varchar(5000)");

    entity.Property(e => e.AppTypeId).HasColumnName("app_type_id");

    entity.Property(e => e.BuildTypeId).HasColumnName("build_type_id");

    entity.Property(e => e.CarAppId).HasColumnName("car_app_id");       

    entity.Property(e => e.ControlLevelId).HasColumnName("control_level_id");     

    entity.Property(e => e.Deleted).HasColumnName("deleted");

    entity.HasOne(d => d.AppType)
                .WithMany(p => p.TblApps)
                .HasForeignKey(d => d.AppTypeId)
                .HasConstraintName("FK_tbl_apps_tbl_app_types");

    entity.HasOne(d => d.BuildType)
                .WithMany(p => p.TblApps)
                .HasForeignKey(d => d.BuildTypeId)
                .HasConstraintName("FK_tbl_apps_tbl_build_types");

    entity.HasOne(d => d.CarApp)
                .WithMany(p => p.TblApps)
                .HasForeignKey(d => d.CarAppId)
                .HasConstraintName("FK_tbl_apps_tbl_car_apps");

    entity.HasOne(d => d.ControlLevel)
                .WithMany(p => p.TblApps)
                .HasForeignKey(d => d.ControlLevelId)
                .HasConstraintName("FK_tbl_apps_tbl_control_level");


});

This is the code I use to test..this is simple test code trying to figure out why the update isnt working. Its very basic repository

public class Repository<TEntity> : IRepository<TEntity> where TEntity : class
    {
        private omni_dbContext context { get; set; }
        private DbSet<TEntity> dbset { get; set; }
        public Repository()
        {
            try
            {
                context = new omni_dbContext();
                dbset = context.Set<TEntity>();

            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally { }
        }
        public IEnumerable<TEntity> Get(Expression<Func<TEntity, bool>> filter = null,
           params Expression<Func<TEntity, object>>[] includeProperties)
        {
            try
            {
                IQueryable<TEntity> query = dbset;
                if (filter != null)
                {
                    query = query.Where(filter);
                }
                if (includeProperties != null)
                {
                    foreach (var includeProperty in includeProperties)
                    {
                        query = query.Include(includeProperty);
                    }
                }
                return query.ToList();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally { }
        }



        public void Update(TEntity entity)
        {
            try
            {
                context.Update(entity);
                context.SaveChanges();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
            finally { }
        }


    }
}

So I use a Manager class for the Entity to instantiate the repository and make my data calls.

Basically I get a set of records..pick out a record...update the properties and send it back to update and every field will update just fine..but the entity resets the car_app_id property to whatever it is prior to my modifying it and I do not understand why.

 EalmsEF.AppManager manager = new EalmsEF.AppManager();
                var apps = manager.GetActiveFull().Where(a => a.CarAppId != null).ToList();
                var app = apps[0];
                app.CarAppId = 2;
                app.BuildTypeId = 2;
                manager.Update(app);

Below are a couple of images showing the configuration from the database for the foreign key constraint to the column and the properties for the column.

enter image description here enter image description here

if anyone has any idea why this one column would refuse to update properly it would be a great help.

UPDATE:::

I have created sql scripts that duplicate the database structure and generate test data. Basically you create a database called test..runt eh two scripts with this duplicate database created the issue can be replicated.

    USE [test]
GO

/****** Object:  Table [dbo].[tbl_control_level]    Script Date: 2/21/2017 3:30:29 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tbl_control_level](
    [control_level_id] [int] IDENTITY(1,1) NOT NULL,
    [control_level] [varchar](50) NULL,
    [description] [varchar](1000) NULL,
    [deleted] [bit] NULL,

 CONSTRAINT [PK_tbl_control_level] PRIMARY KEY CLUSTERED 
(
    [control_level_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO



/****** Object:  Table [dbo].[tbl_app_types]    Script Date: 2/21/2017 3:29:51 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tbl_app_types](
    [app_type_id] [int] IDENTITY(1,1) NOT NULL,
    [app_type] [varchar](50) NULL,
    [deleted] [bit] NULL,   
 CONSTRAINT [PK_tbl_app_types] PRIMARY KEY CLUSTERED 
(
    [app_type_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


/****** Object:  Table [dbo].[tbl_car_apps]    Script Date: 2/21/2017 3:31:32 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tbl_car_apps](
    [car_app_id] [int] IDENTITY(1,1) NOT NULL,
    [car_id] [varchar](50) NULL,    
    [deleted] [bit] NULL,
     CONSTRAINT [PK_tbl_car_apps] PRIMARY KEY CLUSTERED 
(
    [car_app_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO



/****** Object:  Table [dbo].[tbl_control_level]    Script Date: 2/21/2017 3:30:29 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tbl_control_level](
    [control_level_id] [int] IDENTITY(1,1) NOT NULL,
    [control_level] [varchar](50) NULL,
    [description] [varchar](1000) NULL,
    [deleted] [bit] NULL,

 CONSTRAINT [PK_tbl_control_level] PRIMARY KEY CLUSTERED 
(
    [control_level_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO




/****** Object:  Table [dbo].[tbl_build_types]    Script Date: 2/21/2017 3:29:15 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tbl_build_types](
    [build_type_id] [int] IDENTITY(1,1) NOT NULL,
    [build_type] [varchar](50) NULL,
    [deleted] [bit] NULL,
 CONSTRAINT [PK_tbl_build_types] PRIMARY KEY CLUSTERED 
(
    [build_type_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO


/****** Object:  Table [dbo].[tbl_apps]    Script Date: 2/21/2017 3:25:58 PM ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[tbl_apps](
    [app_id] [int] IDENTITY(1,1) NOT NULL,
    [app_acro] [varchar](50) NULL,
    [app_name] [varchar](100) NULL, 
    [app_type_id] [int] NULL,   
    [control_level_id] [int] NULL,
    [build_type_id] [int] NULL, 
    [car_app_id] [int] NULL,
    [deleted] [bit] NULL,

 CONSTRAINT [PK_tbl_apps] PRIMARY KEY CLUSTERED 
(
    [app_id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[tbl_apps]  WITH CHECK ADD  CONSTRAINT [FK_tbl_apps_tbl_app_types] FOREIGN KEY([app_type_id])
REFERENCES [dbo].[tbl_app_types] ([app_type_id])
GO

ALTER TABLE [dbo].[tbl_apps] CHECK CONSTRAINT [FK_tbl_apps_tbl_app_types]
GO

ALTER TABLE [dbo].[tbl_apps]  WITH CHECK ADD  CONSTRAINT [FK_tbl_apps_tbl_build_types] FOREIGN KEY([build_type_id])
REFERENCES [dbo].[tbl_build_types] ([build_type_id])
GO

ALTER TABLE [dbo].[tbl_apps] CHECK CONSTRAINT [FK_tbl_apps_tbl_build_types]
GO

ALTER TABLE [dbo].[tbl_apps]  WITH CHECK ADD  CONSTRAINT [FK_tbl_apps_tbl_car_apps] FOREIGN KEY([car_app_id])
REFERENCES [dbo].[tbl_car_apps] ([car_app_id])
GO

ALTER TABLE [dbo].[tbl_apps] CHECK CONSTRAINT [FK_tbl_apps_tbl_car_apps]
GO

ALTER TABLE [dbo].[tbl_apps]  WITH CHECK ADD  CONSTRAINT [FK_tbl_apps_tbl_control_level] FOREIGN KEY([control_level_id])
REFERENCES [dbo].[tbl_control_level] ([control_level_id])
GO

ALTER TABLE [dbo].[tbl_apps] CHECK CONSTRAINT [FK_tbl_apps_tbl_control_level]
GO




     USE [test]
GO

INSERT INTO [dbo].[tbl_app_types]
           ([app_type]
           ,[deleted])
     VALUES
           ('app type 1'
           ,0)
GO

INSERT INTO [dbo].[tbl_app_types]
           ([app_type]
           ,[deleted])
     VALUES
           ('app type 2'
           ,0)
GO


INSERT INTO [dbo].[tbl_build_types]
           ([build_type]
           ,[deleted])
     VALUES
           ('build type 1'
           ,0)
GO

INSERT INTO [dbo].[tbl_build_types]
           ([build_type]
           ,[deleted])
     VALUES
           ('build type 2'
           ,0)
GO

INSERT INTO [dbo].[tbl_control_level]
           ([control_level]
           ,[description]
           ,[deleted])
     VALUES
           ('ct 1'
           ,''
           ,0)
GO

INSERT INTO [dbo].[tbl_control_level]
           ([control_level]
           ,[description]
           ,[deleted])
     VALUES
           ('ct 2'
           ,''
           ,0)
GO

INSERT INTO [dbo].[tbl_car_apps]
           ([car_id]
           ,[deleted])
     VALUES
           ('1'
           ,0)
GO

INSERT INTO [dbo].[tbl_car_apps]
           ([car_id]
           ,[deleted])
     VALUES
           ('2'
           ,0)
GO

INSERT INTO [dbo].[tbl_apps]
           ([app_acro]
           ,[app_name]
           ,[app_type_id]
           ,[control_level_id]
           ,[build_type_id]
           ,[car_app_id]
           ,[deleted])
     VALUES
           ('testapp1'
           ,''
           ,1
           ,1
           ,1
           ,1
           ,0)
GO

INSERT INTO [dbo].[tbl_apps]
           ([app_acro]
           ,[app_name]
           ,[app_type_id]
           ,[control_level_id]
           ,[build_type_id]
           ,[car_app_id]
           ,[deleted])
     VALUES
           ('testapp2'
           ,''
           ,1
           ,1
           ,1
           ,1
           ,0)
GO
1
Are you loading more data after you update your foreign key value?johnny 5
Which way does the relationship go? Wouldn't updating the key in car table break the relationship with the other table?Joshua Drake
I'm not doing any further updating or loading of data. Its a simple get the record modify the properties and update the entity. As for updating the key.. I'm not updating the key in the car table..I'm update the referencing column in the app table to a different viable record in the car table.Bastyon
As mentioned above...I can update any column/property on this entity. Just not the car_app_id value. Why can I update the built type id or the app type id just fine..but when i try to update the car_app_id . It just reverts the value and moves on...what is it about that column that wont take an update. The value is valid..its a valid record so it doesn't violate the constraint...and all the update method in the manager does is instantiate the repository and call the repository to the TblApps object and call the update method.Bastyon

1 Answers

0
votes

Looks like i figured it out.

After some further investigation this is what I discovered...

The entity that was being updated was gotten with an include on a navigation object..so this particular instance..the CarApp object was included in the "Get" on the record. for some reason this was causing the update to reset the changes back to whatever the value in the navigation property was set to.

Out of curiosity I added the other properties as includes on the get and it started the same behavior with them...when I removed all includes of navigation properties and tried to save the update it worked fine.

So basically if you are going to do an update on an entity with an include of the navigation property the constrained column is for..you either have to update that property as well..or else remove the reference from the updated property