Add Controller database error in ASP.NET MVC 4 - c#

When using the Add Controller helper/scaffolder with Entity Framework CRUD integration, I get the following error:
Unable to retrieve metadata for 'MyApp.Models.MyModel'. Cannot attach the file
'C:\Users\Lev\AppData\Local\Temp\2d7daf19-d47f-df24-ac76-d3df4fg2sa\aspnet-MyApp-
20130409170533.mdf' as database 'aspnet-MyApp-20130409170533.mdf'.
The question is: Why is it looking for the database in my temp files? The database file is located in:
C:\Projects\MyApp\MyApp\App_Data
This is my connection string:
Data Source=(LocalDb)\v11.0;
Initial Catalog=aspnet-MyApp-20130409170533;
Integrated Security=SSPI;
AttachDBFilename=|DataDirectory|\aspnet-MyApp-20130409170533.mdf
And so it seems that the reference to |DataDirectory| is misbehaving. How can I set DataDirectory?
If I manually put the correct path into the AttachDBFilename property, then the scaffolding works. But I'd still love to leave it as |DataDirectory|.

Just restart the Visual Studio. This helped at my project.

Related

Could not add "ADO.net Entity Data Model" with sqlite

I am trying to setup entity framework with sqlite in visual studio 2012. So, here is what I have done so far:
Installed System.Data.Sqlite with "Design-Time Component".
Entity Framework Tools for visual studio 2012.
System.Data.SQLite,System.Data.SQLite.EF6 and System.Data.SQLite.Linq from NuGet.
Now I am trying to add "ADO.net Entity Data Model",By following steps:
Add New Item.
ADO.net Entity Data Model.
"EF Designer from Database" from wizard and click "Next".
Add "New Connection", Data Source: System.Data.Sqlite Database File.
After choosing db file and provide password when I click on "Next" button, following error occurs:
I have checked my db file in "Sqlite Browser" also I have setup password for db file with the help of following code:
string conn = #"Data Source=pahtto\App_Data\app_data.sqlite;";
SQLiteConnection connection = new SQLiteConnection(conn);
connection.SetPassword("123456");
connection.Close();
What am I missing?
Please guide me.
Thank you.
Try looking in your App.config file and change the connection string and set your password there.
old connection string
source=X:projectsmeowdb.sqlite;
modified Connections string
source=X:projectsmeowdb.sqlite;password=this is my password
Look at this link for more information
http://www.dxsdata.com/2013/09/visual-studio-with-sqlite-solving-error-file-is-encrypted-or-not-a-database/
It seems that the problem was in my db file even though it was working fine in "Sqlite Browser".
So, I just deleted and created new file and every thing works.

Not using database connection string

I have google'd the crap out of this problem, I cannot find a solution.
Using EF code first approach against a domain assembly, being consumed by a .net web application.
in the domain project there is a app.config, in there I have the following connection string for EF
<connectionStrings>
<add name="DefaultConnection" connectionString="Initial Catalog=easyDayTea;Data Source=localhost;user=sa; password=12344321" providerName="System.Data.SqlClient" />
Then in the context class TeaDb.cs I have the following constructor:
public TeaDb()
: base("name=DefaultConnection")
{
}
I have also tried just using "DefaultConnection" by itself in the constructor.
The problem:
Everything was fine until EF decided it wasn't going to take notice of additional classes/tables added to the context, so I removed EF from the project by deleting the migrations folder and empting the database of tables, then re ran enable-migrations and then the web application project to make EF do it's stuff to the database. However it did nothing!
When I run the web application though it works! and there is data (from the seed) in the tables, however not in any database i can see! It must be using a portable sql file, which doesn't make sense as I have it configured for a specific database / server by use of the configuration string.
I have also tried specifically specifying the connection string to use by doing a:
update-database -ConnectionStringName DefaultConnection -f
Still no joy.
If anyone could help me it would be amazing!
Thanks,
Xavier.
You'll find your database at Users\[youruser]\[Name you passed in your context constructor].mdf
app/web.config are only used if they are in the main project, if you have an app.config/web.config outside your main project it will not be used (some templates add them, but they are meant to be used as an example).
Check this answer for a similar problem with EF4
EF doesn't use the connection string from the app.config in the class library. It will use the connection string from the web.config in your web application. If you don't have the connection string defined in your web.config then it might be using conventions to attach the database with LocalDb in your App_Data directory.

Database connection issue in MVC3 project

