I'm totally new to Entity Framework and have done some reading, and as a test I have put together a very brief test framework with just two small Entities. I then right clicked and selected "Generate Database from Model" which takes me to the SQL Connections page. However, none of the previous connections I have used appear in the drop down list, and when I select Create New Connection I only have the options to use 'SQL Server Compact 3.5' or 'SQL Server Database File'.
I have come across this before with SQL Express and the work around is to create my own Connection String to access the required Database. However, with me using Entity Framework to create the database, it is impossible to write an appropraite connetion string.
I therefore seem to be in a Catch 22 situation.
I cannot write a connection string until the database is created.
I cannot create the database from EF without accessing SQL Server (via a connection string).
Anyone come across this or can point out what I'm doing wrong. Like I said I'm totally new to EF so I apologise if this is a very basic question.
Unless you want to use a database file in a user instance, you need to either use SQL Management Studio Express, or use the SQLCMD command line tool to create the database. I would recommend SQL Management Studio Express as it is easy to learn in my opinion.
The Entity Framework tools are intended to be used to create the database schema, not the database itself. You still need to define the file groups, security information, and other basics of creating and configuring a blank database.
See this Q/A for appropriate links:
How to create DB in SQL Express using SQL commands?
Thanks #psuedocoder, your assistance helped me resolve the problem. The answer in the end was simple, but not intuitive to me, hence my difficulty. I thought some elaboration on the answer might help others who are equally new to Entity Framework as me.
From Visual Studio
Create your EF model in Visual Studio.
Right click the model canvas and select 'Generate Database from Model'.
You are then asked to select the database connection. Select 'New Connection'.
In the 'Add Connection' dialog box, rather than 'Browse' for an existing database, just simply type the name of your new database in the Database text box and Click Connect.
When you try to connect you will get a warning saying the database does not exist, but you will be asked if you want VS to 'Create It'. Select Yes.
As #psuedocoder states this does not actually create the database in SQL Server, but it does create an object in your VS soluton which contains the TSQL script required in order to create your database.
From SSMS
Go to SQL Server Management Studio and create a blank database of the same name used in step 4 above.
In SSMS select open file and navigate to the windows folder containing your VS solution files.
Open the TSQL script file. This will have a DB Script icon and have an .edmx extension.
Click Execute, and you will have your new database created from your EF model.
Related
As a beginner in C#/VS I want to practice making basic LINQ queries on simple DB's. I downloaded the "famous" Northwind and Pubs databases on the official Microsoft websites (https://www.microsoft.com/en-us/download/details.aspx?id=23654). It gave me a .msi files that installed 6 files on my c: instnwnd.sql, instpubs.sql, NORTHWND.LDF, NORTHWND.MDF, PUBS.MDF, PUBS_LOG.LDF.
When I launch Visual Community 2015 and simply make an "Open => File..." and open the "instnwnd.sql", it opens the .sql file without problem. The problem is when I'm trying to make a connection to the database, being either through the Solution Explorer (Data Connections => Add Connection) or while I'm trying to create an EDM (Database first), which is what I want to do eventually.
The exact problem is that one : the Northwind and Pubs databases doesn't show up in the database list when i'm trying to add a connection, I only get the system databases. Here is what I get :
http://img15.hostingpics.net/pics/128870screenshot1.jpg
I read many MSDN documentation pages and several StackOverFlow topic about the matter (the closest of solving my issue was that one, but didn't work :How to connect to LocalDB in Visual Studio Server Explorer?). I have SQL Server Express 2014 with tools, started SQL services in my Windows Services etc.
What should I do in order to have those databases available so I can generate code and starting making my LINQ exercices ?
Thanks in advance !
You need to attach the databases before you can see them**. You're having a problem attaching the databases due to versioning (which is odd), but your sample db's are very old. Perhaps try newer ones from here https://northwinddatabase.codeplex.com/ and make sure you attach them before expecting them to show up in the tools.
** Technically this is not true. I believe there is a way of connecting to sql server and passing a file path in the connection string, however this will likely still fail for your due to the error you get trying to attach the dbs you have. Additionally, it requires a specially crafted connection string which may or may not be usable with the gui tools you're trying to use. It is also, in my opinion, a less common way of accessing sql server databases, so I would stick with attaching them first. If you want to find out more about this, I believe the feature you need to research is called 'sql server user instances'.
ps: I'm not sure "replicating" is the correct term for this.
other than the SQL statements/stored procedures defined for the application's use, we have to use cmd to execute anything on the database in case only SQL express is installed (without management studio). no?
since that is out of my control (I mean to have management studio or not) I'd like to provide my application's super admin(IT supervisor in my case) with GUI similar to SQL server management studio new query option.
the basic idea would be to have a textarea to enter SQL command, a submit button and datagridview to display results. this part is feasible.
but I'm debating the idea itself, is it worth being implemented in the first place? what are the trends relating to this kind of situations? any best practices?
if we were to expand the basic idea, I have 2 points:
- SQL server management executes multiple queries at once and displays them is the same result window even if they don't match in columns, would that map to multiple datagridviews?
- SQL server management have a messages tab to display execution related notes, how to get those message in the application in order to be displayed as well?
this example of management studio from MSDN
any pointers to resources, possible duplicates or even the correct SE community I should've posted this on (I tend to mix them up :)) are most appreciated.
Thanks
update:
I don't mean providing this functionality for all the users of the application.
since I'm both the IT supervisor and the developer, anything related to the application will end up being brought to me. so I have access to the database anyway that's not a security breach.
but instead of having the application in developing cycle I'd have on-spot fix capability.
for example:
if an urgent report was required: I'd query the requested data in the private IT section without delay, then make changes to the application with no time-pressure.
Have you already considered using SQL Server Management Objects?
It is basically the same library in which SQL Server Management Studio (and the Express Edition) are based. So you wouldn't have to worry on differences about how you run/execute your queries.
Also, you can redistribute the necessary .dlls, without the need of installing the SQL Server Management Studio itself: http://technet.microsoft.com/en-us/library/ff713979%28v=sql.105%29.aspx
In our company we use a tool that can run SQL Queries like the one you want, getting information from those objects (SMO) with the same SQL Messages outputs and tables (you can use it to create your gridviews) as SQL Management Studio does. But we use it for the purpose to automate customer deploys/patches.
Katia,
You want to run ad-hoc read-only queries against your database and then give the results to your client.
Make sure you have appropriate database permissions
Use the SqlCommand object to send in the sql
Get the stuff back as a DataReader (reader)
Iterate over the column names with reader.GetName(i)) for reader.FieldCount
While reader.Read iterate over the column values with reader.GetValue(i) for reader.FieldCount
Parse the above into an HTML table
Send the HTML table to your client and get them to open it in Excel
Make sure and include the query that you used so that you can replicate/refine
And we're done.
PS Let me know if you need more implementation details
I am trying to use Entity Framework 6.1.1 in Visual Studio 2013 with C#. I am using SQL Server 2012 in the back end. I have tested a regular SQL Server database which works fine. I used the Database First design pathway. I have a database that is actually a Linked Server in Microsoft SQL. The back end of the linked server is SQLite. The linked server is interacted with via several views in a regular Microsoft SQL database. I do not have to write or modify data in the linked server in any way.
I cannot get Database First to build a model of this database with the views, regardless of what I try. I have tried to use Code First to manually write an interface with the database, without any success.
Is it possible to get Entity Framework to talk to this database? How can I do so?
Okay, if anyone later finds this and has the same problem, I resolved this by using Code First and manually creating Entities with names to match the SQL tables and variables with names to match the fields. This worked fine and I could even use navigation properties and all worked great.
I'm creating my first project in (C#) ASP.NET MVC3 using Visual Studio 2010 Professional, I'm creating a very basic blog system. During the tests I created a some tables and now I want do store different data on those tables but I keep getting this error:
The model backing the 'CategoryContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.
Obviously I need to update the tables in database but I can't find where I can access the tables. I can open Server Explorer using CTRL+ALT+S but there is no Database or tables there.
Here is the screenshot of it:
http://img710.imageshack.us/img710/8944/screenshot001kd.png
Where/how can I find and/or edit the actual database tables?
Thanks.
Well here is the best I can do for you:
If using SQL Compact Edition then you should look into your project folder on the filesystem and look for a folder called app_data and you should have a database in there.
Alternativly you could just open your web.config file and look for the section that is the "ConnectionStrings" section and just look for the location of the database.
If it is in SQL Server Express, then in your Database Explorer window, click on the database connections and create a connection to your local sql server express. You should see it in the list of databases as the machinename\sqlexpress.
From your error I'm going to guess that you're using Entity Framework's code first approach.
What happened is that Entity Framework has created a database for you on the local sql express install. To find the actual database, look in your web.config file and find the connection string section which will tell you the server and the catalog for the database. You can either us Visual Studio to access the database by using the data connection area found in the server explorer panel in your picture. Or you can use Sql Server Management Studio to do the same thing.
Now the reason why you're getting an error is because Entity Framework created that database based on the entities which you initially defined. Once you have made changes, the database no longer matches the entities and you get the error listed above. Your solution is to either delete your database once you have found it and allow the auto-magic to happen again, or you will need to do some changes to your code to not auto-generate your database each time you make scheme changes.
I would suggest you look through MSDN's ADO.NET Entity Framework 4.1 and read up on 'code first' development styles.
Good luck, and hope this helps you some.
I'm having trouble getting EF 4.1 working on my computer. It seems to be some problem with my database settings. I was trying out this simple walkthrough:
http://blogs.msdn.com/b/adonet/archive/2011/03/15/ef-4-1-code-first-walkthrough.aspx
But when it reaches db.Categories.Add(food); it just freeze.
I have a normal SQL Server 2008 R2 installed, not SQL Express. There also seems to be some problems with creating .mdf files instead of a direct connection to the localhost SQL server.
I've also tried adding an entity model with a database connection, but this does not seem to work. Do anyone have any pointers for me.
Thanks for any answers :)
edit:
I now get a System.Data.ProviderIncompatibleException with "Vendor returned no ProviderMaifestToken-string"
I guess this is because Database Re-Initialization.
If your EF code try to drop and create database with SQL Management Studion open or connected.
This situation comes up.
I found the problem. These tutorials do not contain information about some vital adjustments that I needed to make it work.
1) Your application must have an App.config containing a connectionstring named with the same name as your class that inherited from DbContext. In my case "TestEF_CF.ProductContext".
2) The database cannot be created before you start to use it. Just set the Initial Catalogue to the name you want Entity Framework to create when it autocreate the database.
Once I did this, it worked properly. I got further with the first step, but got an exception when I tried to save to the database. When EF autocreated the database everything worked fine.
One thing that may have made it more problematic on my development environment could be the fact that I cannot use local mdf files directly. Still it would not hurt if the EF team could share these details more open than they do now, it would save me (or us) some frustration.
Try setting the datasource on the connection string to .\SQLEXPRESS
It did work for me