to-SQL DataContext and I run:
datacontext.CreateDatabase()
This works fine.
Recently I dragged a stored procedure onto the methods pane. I was thinking this stored procedure was now part of the datacontext and would get regenerated when creating the database.
It doesn't seem to. Does anyone know why or how to make it happen?
The DataContext.CreateDatabase method creates a replica of the database only to the extent of the information encoded in the object model. Mapping files and attributes from your object model might not encode everything about the structure of an existing database. Mapping information does not represent the contents of user-defined functions, stored procedures, triggers, or check constraints. This behavior is sufficient for a variety of databases.
SPs not part of that http://msdn.microsoft.com/en-us/library/bb399420.aspx
To my knowledge stored procedures must be declared in the Sql Server Management Studio (or such tool) and can'e be done via LINQ.
Related
I'm developing a MVC5 web project in VS 2013 and I have to use an already existing database and its Stored Procedures so I'm looking forward to using Entity Framework database first approach to help me model the classes.
My question is, should I create the classes (the model) directly from the tables using EF? i mean should my classes represent a table in the database exactly the way they are? - given that some stored procedures return a combination of different attributes from different tables, I'm confused as what the classes on the code should represent exactly.
Also i want to have my own form to let users upload and read their info, so scaffolding the views to create the read/update/delete won't come handy for this task, will it?
Thanks!
If it is code first then you can use the EF tools to scaffold your database for you from your existing database. If it's database first, all of the database models are generate for you anyway and whenever you update your database the models can be updated to reflect the changes for you.
If you are using stored procedures for code first, you'll need to create objects for each stored procedure so that the return values can be mapped back to an object. These should really match precisely the data that is being returned back in both type and naming:
this.Database.SqlQuery<YourEntityType>("storedProcedureName",params);
As for having your views scaffolded for you, I think you should take one step at a time and see what works for your use case or not.
We have a legacy application which we are moving to C#, and I would like to use Entity Framework (EF) for data access. SQL Server 2012 is the database, and currently I am using EF 5.0. The original application did a lot of data intensive processing. As part of that processing, the original application made extensive use of “temp” tables. Most of these “temp” tables were not actually SQL Server temp tables (e.g. #sometable in tempdb), but real tables created and destroyed in their own “temp” database in SQL Server.
Now, that I am working with C# EF I want to be able to use temp tables (of either type) as part of my data processing. I have done some googling on using temp tables with EF - and so far I have found that you can create the tempdb temp tables using the SQLBulkCopy, but there is no way to query against them using EF Linq, as they are not part of the normal data structure. The other option I have read about - is using a stored procedure to do the processing and passing it a table valued parameter. This would force us to have thousands of stored procedures - and put a great deal of the business logic in sprocs.
Is there any other way to create and use temporary SQL Server tables? Does EF 6 have any added capabilities in this area?
I've never seen temp tables used in EF.
If the data process in question is that intense, it's probably better to leave it as a stored procedure (I'm assuming this is how the legacy code worked). EF can run stored procedures with no problem; you can even get the results back as a model entity.
If you really don't want to use a stored procedure, you could also simulate a temp table in EF by using a regular table and just filtering it to the current session (through the use of a GUID or some other throw-away synthetic key). This technique works fairly well, but has the disadvantage of needing to clean the garbage data out of the table when your procedure is done.
Some background to the problem:
I have a hierarchy of objects that looks like this:
Each 'CallAction' will have Queue OR a Question based on the 'ActionType'.
When a workflow is created through the website it is limited to 2 levels of questions. All of this has been implemented in C# and the tables created in the SQL Server. I now need to find a way to add this all to the database.
As I am extending Website Panel (an open source project) I'm trying to follow the same standards they have been using this means every call to the database is done using stored procedures.
My question is: is there any way I can just pass a 'workflow' to the SQL stored procedure and handle the multiple different levels in SQL, or am I going to have to create multiple stored procedures and handle the different levels in my C# code calling different stored procedures based on the hierarchy?
I also understand that I could access the database directly using SQLCommand or LinQ but I would rather use stored procedures if that is possible.
No such thing as objects in SQL Server. You could pass properties as named parameters to a stored proc (this way no need to maintain their order), and have the stored proc put them where they need to go: update, insert, delete.
For entire workflows, you should use matrix parameters. Google "directed network matrix representation". A single matrix will then represent all of your objects and their directed links to each other (1 - forward, -1 - back). If you want to get a little fancier, instead of using numbers, you can create a string matric which stores object names along with direction. If you multiply direction by a Real number, you get connection weight (e.g. probabilities).
But ALL this is a very backwards way of doing it. You should create this kind of adaptors in C#, and do bulk inserts/updates.
Sorry I couldn't be more helpful. Good luck.
EDIT: since your "workflow" looks a lot more like an ontology than a real workflow, you can see about SPARQL and such - ontology-specific SQL languages. Or Google "storing ontologies in SQL databases"
If you can express the hierarchy in XML, then you could pass it as an XML data type to your stored proc. The proc could then read the XML using XPath etc.
I think CLR Stored Procedures can help you:
http://msdn.microsoft.com/en-us/library/ms131094.aspx
For a while i have been googling how to use LINQ and connecting my stored procedures in c#. Anyone out there can give me some form of hint or some sort of help? That would be greatly appreciated.
You can have the Visual Studio / DBML designer scaffold the stored procedure for you and generate the required code to call it, with strongly typed parameters. Just drag the required stored procedure within the DBML designer:
dragging it within an existing entity will make your stored procedure returns a collection of instances of that type, if possible;
dragging it to the Stored procedures panel will make it returns a collection of rows specific to that stored procedure.
Easiest way is to use a Linq-to-Sql layer or .dbml. Scott Guthrie has a great series of blog posts on setting this up. Start here: http://weblogs.asp.net/scottgu/archive/2007/05/19/using-linq-to-sql-part-1.aspx
Maybe I'm missing something here... we are trying to adopt a framework for data access and have been exploring EF3.5.
Everything we do in our organization is required to be in a stored procedure so the DBA's can have a feeling of control. If I generate my entities from my database schema, I see how to map stored procedures to the update/insert/delete commands of an entity, but there is no mapping for the retrieval of the data.
Is this always internal to the framework, or can we somehow map our retrieval procedures to their respective entities? If it is internal, is it possible to view/modify the generated sql?
Thanks in advance for your help.
When creating a model you are able to add a stored procedure to it.
In case these stored procedures are returning collections of Entity types they can be added to the model.
Open the model in designer, right-click on the procedure and select the "Create Function Import" option. Specify the correct return type, and you'll get a method retreiving entities.