When the app runs for the first time with a new connection string name it creates the Local Database files in the App_DATA folder, but it doesn't recreate it for the 2nd time if the database files missing in the App_DATA folder... Will have to change the database name given in the connectionstring. Why?
How to solve this problem?
Where it stores the state that the database has been created for the first time...
How it does the comparison/check to confirm whether the database has been created during an app start previously?
Data Source=(LocalDb)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\aspnet-WebDemo-20210104031025.mdf;Initial Catalog=aspnet-WebDemo-20210104031025;Integrated Security=True"
it knows the database "aspnet-WebDemo-20210104031025" has been created and looks for it in the App_Data folder. doesn't re-create it for the 2nd time when it is not available in the folder
** Didn't use code to create custom tables, just only the basic template comes with Visual Studio 2017 MVC web application including authentication(not azure- just user creation with email and so on....)
Related
I have four projects in one solution
RapporAMS.Data (Contains the connection string and database access queries and entity framework installed, it also have connection string in app.config file same that of web.config in forth project)
RapporAMS.Service (Act as an interface between RapporAMS.Data and RapporAMS.MVC )
RapporAMS.Entities (Contains all the Entities and view models used in the solutions i.e business objects)
RapporAMS.MVC (MVC project front end of the application also have connection string in web.config and entity framework installed)
I have created the database in Plesk named "esssospa_AmsDB" with username and password. Then I used "import dump" to import .bak file. Now, it is showing file size and the total number of tables in the database.
I am not sure about database has been attached to esssospa_AmsDB or not
After that. I winziped only RapporAMS.MVC project and uploaded it to the httpdocs directory on the server.
Then I changed the connection string in web.config don't know how to change the connection string in app.config of RapporAMS.Data. This may be the issue of the error.
Problem 1: Database esssospa_AmsDB is not showing up in my SQL EXPRESS 2014 when I logged in using the credentials.
Problem 2: Having 500-Internal Error when I access the website, further screenshot is attached. click # Database on Server
Lately i was building C# windows forms application with service-based database.
I was forced to make few changes to make it work, but it's still not working as it should.
I'am doing this at start:
1. Add Service-based database to project
2. Create Tables
3. Add Entity framework 6.1.2 Model to project
This creates seccond database file in Project and in Data Connection. Now if i want to show any data, I must create dgv and dataSet pointing at this newly created .mdf file. Also i need to change Copy to Output Directory to Copy if newer. Now I can see my data, but i only see empty table in Data Connection in Server Explorer. Also if I open it and refresh, all data in my application are gone.
Is there any tutorial, or something about How to add Service-based database and connect to this file using entity framework, wihout coppying it to output directory?
If someone need a solution for this case, there is one:
Create database in any program (could be visual studio)
Copy it to /bin/debug/
Put bin folder to your solution in Visual Studio
Select database and make Copy to Output - Do not copy
Edit connection string in entity framework to
attachdbfilename=|DataDirectory|\file.mdf;
Database should now appear in Server Explorer in VS. You can now remove all newly created databases (leave only this one you copy to bin/debug/)
Now you will see correct database in Data Connections, and your program will update this one. You can easily coppy your solution to any other location, and connections will not be messed up.
Btw, I think this is entity framework error or DataDirectory should point to Solution folder, not bin/debug. I know we can change DataDirectory pointing...
I created a standard C# console application in 2010 Express, used the wizards to create a simple sql express db file which gets placed in the project dir, used the wizards to generate an entity data model based on the newly created db, wrote a few lines to test out entity.
Problem is each time I run the application it recreates the DB file in whatever dir the exe is in overwriting itself everytime and completly ignoring the original db file that sits in the project dir.
Does anyone know what I am missing here ?
Not entirely sure about this but try it out.
Make sure your connection string points to the database file in the project directory.
Select the file in Visual Studio and Choose 'Copy to Output Directory' ->> Do Not Copy.
Hope it works.
you must be using some way to find that file or some path parameter within the database connection that points to the database file.
Now either you can have that parameter be generated by code or make the directory specific and make the database file copy at a specific location from the original location so that the only that particular database is accessed using the application.
For doing that you can add another key in the application config file or at some other place so that the database your application is accessing is in the project directory itself at all times.
I've added a new MDF file to a .NET4.0 class library project, created a single table and a LINQ to SQL data model in order to access it. The connection string that was automatically added is:
Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ClosedIncidents.mdf;Integrated Security=True;User Instance=True
I run some code against it that first prints out the tables records to the console, add's a record, and then prints out all records again. The idea is to just check that after VS closes the MDF file retains the changes. This is proved true as on a second run of the app it moans that it can't add the same hard coded record because it already exists.
The issue I am pondering on, is why when in Server Explorer if I view the data of the table that it shows nothing is there.
My understanding is that it attaches the server or user sql server instance directly to the mdf file within my project. Changes through the instance are written back to the source mdf, not a copy of it. Why then does the program show the record being added but not Server Explorer?
Chances are the file is being copied to the output directory, and so the IDE and the application are actually touching different files. You can confirm this by doing a clean build.
I have a class library that attaches itself a tiny SQL Server database that resides in its Data Directory. When i'm using this class library with another windows application i see that once i compile my code, the database files get copied to the bin folder of my windows app project. However when i publish the windows app,install, and run it, i get the error 'An attempt to attach an auto-named database for file C:\Documents and Settings\User\Local Settings\Apps\2.0\Data..\DB.mdf failed.' Obviously this folder doesn't have the mdf files.
I guess this won't be a problem if i just add the database files to my windows application project. But surely there's a better way?
You could include an SQL script for creating the database into your "installation/run first time routine".
I guess that you've already stated that having a form of SQL Server is an installtion prerequisite.
For the data files I would recommend that you use a variable connection string for accessing your database. That way you can change the installation routine to include asking the user where they wish to have the data files installed and save that as part of your connection string to the app.config file.
Conversely you could also use the users selection of where to install the app to override the relative path stored for the database within your code (using the same connection string variable as mentioned above).