Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Sunday, August 17, 2014 4:42 PM
Hi, I hope can help me I work on a new project they used a Entity framework but not has EDMX or TT files, they map tables like:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new MytableMap());
MyTableMap is:
public class MyTableMap : EntityTypeConfiguration<MyTable>
{
public MyTableMap()
{
// Primary Key
this.HasKey(t => t.table_ID);
// Properties
this.Property(t => t.tablepro)
.HasMaxLength(100); ..
And I need add a SP to this project I create something like:
public virtual ObjectResult<SP1Result> SP1(string userid)
{
var typeParameter = userid != null ?
new ObjectParameter("userid", userid) :
new ObjectParameter("userid", typeof(string));
return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction< SP1Result >("SP1", typeParameter);
}
I map the SP1Result to entity:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Types().Configure(t => t.MapToStoredProcedures());
modelBuilder.Configurations.Add(new SP1Result());
But I have this issue:
The FunctionImport 'SP1' could not be found in the container 'MyContext'.
**Description: **An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
**Exception Details: **System.InvalidOperationException: The FunctionImport 'SP1 could not be found in the container 'MyContext'.
Source Error:
Line 251: new ObjectParameter("userid", typeof(string)); Line 252: Line 253: return ((IObjectContextAdapter)this).ObjectContext.ExecuteFunction<SP1Result>("SP1", typeParameter); Line 254: } Line 255: } |
I hope can you help me how add the SP to my context.
Best Regards,
All replies (2)
Tuesday, August 19, 2014 1:37 AM âś…Answered
Hi josh43jj,
Thanks for your post.
For your problem, i suggest that you can try the following steps:
Step1: Create a stored procedure in your sql management studio
Step2: Import Stored Procedure.
Step3: Call Stored Procedure using ExecuteStoreQuery<T> function, code like below:
using (SqlCommand cmd = con.CreateCommand())
{
cmd.CommandText = "GetEmployeeData";
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.CommandTimeout = 0;
sqlReader = (System.Data.Common.DbDataReader)cmd.ExecuteReader();
IEnumerable<EmployeeDetail> empDetails = context.Translate<EmployeeDetail>(sqlReader).ToList();
}
You can refer to the link below for more detailed steps :
http://www.c-sharpcorner.com/UploadFile/ff2f08/call-store-procedure-from-entity-framework/
You can also refer to the link below:
http://www.entityframeworktutorial.net/stored-procedure-in-entity-framework.aspx
Best Regards,
Kevin Shen.
Monday, August 18, 2014 3:23 AM
hi
required to build a ADO.NET class as a separate project. Entity Framework passes the parameters to an object that is bound as a dataset in the class of ADO.NET inherited.