簡體   English   中英

實體框架不會將對象保存到數據庫

[英]Entity Framework does not save object to the database

使用實體框架,我以相同的方式將兩個不同的對象保存到數據庫中。

插入一個對象(它生成以下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

另一個對象未​​插入,並且不返回錯誤。 為此框架生成的唯一SQL EF是:

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'info@test.org',@17=N'123456789',@18=N'Bla Bla'
go

更新:EF代碼

這是來自GenericRepository.cs(基於本教程 ):

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

以及相應的UnitOfWork.cs:

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

失敗的對象:

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);

調用insert和save方法:

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

EF為什么不為新對象創建導入查詢?

出現此問題的根本原因是PEBKAC 您的謙虛程序員看錯了數據庫表。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM