I create new ASP.NET MVC4 project with account controller uses forms authentification.
Then I run project and register new user for creating database file.
Then I create some tables in new mdf database file and I use ADO.NET Entity Data Model for this new tables instead of membership's tables.
After creating model.edmx there are two connection strings in web.config file.
name="Entities" connectionString="metadata=res://*/Models.Database.LAModel.csdl|res://*/Models.Database.LAModel.ssdl|res://*/Models.Database.LAModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDb)\v11.0;attachdbfilename=|DataDirectory|\aspnet-LAWebProject-db.mdf;initial catalog=aspnet-LAWebProject-db;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient"
name="DefaultConnection" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-LAWebProject-db;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-LAWebProject-db.mdf" providerName="System.Data.SqlClient"
The project debugs few times then crashed with following exception:
Can not connect to the database server SQL Server.
Please help me to resolve it...
Try removing that "\" from your connection strings:
|DataDirectory|\aspnet-LAWebProject-db.mdf;
becomes
|DataDirectory|aspnet-LAWebProject-db.mdf;
If it doesn't work, check that the db file aspnet-LAWebProject-db.mdf exists in the App_Data folder.
Make sure your LocalDb instance is running too:
LocalDB: Where is My Database?
Related
I have an error:
An exception of type 'System.Data.Entity.Infrastructure.UnintentionalCodeFirstException' occurred in DataAccess.dll but was not handled in user code
Additional information: The context is being used in Code First mode with code that was generated from an EDMX file for either Database First or Model First development. This will not work correctly. To fix this problem do not remove the line of code that throws this exception. If you wish to use Database First or Model First, then make sure that the Entity Framework connection string is included in the app.config or web.config of the start-up project. If you are creating your own DbConnection, then make sure that it is an EntityConnection and not some other type of DbConnection, and that you pass it to one of the base DbContext constructors that take a DbConnection.
In DataAccess project I have an EF 6 with App.Config file with string:
<connectionStrings> <add name="CVJobOnlineEntities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model 1.msl;provider=System.Data.SqlClient;provider connection string="data source=STEFAN-PC\SQLEXPRESS;initial catalog=CVJobOnline;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" /> </connectionStrings>
and in my second project, which is the main Start-Up project I have in WebConfig:
<connectionStrings>
<add name="CVJobOnlineEntities"
providerName="System.Data.SqlClient"
connectionString="Server=.\SQLEXPRESS;Database=CVJobOnline;Integrated Security=True;"/>
So, obviously I am mixing EDMX and CodeFirst conn strings, but, I need it CodeFirst because of my Identity tables which I was incorporate in my SQL SERVER DB.
Also in my DbContext, I recalled base to use FirstCode (Model1.Context.cs):
public partial class CVJobOnlineEntities : DbContext
{
public CVJobOnlineEntities()
: base("name=CVJobOnlineEntities")
{
}
You must specify your connection string only once at the entry point of your application. Your DataAccess project does not need a connection string if it is not executable. Cut & paste the connection string from your DataAccess project to the web configuration file of your application entry point, overwriting the old one.
The problem was not exactly mixing two types of connection strings, since the one from DataAccess was never read by the Entity Framework. The one provided at your entry point config was just wrong in your scenario, because you are using model-first and not code-first.
I have an old winforms project which uses localDB and ADO.NET SqlConnection. Now I have created a new database (not local), which I use Entity Framework to connect to. When I use the new EF-database on my SqlConnection I get the error that says SqlConnection does not support the metadata from my new connectionstring.
Is it correct that SqlConnection needs a localDB? How can I do so that the SqlConnection can connect to my new database?
//Martin
An Entity framework connection string contains a meta data element that points to the location of the entity framework model:
<connectionStrings>
<add name="AdventureWorksEntities"
connectionString="metadata=.\AdventureWorks.csdl|.\AdventureWorks.ssdl|.\AdventureWorks.msl;
provider=System.Data.SqlClient;provider connection string='Data Source=localhost;
Initial Catalog=AdventureWorks;Integrated Security=True;Connection Timeout=60;
multipleactiveresultsets=true'" providerName="System.Data.EntityClient" />
</connectionStrings>
A Sql connection string looks something like this:
Server=myServerAddress;Database=myDataBase;Trusted_Connection=True;
If your application uses an SQLConnection then you must connect to the database with a connection string that has the structure of a SQLConnection string.
I am using VS2012 with .NET 4.5. I am learning Entity Framework and I have a problem. I added ADO.NET Entity Data Model to my project and generated my entities using the wizard.
The wizard added a connection string into app.config file :
<add name="MalariaEntities" connectionString="metadata=res://*/MalariaEntities.csdl|res://*/MalariaEntities.ssdl|res://*/MalariaEntities.msl;provider=System.Data.SqlClient;provider connection string="data source=OFIR-PC;initial catalog=Malaria;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
In my project I am seeing all the entities and it seems fine but when I try to do any database action I get the following error:
No connection string named 'MalariaEntities' could be found in the application config file.
For example, I tried to insert simple row:
using (MalariaEntities DB_Context = new MalariaEntities())
{
MapsMainCategoriesDsc a = new MapsMainCategoriesDsc();
a.Category = "aa";
DB_Context.MapsMainCategoriesDsc.Add(a);
DB_Context.SaveChanges();
}
When the debugger reach to the last line the exception is raised. What can be the problem?
The wizard added a connection string so why my project cannot use it?
Thanks
I am programming with EF5-code first and want to use LocalDb. How can I change the default location of LocalDb database file? Default location is %USERPROFILE% directory according to http://blogs.msdn.com/b/sqlexpress/archive/2011/10/28/localdb-where-is-my-database.aspx
Previously I was using SqlCe DbConnectionFactory which accepts a parameter for database path/name. If I use AttachDbFileName parameter then it raises an exception if database doesn't exist (it's supposed to create it!).
I got this working thanks to this post. The answer is simple. Basically I added
<connectionStrings>
<add name="DataModel.Context" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=database;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\database.mdf" providerName="System.Data.SqlClient" />
to the configuration section of app.config and it magically replaced the "database" with my actuall database (DbContext) name. EF code first also works perfectly to create the database if it doesn't exist.
For sample I'm using this connection string :
<connectionStrings>
<add name="PicturesDatabase"
connectionString=" Server=.;
Database=SomeprojectDatabase;
Trusted_Connection=True;"
providerName="System.Data.SqlClient"/>
<../>
And then I use it in Application_Start() :
Database.DefaultConnectionFactory =
new SqlConnectionFactory(ConfigurationManager.
ConnectionStrings["PicturesDatabase"].ConnectionString);
And in my database I get this really strange new Database :
MyAppNamespace.Models.PicturesCatalog
with two tables dbo.EdmMetadata and dbo.Pictures
Tables are fine, but why doesn't it create a new database named PicturesDatabase (as in connection string) with those tables ?
I've tried dropping this table few times, I did try creating PicturesDatabase and using it... but it still generates this MyAppNamespace.Models.PicturesCatalog. What is the problem with it ? And how do I fix it ?
System.Data.Entity.Infrastructure.SqlConnectionFactory is not meant to use in this way. This connection factory includes a constructor that allows us to override pieces of the final connection sting, such as username, password and server and not the whole thing.
For example, we can change the Database Server like this:
Database.DefaultConnectionFactory =
new SqlConnectionFactory("Server=MyDatabaseServer");
What you try to do here can be easily accomplished by providing a connection string in your app.config that matches the name of your DbContext - I assume it's PicturesCatalog:
<connectionStrings>
<add name="PicturesCatalog" connectionString="data source=.;
Initial Catalog=PicturesDatabase; Trusted_Connection=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>