I am developing an Asp.Net WebForms website. I am used from my prior project to the "dataset designer", it was my main worksite, I had there all my tables and also hundreds of queries which I could easily call from code like this: "this.InventoryTableAdapter.MyQuery('parameters'), it was great, but I can't find such a thing in Asp.Net, am I missing something. how do you access your special queries in code. do you have to write cumbersome ADO.Net code for each small thing, can someone enlighten me please.
You can still add a database designer to your .NET application. Today's standard ORM for .NET is Entity Framework, although many other good options exist. One thing to research is Entity Framework Database First. Here's how to get started...
In your project, choose to Add New Item. In the Add New Item dialog choose the Data items and then select ADO.NET Entity Data Model. Click Add.
In the Entity Data Model Wizard, select EF Designer from database.
If you have database connections defined within your development environment, you may see one of these connections pre-selected. Or, you can create a new connection to your database.
After setting up your connection, select the tables you want to generate models for. These will be the tables available in your database designer.
This is a very high level how to. You can read more about this approach here: https://learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/database-first-development/creating-the-web-application
Another ORM option is LinqToSql. You can read more about creating a database designer ".dbml" file using LinqToSql here: https://msdn.microsoft.com/en-us/library/bb384428.aspx
I hope this helps.
Related
Before I posted this question, I did some Googling first on how a database was created through C# and mostly it points to either SMO or SQL query files and it was the time of SQL Server 2005 and 2008.
So at this day in age, is there an easier way to create a database with empty tables, tables with data in them by default, stored procedures and views?
I need a suggestion.
I think the answer is probably Entity Framework. You can do 'code first' and use database migrations, allowing you to write your C# code and use that to generate a lot of the database for you.
Ultimately though, 'easier' is subjective. I personally find EF great for the 'normal' stuff, but at the end of the day, if you need a stored procedure to do some custom logic; you need to write the custom logic, in some fashion.
Maybe have a look and see if you think it fits your needs.
https://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application
Looked at the database projects in studio 2013. You create a database as a series of scripts using a familiar GUI. However, changes are published - this process creates a unique change script targeting the connection you define. For new databases the whole thing gets created, but publish against a partial or out dated version and the script created in a change script to bring it up to date.
You can even writ unit tests against your database using specialist tools, although I do find them lacking a bit.
More on msdn - here
Depends. right out of gates. Sp and views. Best shot is directly from database through a workbench. I can then capture definitions and store in a file to be replayed through c#
As for tables there are many orms that can generate tables via c#. Look at entity frameworks. Code first examples
I have generated tables using EF Works fine. I then went into database and created views and sps.
The trick is to migrate new views and sps into your EF model U can google entity Frameworks code first ... Adding views and SPs.
Worst case is u create database all through database workbench. Create a script that an be played to recreate eveything. By running. Then use EF DATABASE first approach
In either case u end up with a good set of autogenerated code to manage CRUD and object management and an abstracted data model
We have to maintain a very old application.
There is an old part (all data access is handled with stored procedures) and since my predecessors took over the application, they started to build a new part for the new requirements which uses entity framework 6.
Now the customer has a new requirement that needs access to one of the tables for which there isn't an EF-model yet.
Is there an easy way to automatically create an EF-model from an existing database table? (I only want this one table, not the whole remaining database!)
Do you know of any tool that can achieve this or do I have to write it by hand?
Thanks in advance
I use EF Power Tools which is a plugin for Visual Studio.
Download here
With the plugin, you can reverse engineer your database in order to create your objects and mappings in your application. Despite the fact that it will save you a lot of time, note that it is not perfect. You might want to review your indexes and relationships as some were missing in my case.
To Reverse engineer your database, it's as simple as this:
The Reverse Engineer Code First command is used to generate the POCO,
mapping (configuration), and derived DbContext classes that are based
on an existing database.
Right click the project and select Entity Framework –> Reverse
Engineer Code First.
Enter the information about an existing database
based on which you want to reverse engineer Code First.
You can check this link from Microsoft for a complete example:
https://msdn.microsoft.com/en-us/data/jj593170.aspx
IMPORTANT: As of Entity Framework 7 (not released yet), only the Code First approach will be used. You might want to consider this before choosing another approach like Model First, for instance. You can read more about: EF7 - What Does “Code First Only” Really Mean
Open the Entity Data Model (edmx) file (edit: or create one if you don't already have one), right click on the design surface in a blank area, and select "Update Model from Database".
Select the proper connection string if prompted, then choose the "Add" tab, and drill down to the tables in your database you want to add and put a check mark by them. Click Finish and you're done.
Check out the EF power tools plugin which give you context menus for Reverse Engineer Code First as well as other code generate options
https://visualstudiogallery.msdn.microsoft.com/72a60b14-1581-4b9b-89f2-846072eff19d
Can't you just create an EDMX and only have it select the tables you want it to point to instead of the whole DB?
Look at Generate From Database in this article
https://msdn.microsoft.com/en-us/data/jj206878.aspx
Let's say you have a database that is currently in use. It has one table that contains some information about Customers.
And let's say that you want to create an application around it. You need to use the Customers table in your program, but you also need to add some new tables, possibly connected to the Customers. For example, you need to add an Orders table that has some association to the Customers table.
I have started to learn Entity Framework. I have learned a bit about "Database-First" and "Model-First" data models. But the situation is, what I want to do doesn't fit into any of these categories. I neither have a completely empty database, nor a finished database schema that can directly be used in the program.
What should I do? If I go with "Model-First" and design my entities using the Visual Studio Designer, can I just use it to create the relevant part of the database schema?
Go with Data-first, THEN go with Model-first. You don't have to pick... use the data-first wizard to generate EF for your existing table, then use the designers to create additional objects, then sync that back to your database. The concepts of model-first and data-first only applies to the initial creating of your EF stuff - anything you do after that can by synced in either direction.
This shows how to put database changes back into your model after it's created - you can do the other way too, but I can't find that article - all the new videos and crap are hard to navigate - you may be able to find it.
http://msdn.microsoft.com/en-us/data/jj206878
After all, the framework wouldn't be very useful if it locked you into never being able to update anything. Right?
The more development into your project, the more Database-First it'll become, so I suggest you to go with Database-First right now and then extent your EF entities and then implement/fake/mock the missing pieces
If you haven't read about TDD, it's time to do so now. It'll save a ton of time down the road especially when working with an ever changing data model.
We are upgrading an old VB6 application which sits on a SQL Server 2005 database, to an Entity Framework solution. The database remains the same, except - we're adding a new table. Is it possible with Entity Framework, to maintain the existing structure, when it gets installed on a client PC - and just add one new table?
Is this how Code First will work? Can I be 100% certain that no other tables will be modified?
i don't think, the effort is worth it to switch to code-first if you have an existing database and want to add only one table.
it is possible to map code-first classes to an existing database (reverse engineer code first). actually, i'm not very experienced with that workflow, but i know you can. You have to deal with a lot of manual mapping (with DataAnnotations or Fluent API), so in your case i would recommend to use the Entity Framework Database First workflow, since adding a single table saves you a lot of work.
this link has some useful information: Arthur Vickers Blog - Don't use Code first by mistake
You have two options, use a database editor such as SQL management studio to create the table which you can then map to a ef entity, or use migrations for ef which will let you update your database via ef.
Take a look at the migrations tutorial here: http://msdn.microsoft.com/en-gb/data/jj591621
I am using the database first approach, since a database developing team is doing the changes I require in the database on the SQL server for me.
Hence, I have to update the EDMX whenever the schema in the database changes.
Note: Changing one single table directly does not work for me, because VS doesn't always detect the changes right (for this issue, here are some details in SO if you're interested).
Hence, I am using the following workaround (regenerating all the tables):
In VS 2012, open the EDMX file by double-clicking on it. The graphic representation of the tables is shown.
Left-Click into the EDMX designer, then select all tables by pressing CTRL+A. Then, remove them by pressing DEL.
Right-Click into the EDMX designer and select "Update Model from Database ..." in the context menu.
The Update Wizard opens. In the "Add" tab, check "Tables", and depending on the requirements, check "Pluralize or singularize generated object names", "Include foreign key columns in the model" and optionally "Import selected stored procedures and functions into the entity model". Usually, I am using the "Pluralize..." and "Include foreign key columns..." options.
Click Finish. Now Save by pressing Ctrl+S.
That workaround works fine for me, and requires just a minute to update the model reliably.
I'm developing a Silverlight 4 application (C#).
I used Silverlight Web application template and Entity framework to get started + VS 2010.
I now have a running application that connects to the database and displays data properly.
The database has one table with 5 attributes.
So what i need to do now..
I have a connection string to connect to a database on an external server. The database is similar to mine, but with more tables and updated information.I need to connect to this database. (EDIT: since this is outside my project i wont be having any EF for this but just privilege to query a single table. (VIEW to be specific))
Since I know the attribute names, table etc., use a SELECT query and get the data. So execute and get the result set. (E.g. "SELECT R.name, R.marks FROM results R").
The result set from query will be stored in, for example a data-table, and then insert into my database.
I also created an object class with accessors for the table, so that I can give the 'result set' the structure before I insert.
Theoretically this sounds possible when I chalk it on my board but I want to know if this would work?
I mean having 2 database connection open. Is this possible? If I follow the same steps as above can I achieve what I want?
Please let me know if I'm unclear about anything. This is just a smaller version of the real application but the logic i need to implement is the same. :)
If there is a better approach I'm happy to consider.
Cheers
You may be over thinking this a little. I add a new class project, and add a new ADO.NET Entity Model to that project using the connection string for the second database. After that, you'll be able to reference the new EF project from your Service or Host (Web) project.