简体   繁体   中英

Connection string C# Entity Framework SQL Server Express

I am using C# WPF EF6.0 and SQL Server Express for final program.

I have created a setup file with Advanced installer that creates database via script and SQL Server Express in

C:\Program Files\Microsoft SQL Server\MSSQL11.SQLEXPRESS\MSSQL\DATA

and I have changed my string connection from

<add name="MyDBEntities" 
     connectionString="metadata=res://*/MyDBModel.csdl|res://*/MyDBModel.ssdl|res://*/MyDBModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\MyDB.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework&quot;" 
     providerName="System.Data.EntityClient" />

To this

<add name="MyDBEntities" 
     connectionString="metadata=res://*/MyDBModel.csdl|
                       res://*/MyDBModel.ssdl|
                       res://*/MyDBModel.msl;
                       provider=System.Data.SqlClient;
                       provider connection string=&quot;
                             Data source=.\SQLExpress;
                             integrated security=True;
                             MultipleActiveResultSets=True;
                             Initial Catalog=MyDB;
                             App=EntityFramework&quot;" 
     providerName="System.Data.EntityClient" />  

I have installed .net Framework 4.5 and SQL Server 2012 sp1 v11.0.300 that I have used in Visual Studio 2013

When I install my program, everything works, but when I run my program it stopped working at first launch. I think the connection string has a problem. If I click debug in Visual Studio, I see this error

An unhandled exception of type 'System.InvalidOperationException' occurred in EntityFramework.dll.Additional information: No connection string named 'MyDBEntities' could be found in the application config file.

The reason is that you have not attached the database files to the local database engine. try to use the appropriate SQL Server methods which are available. Your application (after installation) can not find the database in the local DB engine.

Finally I couldn't create a setup file with Advanced Installer . But I created it by Install Shield . My problem was insert or delete or edit from database. I installed my program in other drive and my problem solved. The reason was a joke that program files in C is readonly .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM