EF5 needs to update ContainerName.FunctionImportName to access the stored procedure when updating models, any charming solution?

I am new to entity infrastructure, please forgive me if my question is too simple.

I am using EF5 to build my project at the moment, there is a function "GetStockItem" in my project that calls the stored procedure and returns data from the SP. Each time I โ€œupdate the model from the databaseโ€ from the model diagram, the update wizard reflects the database changes without problems, but GetStockItem stops working. Error message when calling GetStockItem:

"The value of EntityCommand.CommandText is not valid for the StoredProcedure command. The value of EntityCommand.CommandText must be of the form" ContainerName.FunctionImportName ".

The solution, as indicated in the error message, is clear, all I need to do is add a ContainerName. before the name FunctionImportName (GetStockItem in my case) in the context.cs file.

My question is, how can I avoid every time I update the models from the database? It is quite annoying to do this manual thing from time to time, and it is easy to forget to do it, and then cause user complaints.

Hope someone can enlighten me with a charming solution! Hooray!

+4
source share
2 answers

I just ran into this using EF5 / DbContext. The solution I found is to edit the T4 template ([Model] .Context.tt) that the DbContext generates.

In this file, find the instructions for generating an ExecuteFunction call. For me, it started on line 288:

public string ExecuteFunction(EdmFunction edmFunction, string modelNamespace, bool includeMergeOption) { var parameters = _typeMapper.GetParameters(edmFunction); var returnType = _typeMapper.GetReturnType(edmFunction); var callParams = _code.StringBefore(", ", String.Join(", ", parameters.Select(p => p.ExecuteParameterName).ToArray())); if (includeMergeOption) { callParams = ", mergeOption" + callParams; } return string.Format( CultureInfo.InvariantCulture, "return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction{0}(\"{1}\"{2});", returnType == null ? "" : "<" + _typeMapper.GetTypeName(returnType, modelNamespace) + ">", edmFunction.Name, callParams); } 

Change the return line so that edmFunction.Name is replaced with edmFunction.FullName , and when you save, the Import function code will be regenerated using fully qualified names.

+8
source

I had a similar problem, I suggest not changing the context.cs file at all; just make sure that the connection strings in the app.config file generated by EF are the same in the calling project, especially the metadata that is very important in the connection string to be correct. If this helps, mark this answer, otherwise accepted, send me the steps to reproduce this error.

+2
source

Source: https://habr.com/ru/post/1486755/


All Articles