Page 1 of 1

Table.Attach(entity, true) does not seem to be working - version 4.4.529

Posted: Wed 09 Jul 2014 10:57
by tomwebreality
Hi.

First time user, I've been enjoying using LinqConnect, but one of the key features for me, was being able to pass around DTOs, then convert these back to entities and save the changes to the database (SQL). This is not working, even with the asModified flag set.

I have the code:

Code: Select all

 private PatientRecord UpdateEntity(PatientRecord record, int userId = -1)
        {
            record.FullName = "WHAT THE...?";
            var entity = record.ToEntity();

            DataContext.PatientRecords.Attach(entity, true);
            DataContext.SubmitChanges();

            return entity.ToDtoWithRelated(1);
        }
So, I pass in a DTO representation of entity (using the ToDto()/ToEntity() methods from the Convertor templates provided) but no matter what I do to the DTO, the changes are never committed to the database.
I've tried loading the original entity from the database, and passing this in to the overload (entity, original) but still no luck.
All I can think of doing now, is to use Automapper to set the properties of the DTO on the "original", attached entity and submit changes, but this makes life awkward and is quite an overhead.
I notice from these forums that bugs such as these have happened before. Please can you update me on the issue as soon as possible? The reason that I purchased a license for LinqConnect was the speed of development (and performance), but this is really going to hold me up if not resolved pretty soon.

Thanks in advance,

Tom

Re: Table.Attach(entity, true) does not seem to be working - version 4.4.529

Posted: Thu 10 Jul 2014 06:03
by MariiaI
Please specify the following details:
- the DDL/DML scripts for the necessary database tables;
- the definitions of the entity classes;
- the generated SQL queries ( http://www.devart.com/linqconnect/docs/?Logging.html );
- whether you are getting any errors, specify the stack traces, etc.

If possible, please send us a small test project, so that we are able to investigate the issue in more details and find a solution for you.

Looking forward to your reply.

Re: Table.Attach(entity, true) does not seem to be working - version 4.4.529

Posted: Thu 10 Jul 2014 07:21
by tomwebreality
Hi Mariial,

Thanks for your prompt reply. I've just created a demo project to try to recreate the scenario (as my app uses DI and the SimpleInjector "Per Web Request" lifestyle: https://simpleinjector.codeplex.com/wik ... WebRequest).

Having created the project without DI, LinqConnect works as expected, whether using the same Context or a new one, passing in, converting and saving the DTO. The code is here for completeness, but it would appear that the issue that I am experiencing is more down to my particular architecture than LinqConnect itself.

When I find out what the issue is, I'll post here!

So, this all works. Good work :-)

TABLE

Code: Select all

CREATE TABLE [dbo].[Sample](
	[Id] [int] IDENTITY(1,1) NOT NULL,
	[DateStamp] [datetime] NOT NULL,
	[Name] [nvarchar](50) NOT NULL,
 CONSTRAINT [PK_Sample] PRIMARY KEY CLUSTERED 
(
	[Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
CONSOLE

Code: Select all

class Program
    {
        static void Main(string[] args)
        {
            InsertChangeAttachSave();
        }

        private static void InsertChangeAttachSave()
        {
            DeleteAll();

            DoWithNewDataContext();
            DoWithSameDataContext();

            LoadAllAndWriteToConsole();
        }

        private static void DeleteAll()
        {
            var dataContext = new DevartV1DataContext();
            dataContext.Samples.DeleteAllOnSubmit(dataContext.Samples);
            dataContext.SubmitChanges();
        }


        private static void DoWithNewDataContext()
        {
            var dataContext = new DevartV1DataContext();

            // insert a row if it doesn't exist
            var row = dataContext.Samples.FirstOrDefault(s => s.Name == "Tom");
            if (row == null)
            {
                row = new Sample()
                      {
                          DateStamp = DateTime.Now,
                          Name = "Tom"
                      };
                dataContext.Samples.InsertOnSubmit(row);
                dataContext.SubmitChanges();
            }

            // reload the row for fun- works with or without!
            //row = dataContext.Samples.FirstOrDefault(s => s.Name == "Tom");
            Console.WriteLine(row.Id);
            Console.WriteLine(row.Name);
            Console.WriteLine(row.DateStamp.ToString());
            Console.WriteLine("--------");
            Console.WriteLine();

            // convert to a dto
            var dto = row.ToDto();

            dto.Name = "Tony";

            SaveViaNewContext(dto);
        }

        private static void DoWithSameDataContext()
        {
            var dataContext = new DevartV1DataContext();

            // insert a row if it doesn't exist
            var row = dataContext.Samples.FirstOrDefault(s => s.Name == "John");
            if (row == null)
            {
                row = new Sample()
                {
                    DateStamp = DateTime.Now,
                    Name = "John"
                };
                dataContext.Samples.InsertOnSubmit(row);
                dataContext.SubmitChanges();
            }

            // reload the row for fun - works with or without!
            //row = dataContext.Samples.FirstOrDefault(s => s.Name == "John");
            Console.WriteLine(row.Id);
            Console.WriteLine(row.Name);
            Console.WriteLine(row.DateStamp.ToString());
            Console.WriteLine("--------");
            Console.WriteLine();

            // convert to a dto
            var dto = row.ToDto();
            dto.Name = "James";
            var entity = dto.ToEntity();
            dataContext.Samples.Attach(entity, true);
            dataContext.SubmitChanges();
        }


        private static void LoadAllAndWriteToConsole()
        {
            var dataContext = new DevartV1DataContext();
            var rows = dataContext.Samples;

            foreach (var sample in rows)
            {
                Console.WriteLine(sample.Id);
                Console.WriteLine(sample.Name);
                Console.WriteLine(sample.DateStamp.ToString());
                Console.WriteLine();
            }

            Console.ReadLine();

        }

        private static void SaveViaNewContext(SampleDto dto)
        {
            var dataContext = new DevartV1DataContext();

            var entity = dto.ToEntity();
            dataContext.Samples.Attach(entity, true);

            dataContext.SubmitChanges();
        }
    }
OUTPUT

Code: Select all

14
Tom
10/07/2014 08:16:36
--------

15
John
10/07/2014 08:16:36
--------

14
Tony
10/07/2014 08:16:36

15
James
10/07/2014 08:16:36