EF CodeFirst adding table to database - asp.net

I've got ASP.NET MVC App with database added as Code First from Database. Now I need to create new table in datebase and add column in other. I've create it manually on database, now my question is how can I update database in Visual Studio project to show and allow me to use my created table? I've tried deleted all classes and connection string and add database again, but I've got exception:
"This operation requires a connection to the 'master' database. Unable to create a connection to the 'master' database because the original database connection has been opened and credentials have been removed from the connection string. Supply an unopened connection."
Also helpfull will be information/link to tutorial how can I add a table from Visual Studio and sent changes to database.
Thanks for any help.

Related

asp.net mvc5 database cannot be display , its login successfully

I create a database code first in asp.net mvc5 the database its login and register successfully but its not display it app_data (i press on show all files) and server explorer?
The first thing you need to do is to configure the connection between your app and your database. If you do this then you need to install entity framework migrations:
https://msdn.microsoft.com/en-us/library/jj591621(v=vs.113).aspx
Code First Migrations has two primary commands that you are going to become familiar with.
Add-Migration will scaffold the next migration based on changes you have made to your model since the last migration was created
Update-Database will apply any pending migrations to the database.
If you want to create sample data in your database you need to create some database seeder and run it in seed method as follows:https://learn.microsoft.com/en-us/aspnet/web-api/overview/data/using-web-api-with-entity-framework/part-3
Your question is quite unclear. I don't know what would you like to see in app_data folder.

Cannot fill azure db with data. WebPage is connected to database but don't want to create tables

I created website using asp.net MVC with Entity Framework Code First. It worked before on Windows Azure, the site was available publicly.
For some reason, I deleted database that was used to store data of my webpage. I created new one, similar to previous one.
I properly connected my site to database (in solution I clicked "Publish", in "Settings" there is place to type data about database, in "Destination Connection String" window I typed all needed data and clicked "Test Connection" - everything seems to be fine)
I published my site, the site works correctly, but when I go to page that gets data from database is see this error:
Invalid object name 'dbo.AspNetUsers'.
In Visual Studio in "SQL Server Explorer" I can see what is in my database. In fact, there is nothing inside (there is one table: "__MigrationHistory" and as I suspect this is created by default).
How can I generate all those tables again? It should generate database structure automatically.
PS: Accidentally I deleted "migations" from my project, do you think this is the cause?
This is not my projects blame, it works OK on localhost on my computer, when I delete localDB, it generates new one without any problem.
Yes, you need migrations. But you can always re-create them (well, if you removed them all it will be initial migration containing all of your previous migrations).
To quick fix you can backup and restore you database on the server. And if you need to re-create migrations do the following:
Backup all your data
Delete the entire database (local and remote), delete all migrations from code.
Create databases (local and remote one).
Run "add-migration Initial" command from PM console
Run "update-database" from PM console.
Upload your code to the server again. If database is empty, new initial migration should be automatically applied.

asp.net with connection to sql server 2008

i am new in using asp.net language and i try to edit code asp.net which is already connected to a database on iis server
the problem that i faced is:
i can retrieve all table that already exist on that database but when i add new table i cannot retrieve it on asp.net "not found"
the same problem when i add new column all already exist column , i can retrieve them except the columns that i added them
but when i edit record , the record updated on asp.net so the problem when i create any thing or alter name of a column
thanks in advance
What sort of data access technology are you using? ADO.NET Entity Framework (one with .edmx file) or ADO.NET (one with .xsd)?
If you are using one of these you need to refresh or add tables in these diagrams too. Otherwise please get your code here so that picture gets clearer.

Entity Framework 5 code first - where is the database created

I am following along this tutorial! everything works great, the blog names saved and retrieved from the database (assuming) since they are displayed in the console. Yet there I don't find any database created for it in local db nor the sql express on my machine. How to find where is the database created in the code first approach.
You can get the connection string by using ctx.Database.Connection.ConnectionString which will tell you what database you are using.
I was having a very similar problem here. My connection string was set to .\SQLEXPRESS and a generated Initial Catalog name, but when I connected to .\SQLEXPRESS using SSMS, nothing showed up.
Using IntelliTrace in VS 2010, I was able to see the ADO.NET command, and in the Autos grid, it showed me the SQL command and the following connection string: Data Source=(localdb)\v11.0;AttachDbFilename=|DataDirectory|(my_context_name_here).mdf. Then, using the Server Explorer I can connect to (localdb) and see the data.
This problem happened because my context was derived from DbContext, but it did not used the base(connectionStringName) constructor (like the UsersContext that comes with the MVC4 template does). Changing that, I can see the database in .\SQLEXPRESS.

How to work with LocalDB and EF, without using migrations

I am on VS 2012 RTM, with EF 5. I am doing Code First, but trying to ignore Code Migrations since I am just in development. To avoid them, I have this set
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<SomeContext>());
Occasionally, even when I don't think I've touched the model, it decides to recreate. That's fine. But it usually results in the error
Cannot drop database because it is currently in use.
So I decided to delete the database entirely. I go into VS, go to the "SQL Server Object Explorer", find the DB and delete it. Now I'm stuck at
Cannot attach the file '{0}' as database '{1}'
I had this happen last night and I just fiddled around until it work (shut down tasks, restart VS, changed the DB and file names in the web.config, etc.
So question 1) How do I get out of this state?
But the more important question, how do I prevent myself from getting in this state at all?
The SQL Server Object Explorer window can hold a connection to the database. Closing the window and all windows opened from it releases the connection.
I had the same problem and managed to fix it. The main problem is that EF won't automatically create the database. The solution is not very hard.
First, go to the Solution Explorer and manually create the database. To do this, go to the folder where your database is located (usually App_Data). Right-click on this folder and click on Add -> New Item. Select the Data tab and select SQL Server Database. Make sure you enter the name Entity Framework expects (which is specified in the Cannot attach the file '{0}' as database '{1}' error message).
Then most likely you will get an error message that says that the database does not contain any model metadata. To circumvent this, change your database initialisation to:
Database.SetInitializer(new DropCreateDatabaseAlways<SomeContext>());
Now run the application and the database should be there with tables correctly created. Once this step has been successfully executed, you change change your database initializer back to:
Database.SetInitializer(new DropCreateDatabaseIfModelChanges<SomeContext>());

Resources