Can you please tell me why Entity Framework 6 needs the "IsModified" line of code for an update with attach, else my code will "Silent Fail" vs Entity Framework 4? In other words, in Entity Framework 4, I do an update with attach it works. But in EF6, if I do similar, the db will not update and no exception thrown (silent fail). If I put the "IsModified" line in my code it works, but this is unacceptable, as developers could leave the "IsModified" code out and updates will fail, and nobody will know.
This problem will occur/not occur in EF6 under the following conditions set in the db: 1. If active is set to default 1 and AllowNulls=false, update fails 2. If active is not set to a default and AllowNulls=false, update fails 3. If active is not set to a default and AllowNulls=true, update works 4. If active is set to default 1 and AllowNulls=true, update works
This is similar to: EntityFramework not saving null and false value but not exact. I will walk you through the problem:
1) If you have Visual Studio 2010, you can follow along, else, you can trust me that EF4 works as described.
In Visual Studio 2010, new project ASP.NET MVC 2 Web Application, using .NET Framework 4, and name it something like EF4Works. Do not create a unit test.
2) Be sure to add the default and not allow nulls, it is important
CREATE TABLE [dbo].[Document](
[Id] [int] IDENTITY(1,1) NOT NULL,
[DocumentName] [varchar](10) NULL,
[Active] [bit] NOT NULL CONSTRAINT [DF_Document_Active] DEFAULT ((1)),
CONSTRAINT [PK_Document] 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]
3) Insert row "DocName" 1(true)
4) Add edmx with this table.
5) In Home Index Controller (add this controller if necessary) add and run this code (similar) and check db that IT WORKS!(updates db) (put a breakpoint before the view is called):
public ActionResult Index()
{
BreazEntities entities = new BreazEntities();
Document document = new Document { Id = 1 };
entities.Documents.Attach(document);
document.Active = false;
entities.SaveChanges();
return View();
}
6) Put back active flag to 1
7) Add new solution and project in Visual Studio 2013. Web, .NET Framework 4.5.1 ASP.NET WebApplication called EF6Fails, next wizard page MVC, change authentication to no authentication. In package manager console: uninstall-package EntityFramework -project ef6fails -force then install-package EntityFramework -version 6.1.3 -project ef6fails
8) Add edmx in ef6fails to the Document table.
9) Run this code in the controller and put a breakpoint before the view is called:
public ActionResult Index()
{
BreazEntities6 entities = new BreazEntities6();
Document document = new Document { Id = 1 };
entities.Documents.Attach(document);
document.Active = false;
entities.SaveChanges();
return View();
}
This is NOT Working. I will have to do the following, which is unacceptable because new developers could exclude the following and not know that code is not working. Is there something, I can add globally to the solution to not make developers add the following? I will research and try to add something myself until I get answers from SO:
BreazEntities6 entities = new BreazEntities6(); Document document = new Document { Id = 1 }; entities.Documents.Attach(document); /* The following line needs to be added in EF6, BUT not in EF4 */ entities.Entry(document).Property(e => e.Active).IsModified = true; document.Active = false; entities.SaveChanges(); return View();