I have developed a project using entity framework on my local machine. What I am wondering is what is the best way to move the project to live server. Do I need to recreate the entities on the live server, if tables are same? Or I need to change the connection string? Reason why I am asking is that on live server I might not have access to the Visual Studio to re create entities.
I searched around cant find what I am looking for. Appreciate your comments.
You need to create the database and objects on the server manually. You could export the DB schema to a SQL script through Management studio.
Also ensure that you get rid of the DropCreateDatabaseIfModelChanges strategy, which might be the case on your development environment- especially if you are using rapid prototyping.
If your connection string is externalized to web.config, donot forget to change that as well.
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'.
I have an existing ASP.Net Web Forms application which is using EF Code First with Existing Database i.e. I am using EF classes and DbContext to point to an existing database.
I want to give the project the functionality to create/edit tables/fields and just wondering if people can recommend the best way to do this. I can't use migrations because the project is used on several different servers/databases.
I thought about putting something in the Global.asax file in Application_Start using SqlCommand. Is this a good idea or can you suggest a better way to do this, preferably with EF?
I'm not sure what if any code would be helpful so please let me know if there is anything you would like me to add to the question.
EDIT:
Based on answers so far felt I should also note that I cannot directly access the servers the application is installed on because they belong to clients. Project is deployed locally, zipped and uploaded onto a site for their download.
When using EF Code First, you should use EF Code First Migrations, although you say it's impossible.
Your only reason not to use this is the multiple database servers. Do you use Distributed Transactions?
Otherwise, the only variable is the ConnectionString to the database server, and EF Migrations will do all the work for you to update your SQL schema.
It's probably a bad idea to do this as part of application startup - it'll require that the user that your application connects to the database with has escalated privileges in order to create/edit tables.
You can use migrations to initialise a database as part of a deployment process using the migrate.exe which is part of the EF NuGet package.
It's probably also useful to read a bit more around migrations - there is support for multiple contexts that can each be migrated separately...
Alternatively you could use a SQL script as part of your deployment process but then you'd need to manage the SQL by hand...
I am working on a project which requires me to write several POCO classes using Entity Framework Code First. There is a lot of entity relationships and inheritance going on and its hard to keep track of everything just looking at the code. Now, as we know, Entity Framework Code First yields an .mdf file as your database, and i was thinking for verification, a database diagram would server me better.
Is it possible for me to view my database diagram in this scenario, and how may i do so??
You could always point it to a SQL Server Express database - by default an MVC 4 project uses LocalDB but if you're more comfortable in management studio you can always create your own database and change the connection string to that.
Also from memory I believe you can also attach an mdf file in management studio but may have trouble while the application is running. But I could be thinking of something else there.
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
I have deployed plenty of software to my clients. Mostly are Window Forms applications.
Here is my current practice.
Manually install SQLExpress and SQL Management Studio to each client PC.
Then use ClickOne to install the code from the server.
When there is a changes in code, I will use ClickOne to deploy -(NO PROBLEM with this step)
But when there is a change in a database column, what do I do?
I have even tried writing a database update script. Each time the program starts, it will read through the .sql update file and run them if the database exists. This solves the problem of updating the database columns, but it does not help in my DEBUGGING work when my customer complain there is a wrong data. At that point, I have to personally go to their site to check it out.
I find it difficult to have the database installed on the client PC as it make my debugging work very very difficult. I am thinking about moving my client database to a host on an Online server. But that then comes with these constraints:
What if the internet is down?
What if my customer has no internet?
Could you help to advise me? Is this a common problem faced by developer? What is the common practice out there? Does Window Azure or SQL CE help?
Depending on the data I would recommend using SQL CE.
If the data isn't too much, speed is not the primary goal (CE is slower than Express) and you don't need DB-Features not supported by CE (e.g. stored procedures) it is the better choice IMHO, because:
The client does not need to install a full SQL server (easier installation/deployment)
You do not have problems with multiple SQLExpress instances
Your SW doesn't need to worry if there even is a SQL instance
Less resources used on the client side
Additionally the clients could send you their SQL CE DB-File for inspection and you do not need to go to their site.
It is also relativly easy to implement an off site sync with SQL CE and MS Sync FW.
Installing one database per client PC can be tricky. I think you have a decent handle on how to deal with the issue currently. It seems like the real issue you are currently facing is debugging. To deal with this, there are a couple ways you could go:
Have the customer upload their copy of the database back to you. This would provide you with the data they have and you could use it with a debug copy of your code to identify the issues. The downside is that if the database is large it might be an issue transferring it.
Remote onto the customer's machine. Observe the system remotely using something like CoPilot. That way you could see what is happening in its natural environment.
There are probably other ways, but these are a couple of good ones. As for using an online database, this is an option but it brings its own set of issues with it. You mentioned a couple. As for Azure, that is cloud-based (online) so the same issues will apply. SQL CE won't help you any more than your current installation does.
Bottom line is that I would recommend you look into the ways to fix your one issue (as listed above) instead of creating a whole new set of issues by moving to an Internet-based solution. I would only recommend moving to the Internet if it was addressing a larger business need (for example, mobility). Doing the same thing you have been doing only online will probably just make life harder.
To recap the comments below since they are so pertinent to the issue, if you are choosing between file-based databases that don't need to be physically installed on the machine, your best choices are probably between SQLite and SQL CE. Microsoft supports SQL CE better but it is a larger package and has less features than the trim SQLite. Here is a good discussion on the differences:
https://stackoverflow.com/questions/2278104/sql-ce-sqlite-what-are-the-differences-between-them
However, the issue gets more complicated when you start looking at linq2sql since that is designed for SQL server. Microsoft does not support SQL CE with linq2sql out of the box, although there is a work-around that will get it to work:
http://pietschsoft.com/post/2009/01/Using-LINQ-to-SQL-with-SQL-Server-Compact-Edition.aspx
SQLite is not supported at all with linq2sql but there is a way to use linq to talk with SQLite:
LINQ with SQLite (linqtosql)
This library also supports other common databases including MySQL and Firebird.
You could use the SQLCMD utility to execute the change script, as mentioned in this related question