Since this question created a lot of confusion let me put in this way :
I am working on an MVC C# project with Entity Framework. I am using a web service to implement a billing feature. Here I am passing values like this:
Billing.cost bills = new Billing.cost();
bills.Charge(0,"model",8,0,"Date",0,1,1,1,0,0,"N","APP","myemail",1,"cost");
Here in place of a model I need to pass my stored procedure name which has required values. My complete code with Stored procedure :
using (var ctx = new market_Entities())
{
ctx.uspmodel(10);
ctx.SaveChanges();
Billing.cost bills = new Billing.cost();
bills.Charge(0,"ctx",8,0,"Date",0,1,1,1,0,0,"N","APP","myemail",1,"cost");
}
Here 10 is an ID which i am passing as an input parameter. In charge method, 2nd parameter that is ctx is of string type and it is reference to my SP.
Problem I have is :
1) My stored procedure doesn't have output parameter.It is taking id as input parameter and based on that it is selecting name, value and address. So in place of ctx, I need to pass address result which I am getting from my stored procedure. But how can I select only particular property from market_entities and pass it as parameter ?
2) when I put breakpoints and check for results, I am not able to see any results.So how can i check results for a stored procedure in MVC which doesn't have output parameters?
Related
I have created a dynamic stored procedure (using Pivot). I have no idea about the columns returned by the stored procedure. So I am not able to create a viewmodel class.
I searched and found this method to execute stored procedure:
public List<dynamic> GetData(int CurriculumID)
{
var data = context.Database.SqlQuery<dynamic>("MyProcedureName #CurriculumId", new SqlParameter("CurriculumId", CurriculumID)).ToList();
return data;
}
Now I am not sure how to send this data to razor view and create dynamic table based on the data. Any guidance?
I am also not able to view data here is how I am getting it:
I have a ASP.NET MVC5 project with EF6. In SQL server 2008 I have a stored procedure that takes 2 parameters, an ID and a language ID, and returns data based on the parameters.
I want to call this stored procedure in one of my controllers to view the data. But I don't know how to do this. Can anybody help me or provide a tutorial for it?
I have the stored procedure in the project as an ADO.NET entity object already, it has a model class and such thus.
You can use Entity Framework's ExecuteSqlCommand to achieve this:
SqlParameter param1 = new SqlParameter("#param1", "Item1");
SqlParameter param2 = new SqlParameter("#param2", "Item2");
context.Database.ExecuteSqlCommand(
"sp_StoredProcedureName #param1, #param2",
param1,
param2);
Getting an error client side with breeze: "Cannot call method 'map' of undefined" when trying to pull over some data. The difference between this action and one that works is that this action is calling a stored procedure and returning ObjectResult<T> instead of DbSet<T>.
Might this be why I get an error? Using Chrome Developer tools, I do see that the breeze controller is returning json data.
I have created a complex model type in the edmx for mapping the rows returned from the stored procedure.
The action in the breeze controller has a return type of IEnumerable<T>.
I experienced the same error when using an EF complex type. A workaround was to create a view in my database instead of using a complex type, set the stored procedure to return a type of the new view which had a primary key and then it worked. It would seem that breeze requires entities to have a primary key defined.
Hm... not quite sure what is happening, so just guessing here, but try adding an AsQueryable() to the result returned, and changing the result type to a IQueryable.
We don't have any stored proc tests for breeze yet, but this is impetus for me to add some :)
I had the very same issue, but thank God I figured out a solution. Instead of using a stored procedure, you should use a view, as Breeze recognizes views as DbSet<T>, just like tables. Say you have a SQL server table that contains two tables Customers and Orders.
Customers (**CustomerId**, FirstName, LastName)
Orders (OrderId, #CustomerId, OrderDate, OrderTotal)
Now, say you want a query that returns orders by CustomerId. Usually, you would do that in a stored procedure, but as I said, you need to use a view instead. So the query will look like this in the view.
Select o.OrderId, c.CustomerId, o.OrderDate, o.OrderTotal
from dbo.Orders o inner join dbo.Customers c on c.CustomerId = o.CustomerId
Notice there is no filtering (where ...). So:
i. Create a [general] view that includes the filtering key(s) and name it, say, OrdersByCustomers
ii. Add the OrdersByCustomers view to the entity model in your VS project
iii. Add the entity to the Breeze controller, as such:
public IQueryable<OrdersByCustomers> OrdersByCustomerId(int id)
{
return _contextProvider.Context.OrdersByCustomers
.Where(r => r.CustomerId == id);
}
Notice the .Where(r => r.CustomerId == id) filter. We could do it in the data service file, but because we want the user to see only his personal data, we need to filter from the server so it only returns his data.
iv. Now, that the entity is set in the controller, you may invoke it in the data service file, as such:
var getOrdersByCustomerId = function(orderObservable, id)
{
var query = breeze.EntityQuery.from('OrdersByCustomerId')
.WithParameters({ CustomerId: id });
return manager.executeQuery(query)
.then(function(data) {
if (orderObservable) orderObservable(data.results);
}
.fail(function(e) {
logError('Retrieve Data Failed');
}
}
v. You probably know what to do next from here.
Hope it helps.
New to Entity Framework .Using EF4. I have found articles and managed to use stored procedures to return a list of entities.
But cannot see/work out how you return a single entity.
Given that I have a stored procedure "GetCustomerById" that return a single customer How do I map it?
Using the Model Browser I right click on "Function Import" and I have added my StoredProcedure
however whatever I select does not seem to return a "Single Entity"
Am I missing the obvious?
thanks a lot for any link or suggestions
When you do Function Import you need to select the entity your SP returns from the drop down (i.e. Customer). The catch is EF does NOT directly returns Customer object as per your selection but System.Data.Objects.ObjectResult which implements IEnumerable.
To be more specific, here is the generated code for your function:
public ObjectResult<Customer> GetCustomerById(Nullable<global::System.Int32> Id)
That's because EF has no idea if your SP returns a single record or a list of them so it wraps the result inside ObjectResult. You can enumerate through this to get your Customer entity like any other IEnumerable object. For example:
Customer myCustomer = context.GetCustomerById(1).First();
In MVC RC2 I am returning an anonymous object type & I want to access it in a strongly typed view.
Let's say that in the controller I query the database & fetch values in var type & I want to pass it to a strongly typed view. How can i access it in that view?
Well, you can't. An anonymous type, cannot be accessed by name. That's the whole point. You can't pass the type around, the type exist internally and you can only expose the type as System.Object.
You can always use reflection to dig up the properties and access them that way, but other than that, there's not way around it.
var q = new { MyProperty = "Hello World" };
var t = q.GetType();
var hello = t.GetProperty("MyProperty").GetValue(q, null) as string;
Console.WriteLine(hello);
If you need to access the type, you should create an user-defined object/type, which can be identified by name.
You can't pass it to a strongly typed view, but you could turn it into a dictionary and access the properties that way.
As part of System.Web.Routing, there is a new object called "RouteValueDictionary", which can take as it's constructor an anonymous object.
The MVC team uses this in many of their helpers.
Example:
IDictionary<string, object> myDict = new RouteValueDictionary(anonymousObject);