Apologies if this isnt phrased very well, but after upgrading to VS2012/.NET 4.5, I know Table Valued Functions are possible in Entity Framework 5.
We use a custom datalayer / orm, and I cant find any code examples that dont use the EDMX model generator as this is of no use.
As a very wild guess I would say some code that defines the table value function will need adding in OnModelCreating(DbModelBuilder modelBuilder) .
Any help appreciated.
Table valued function are not supported for Code-First strategy, only for Database-First with EDMX: http://msdn.microsoft.com/en-us/hh859577. Quote:
TVFs are currently only supported in the Database First workflow.
I was able to easily execute a Table Valued Function using EF 5 as follows:
int orderID = 100;
var query = context.Database.SqlQuery<Product>("Select * from [dbo].[tfn_GetOrderProducts](#p0)", orderID);
var results = query.ToList();
where Product can be any POCO class whose member names match up with the results of the table valued function.
This isn't a perfect solution -- it's not returning an IQueryable, so you can't use this as part of a larger LINQ query; however, in this case, it was all I needed.
the following suggestion was removed from this link http://blogs.msdn.com/b/adonet/archive/2011/06/30/walkthrough-table-valued-functions-june-ctp.aspx. I've found this information that may be usefull to you.
Code First Approach
Entity Framework June 2011 CTP does not include Code First support for
TVFs. However, you can use DbContext against your TVFs. You can do
this by adding the DbContext template to your model. The steps to add
the template are the following:
Open NorthwindModel.edmx and right click on the canvas
Click on Add Code Generation Item…
Select ADO.NET DbContext Generator V4.2, enter a name for your template, and click Add
Related
I'm creating a web app based on a database. The datas in the database need to be displayed, edited and deleted by the web app user.
Right now I need to remove elements in my sqlite database table after the user inputs the name of the database table and the id (which is also the primary key) of the element. How can I do it?
I always used Entity Framework before and also in the Web App so I was looking for a solution with it, but if there's a simpler way to do it, I'll stick with it.
Thank you
I think the answer here is similar but I need help to adapt it to what I need now.
Entity Framework C# queries from strings
this is the UI
and here is the endpoint in the backend
//DELETE method
[HttpDelete("DeleteElementInTable")]
public IActionResult DeleteElementInTable(string tableName, string elementKey) //url query parameters
{
var db = new MyContext();
//code to remove the item ... something like:
DbManager.RemoveElement(tableName, elementKey); //DbManager is the static class dealing with the db context
return //csv of the deleted element;
}
I'm still a young developer but here I can see there are some lacks of knowledge. First of all, which technology are you using to build your web-app? From what you posted my guess is you are trying to use MVC. As #Panagiotis Kanavos said above you need an entity to interact with the database if you want to use Entity Framework, through which you don't need to pass table name in your GET function. Last but not less important you can't execute the delete operation in a GET function.
We have a Data library they have built it with CodeFirst.
Now I ran a SQL command and added a new table to that database.
But Now I want to also see its generated object class, its DBContext definitions ,etc.. in the code first code so I can use them in my LINQ queries.
So what I did was following number 3 method from this MSDN page:
https://msdn.microsoft.com/en-us/library/jj200620.aspx
But it didn't do it right. For example it did not add any definition for this new table to DBContext.cs file, for example all my other tables that used to be there are defined like this:
DbSet<Zipcode> Zipcodes { get; set; }
But it has not added anything for me.
What is the correct way to do this?
I'm unaware of a way to simply add a new table and have it 'plug-n-play' with your existing model without manual work. The two options I know of are:
Rebuild the model using Code First from DB and include your added table
Manually create the table as a class and add the DbSet and entity in the OnModelCreating method in your model
Code First from Database only works when you already have a database. If you want to add a new table, you will have to start using Code-First(alone), that means: add the entity Zipcode to the model, DbSet to the DbContext and after that when you compile it will generate de table in the database.
I am using EF reverse POCO generator. When I save the .tt file it automatically generates the return model of all the stored procedures.
In couple of my SP's, I have a user defined table type as one of the input params. For those SP's, my POCO is not creating the return models.
Earlier I had this issue for normal SP for which I used a return query from the temp tables. Adding SET FMTONLY OFF on top of the SP fixed that.
I am pretty sure it is with using the user defined table types that is causing the issue because when I just remove them, the return models are generated.
I am looking for a fix to this. Any help would be greatly appreciated.
I have a challenge. We have a database that was originally designed to be used with VB6 and most of the functionality sits in stored procedures. I cannot make changes to the stored procedures as the old application will still need to work for a while and I do not have my own copy of the database that I can modify even briefly.
So is it possible to execute a stored procedure from EF and have it do it's best to write the results into an array/collection of POCOs?
I've tried the database first approach and import but EF says the stored procedure does not return any columns and so cannot create a complex type. I've found there are ways to change the stored procedure to allow this to work but I cannot alter the database I'm using.
Another challenge is that the names of the columns in the results are things like 'Date last changed' in other words with spaces. How will EF try to map these? Would it become DataLastChanged or possibly Data_last_changed? Is there a way to mark my POCO with attributes to say how they are mapped?
What I was hoping for is something like
var resuls = efContext.ExecuteStoredProcedure<MyPOCOType>("spName",param1, param2, ...);
And have EF do it's best to match the results to the type. Does such a thing exist? Incidentally we are using EF4 but I believe 5 is available to us.
I think I've cracked part of the problem for myself. The following snippet does what I need.
using (DbContext context = new DbContext("DBConnectionStringNameFromAppConfig"))
{
SqlParameter[] parameters =
{
new SqlParameter("#OwnerID", DBNull.Value),
new SqlParameter("#ExternalColorID", colorOwner.ExternalColorID),
new SqlParameter("#ProductionSiteID", DBNull.Value),
new SqlParameter("#PanelstatusNr", DBNull.Value),
new SqlParameter("#DateLastChecked", DBNull.Value),
new SqlParameter("#rowcount", DBNull.Value),
};
var colors = context.Database.SqlQuery<Models.ColorSelectEvaluation>("[dbo].[sp_Color_Select_Evaluation] #OwnerID, #ExternalColorID, #ProductionSiteID, #PanelstatusNr, #DateLastChecked, #rowcount", parameters).ToList();
}
The confusing this is still the naming of the columns. They mostly seem to work but EF is not mapping the resulting column 'Needs evaluation' to the property NeedsEvaluation on my object.
Regarding the column names not matching. Another Q&A on stackoverflow deals with this nicely.
Why is my DbModelBuilder configuration ignored when mapping Entity from DbSet<T>.SqlQuery?
To summarise, MS think it would be great but they do not support mapping of names in this way. The only solution is to change the stored procedure and that is no option for me as it would break the legacy applications still using it.
Using Entity Framework one often writes queries such as
var orders = from o in context.Orders.Include("Customer")
where o.OrderDate.HasValue && o.OrderDate.Value.Year == 1997
orderby o.Freight
select o;
What really makes my stomach churn is the "Customer" string argument. I have a hard time believing that EF does not generate table names as constants somewhere. Does anyone know a better approach than to using a string? for the Include fetch option?
EF 4.1 has strongly typed version of Include usable for IQueryable, ObjectQuery and DbQuery. Once you add reference to EntityFramework.dll (EF 4.1) you can add using System.Data.Entity and use eager loading with lambda expressions
// get Orders with related Customers
var orders = from o in context.Orders.Include(o => o.Customer) ...
Edit:
If you don't want to use EF 4.1 check this article. I already used in my project and I'm happy with it.
IMO GetType might help you other than .edmx file where all the definitions is stored,
context.Orders.Include(CustomerEntity.GetType.Name or full name )
How about
Include(context.Customers.EntitySet.Name)
?
You can create a Text Template which will allow you to generate the code in addition to EF's default code. You can do this by right click and clicking "Add Code Generation Item".
In this text template, you can create your constants as you need in "CustomerProperties" and create constant name for each navigation property.
http://msdn.microsoft.com/en-us/data/gg558520