2

Using entity framework I save two different objects to the database in the same way.

One object is inserted (it generates the following sql):

exec sp_executesql N'insert [dbo].[ImportJob]([SourceSystemId], [Start], [Source],   [ImportType], [IsReadCompleted], [IsValidated], [IsImportCompleted])
values (@0, @1, @2, @3, @4, @5, @6)
select [Id]
from [dbo].[ImportJob]
where @@ROWCOUNT > 0 and [Id] = scope_identity()',N'@0 uniqueidentifier,@1  datetime2(7),@2 nvarchar(1000),@3 int,@4 bit,@5 bit,@6 bit',@0='1A908EAE-9438-49A3-9784-C9D84F99D217',@1='2013-08-29 14:46:58.3071350',@2=N'uploads\825d3e08-f795-45dc-aa8f-16e02dec3b29.xlsx',@3=1,@4=0,@5=0,@6=0

The other object is not inserted and it doesn't return an error. The only SQL EF generates for this framework is this:

exec sp_executesql N'declare @generated_keys table([Id] uniqueidentifier)
insert [dbo].[ImportOrganization]([ImportJobId], [Import_ExistingId], [Import_SourceSystemPk], [Import_IsNew], [Import_IsUpdated], [Import_IsRemoved], [SourcePk], [OrganizationTypeText], [Name], [OrganizationTypeId], [MissionStatement], [Address_Street], [Address_Number], [Address_Postcode], [Address_City], [PhoneNumber], [Website], [EmailAddress], [BankAccount], [BankAccountHolder])
output inserted.[Id] into @generated_keys
values (@0, null, @1, @2, @3, @4, @5, @6, @7, @8, @9, @10, @11, @12, @13, @14, @15, @16, @17, @18)
select t.[Id], t.[TimeStamp]
from @generated_keys as g join [dbo].[ImportOrganization] as t on g.[Id] = t.[Id]
where @@ROWCOUNT > 0',N'@0 int,@1 nvarchar(max) ,@2 bit,@3 bit,@4 bit,@5 nvarchar(max) ,@6 nvarchar(100),@7 nvarchar(100),@8 int,@9 nvarchar(1000),@10 nvarchar(100),@11 nvarchar(20),@12 nvarchar(10),@13 nvarchar(100),@14 nvarchar(20),@15 nvarchar(200),@16 nvarchar(200),@17 nvarchar(50),@18 nvarchar(100)',@0=20,@1=N'1',@2=1,@3=0,@4=0,@5=N'1',@6=N'Bla Bla',@7=N'Test bla',@8=1,@9=N'bla bla bla mission',@10=N'Street',@11=N'1',@12=N'30010',@13=N'Blaa',@14=N'0140 123 45 678',@15=N'www.test.org',@16=N'[email protected]',@17=N'123456789',@18=N'Bla Bla'
go

update: the EF code

This is from GenericRepository.cs (based on this tutorial):

public virtual void Insert(TEntity entity) {
    dbSet.Add(entity);
}

and the corresponding UnitOfWork.cs:

public void Save() {
    context.SaveChanges();
}

the object that fails:

ImportOrganization item = new ImportOrganization();

item.Name = excelReader.GetString(1);
item.OrganizationTypeText = excelReader.GetString(2);
item.MissionStatement = excelReader.GetString(3);
item.Address = new DAL.Entities.Address
            {
                Street = excelReader.GetString(4),
                Number = excelReader.GetString(5),
                Postcode = excelReader.GetString(6),
                City = excelReader.GetString(7)
            };
item.PhoneNumber = excelReader.GetString(8);
item.Website = excelReader.GetString(9);
item.EmailAddress = excelReader.GetString(10);
item.BankAccount = excelReader.GetString(11);
item.BankAccountHolder = excelReader.GetString(12);
item.Import = new ImportEntity
{
       SourceSystemPk = excelReader.GetString(0)
};
item.ImportJob = _importJob;
item.ImportJobId = _importJob.Id;
item.SourcePk = excelReader.GetString(0);

Calling the insert and save method:

unitOfWork.ImportOrganizationRepository.Insert(item);
unitOfWork.Save();

Why would EF not create an import query for a new object?

6
  • Can you show your code as well as the import statements? Commented Aug 29, 2013 at 13:02
  • So no exceptions ? Have you tested this with linq pad to isolate whether you are a bug in your code or the sql ? Commented Aug 29, 2013 at 13:03
  • it doesn't save in linqpad. No errors. And no item in the database (yes I'm looking in the right database) Commented Aug 29, 2013 at 13:23
  • 1
    is dbSet part of your context? I would expect to see context somewhere there. It seems like item isn't getting attached to the context, and therefore the context doesn't think anything needs to be saved. Debug this, and right before unitOfWork.Save(), take a look at context.Entry(item).State to see what it is. It should be EntityState.Added. Commented Aug 29, 2013 at 13:24
  • 1
    It's okay to make stupid mistakes... we've all done it, and we wouldn't be programmers if we didn't. For the sake of clarity, though, could you please either answer and accept your question, or make it clear in the subject line that it's solved? Thanks. Commented Aug 29, 2013 at 22:08

1 Answer 1

1

The root cause of this problem was PEBKAC. Your humble programmer looked in the wrong database table.

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

1 Comment

+1 You haven't solved my issue, but at least made me smile :)

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.