Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Using EF with a view
Message
From
17/12/2015 16:57:26
 
General information
Forum:
ASP.NET
Category:
Entity Framework
Environment versions
Environment:
C# 4.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01629081
Message ID:
01629082
Views:
101
This message has been marked as the solution to the initial question of the thread.
The view needs a primary key

>Hi everybody,
>
>I'm trying to figure out why this code does nothing - the changes are not saved
>
>
>public void UpdateTemplatesList(List<TemplatesList> listToChange)
>        {         
>            DbContext db = (DbContext)_siriusContext;
>            db.Configuration.AutoDetectChangesEnabled = false;
>
>            foreach (var row in listToChange)
>            {
>                EntityState entityState = _siriusContext.GetEntityState(row);
>                if (entityState != EntityState.Detached)
>                {
>                    if (row.TemplateId == 0)
>                        _siriusContext.SetAdd(row);
>                    else
>                        _siriusContext.SetModified(row);
>                }
>                else
>                {
>                    if (row.TemplateId == 0)
>                        _dbSetList.Add(row); // Todo - Add INSTEAD OF Insert trigger
>                    else
>                        _dbSetList.Attach(row);
>                        _siriusContext.SetModified(row);
>                }
>            }
>
>            db.Configuration.AutoDetectChangesEnabled = true;
>
>            SaveChanges();
>        }
>    }
>
>TemplatesList is a view defined like this:
>
>
>CREATE view [dbo].[SiriusV_TemplatesList] WITH SCHEMABINDING
>as
>select t.TemplateID,
>       t.Descrip,
>       t.Hidden,
>       t.TmplType,
>       t.Prescedenc,
>       t.Dw
>  from dbo.template t
>;
>
>
>GO
>
>create trigger trChangeTemplatesList ON dbo.SiriusV_TemplatesList INSTEAD OF UPDATE
>AS
>  BEGIN
>  SET NOCOUNT ON;
>  update dbo.template set prescedenc = I.prescedenc
>  from inserted I 
>  END
>
>  GO
>
>I've tested that I can update the view and the underlying table is updated correctly in SSMS. So, something is wrong in that code but I don't see what exactly is wrong.
>
>Do you have experience with the similar problem?
>
>Thanks in advance.
Craig Berntson
MCSD, Microsoft .Net MVP, Grape City Community Influencer
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform