0

I am using windowform application to insert data into .mdf file which is in AppData folder and .edmx file in project folder.

Insertion Code Here:

   Customer newCustomer = new Customer();
    newCustomer.CustomerName = txtCustomerName.Text;
    newCustomer.ShopName = txtShopName.Text;
    newCustomer.PersonalPhone = txtPersonalPhoneNo.Text;
    newCustomer.ShopPhone= txtShopPhone.Text;
    newCustomer.Address = txtShopAddress.Text;
    newCustomer.Province = cbProvince.SelectedItem.ToString();
    using (NobelDBEntities1 entity = new NobelDBEntities1())
     {
       entity.Customers.Add(newCustomer);
       entity.SaveChanges();
     }

App.config file

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <configSections>
    <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
    <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
  </configSections>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
  </startup>
  <connectionStrings>
    <add name="NobelDBEntities1" connectionString="metadata=res://*/NobelModel.csdl|res://*/NobelModel.ssdl|res://*/NobelModel.msl;provider=System.Data.SqlClient;provider connection string=&quot;data source=(LocalDB)\MSSQLLocalDB;attachdbfilename=|DataDirectory|\AppData\NobelDB.mdf;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;"
      providerName="System.Data.EntityClient" />
    <add name="NobelFactoryOrderingSystem.Properties.Settings.NobelDBConnectionString"
      connectionString="Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\AppData\NobelDB.mdf;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
      <parameters>
        <parameter value="mssqllocaldb" />
      </parameters>
    </defaultConnectionFactory>
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

Note:

i tried all other example on stack overflow but didn't get any appropriate solution any help will be appreciated!

weje
  • 69
  • 1
  • 7
  • Right click on the NobelDB.mdf file and check the property Copy to Output Directory. What is its value? – Steve May 23 '18 at 13:56
  • @Steve Copy to Output Directory property value is Copy always – weje May 23 '18 at 14:45
  • Look at my answer here then https://stackoverflow.com/questions/17147249/why-saving-changes-to-a-database-fails/17147460#17147460 – Steve May 23 '18 at 14:53
  • @Steve i looked at your answer and modify NobelDB property from copy always to Do Not copy after thet it through **exception i.e. The underlying provider failed on Open."** – weje May 23 '18 at 16:24
  • and when i set property value to copy if newer than same thing happened it didn't store record into real file – weje May 23 '18 at 16:28
  • It seems like you don't have permissions to open that file. This is weird. Do you have an InnerException with more info about the problem? – Steve May 23 '18 at 16:35
  • @Steve no i didn't get any other exception – weje May 23 '18 at 17:55
  • any thing that i should try to solve it so please help on this – weje May 23 '18 at 17:56
  • i am using window authentication for sqlserver – weje May 23 '18 at 18:10
  • Try to add _Database=NobelDB;_ to your connectionstring – Steve May 23 '18 at 19:48
  • @Steve i am still facing that issue i tried everything but didn't succeeded there are two connection in connection string that are – weje May 29 '18 at 13:16
  • second one inside connection string tag is – weje May 29 '18 at 13:19

0 Answers0