How To Insert/update An Updatable View Using Ef Code-first
Solution 1:
Ok. After digging around a lot more into the EF documentation, I ran across this: https://msdn.microsoft.com/en-us/library/dn469464(v=vs.113).aspx. Furthermore, I found the following BLOG post regarding implementing the IDbCommandInterceptor where it makes reference to being able to modify the SQL generated by EF before it gets executed: https://www.skylinetechnologies.com/Blog/Skyline-Blog/December-2013/Entity-Framework-6-Intercepting-SQL-produced.
So, my solution was to implement my own IDbCommandInterceptor and override the ReaderExecuiting method as follows:
publicvoidReaderExecuting(DbCommand command, DbCommandInterceptionContext<DbDataReader> interceptionContext)
{
string sqlToIntercept = "INSERT [dbo].[vwView]";
if (command.CommandText.Contains(sqlToIntercept))
{
command.CommandText = command.CommandText.Replace("WHERE @@ROWCOUNT > 0 AND [ent_pk] = scope_identity()", "WHERE @@ROWCOUNT > 0 AND [ent_id] = @1");
}
}
Now, while not perfectly suited to every possible variation of this problem, in my case it does work quite nicely as the ent_id field in the Parent table must be unique thanks to business rules.
Hopefully, even if this scenario isn't exactly the same as a similar one you may encounter, this solution may provide additional direction allowing you to solve your specific scenario.
Thanks to everyone who took the time to read this and comment. All the best.
Post a Comment for "How To Insert/update An Updatable View Using Ef Code-first"