I successfully followed the contoso university tutorial, getting it to work with SQL Express in VS 2010 - creating the DB in the App_Data folder. The tutorial follows the EF Code First model. Wanting to see the data in the tables, I opened the database in Server Explorer and was able to view the table data.
Now when I run it again, I can no longer access the database - the error given is:
One or more files do not match the primary file of the database. If you are attempting to attach a database, retry the operation with the correct files. If this is an existing database, the file may be corrupted and should be restored from a backup.
Cannot open database "SchoolDB" requested by the login. The login failed.
Login failed for user 'DEV\bwilliams'.
Log file 'C:\ContosoUniversity\ContosoUniversity\ContosoUniversity\App_Data\SchoolDB.ldf' does not match the primary file. It may be from a different database or the log may have been rebuilt previously.
DEV\bwilliams would be windows authentication.
Here is my connection string:
<add name="SchoolContext"
connectionString="data source=.\SQLEXPRESS;Initial Catalog=SchoolDB;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|SchoolDB.mdf;User Instance=true"
providerName="System.Data.SqlClient" />
I tried to open the DB again in server explorer and it tells me that the login failed. So I figured I could drop the database and have EF recreate it - but after doing so, I still receive the same error. The whole point is to create a project and deploy to an actual server, but I can't seem to get past these connection issues.
Update: Changing the name of the database in the connection string creates the new database and all is well. However, opening it up in Server Explorer in VS 2010 causes the same problem. Very frustrating.

Why could "update database" be missing in visual studio 2012 when publishing?

I'm trying to deploy an application to my webserver and everything works, except that it does not update the remote database.
With a new demo MVC project you have the option "Update database" when publishing
For some reason that options is not available when I want to publish my own MVC project.
Why is this option not showing?
I am using EF (database first)
I am getting a warning under the connectionstring fields: in order to publish a code first model you must enable EF code first migrations. (although this project should not use code first)
Web Deploy 3.0 and the dbDacFx provider are installed on the server
-- EDIT --
I have now recreated the project, and the update database keeps showing until the first web publish. then it just disappears and shows the warning in order to publish a code first model you must enable EF code first migrations. (although im not using code first, im using database first) Is this a bug in VS or what am I doing wrong?
--FIX--
I got it working by adding another connection string pointing to my database (without EF interference) in web.config: <add name="DeployCode" connectionString="Data Source=(localdb)\Projects;Initial Catalog=xxxxx;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;" providerName="System.Data.SqlClient" />

Using Entity Framework as a Web Part's Data Source in Sharepoint 2010

I have a very simple web part. I have a single grid view, which I am populating using linq to entities (or at least that's what I want to do). The Entity Data Model .edmx file is located in the same project as the web part, and everything looks to be in working order. When I debug the project, it blows up on the entity model constructor with the error message:
The specified named connection is
either not found in the configuration,
not intended to be used with the
EntityClient provider, or not valid.
My connection string in the App.Config is as follows:
<add name="MyDBEntities" connectionString="metadata=res://*/MyDBEntityModel.csdl|res://*/MyDBEntityModel.ssdl|res://*/MyDBEntityModel.msl;provider=System.Data.SqlClient;provider connection string="Data Source=MyServer;Initial Catalog=MyDB;Integrated Security=True;MultipleActiveResultSets=True"" providerName="System.Data.EntityClient" />
The constructor:
public MyDBEntities() : base("name=MyDBEntities", "MyDBEntities")
So, from what I've read elsewhere, my problem is that SharePoint can't see my connection string. Which means, that the App.Config from my project isn't actually getting loaded into SharePoint when I run/debug the project. If that's the case, then how I do set my project up in Visual Studio 2010 to ensure SharePoint picks up the App.Config in addition to the master SharePoint config file. If I have to manually copy the connection string, is there a "best practice" procedure for doing so? Are SharePoint Web Parts combined with the Entity Framework just not ready for prime time?
The SharePoint Tools for Visual Studio 2010 have come along way and will automatically make many of the necessary entries into web.config. Unfortunately, they won't make Entity Framework entries for you. To do this, you'll need to write a feature receiver for your web part project that adds the EF connection string.
The SharePoint API has an object named SPWebConfigModification. You should write a FeatureActivated event that uses this class to make your modification to web.config and then a FeatureDeactivating event that removes the modification.
-Greg
I was wrestling with a the same exception in a SharePoint 2010 WebPart, and I've finally got it working, but here are two important things that I learned along the way.
You must use a farm solution rather than a sandbox solution. The reason for this is, sandbox solutions do not have access to data outside the site collection. A more meaningful exception would have been helpful to solve this quickly, but I was receiving the exception as above.
Your connection string must be in the web.config of the Web Application that you install your WebPart on. It is not added automatically when you install your WebPart, so you must either update the web.config the way Greg lists above, or edit it manually. It sits in C:\inetpub\wwwroot\wss\VirtualDirectories{WebApplicationName}\web.config

Categories

Resources