Reputation: 13
I am building a desktop based application on visual studio 2012. Entity Framework is not working as SaveChanges()
occur it does not give an exception, it seems that data has been entered but after refreshing database no extra row is added in it.
For demo I made 2-Tier Architecture.
My View is......
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
MyDAL dal = new MyDAL();
if (dal.Add(StudentName.Text, Contact.Text))
{
MessageBox.Show("Addition Successfull");
}
else
{
MessageBox.Show("Addition Failed");
}
}
}
My Data Access Layer........
public class MyDAL
{
public bool Add(string name, string contact)
{
using (var db = new Database1Entities())
{
Student s = new Student();
s.StudentName = name;
s.ContactNo = contact;
try
{
db.Students.Add(s);
db.SaveChanges();
return true;
}
catch (Exception ex)
{
return false;
}
}
}
}
And My database Script is.....
CREATE TABLE [dbo].[Student] (
[Student_Id] INT IDENTITY (1, 1) NOT NULL,
[StudentName] VARCHAR (50) NULL,
[ContactNo] VARCHAR (50) NULL,
PRIMARY KEY CLUSTERED ([Student_Id] ASC)
);
My App.Config in View
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
<connectionStrings>
<add name="Database1Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Database1.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
</configuration>
My App.Config in DAL is....
<?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=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
</configSections>
<connectionStrings>
<add name="Database1Entities" connectionString="metadata=res://*/Model1.csdl|res://*/Model1.ssdl|res://*/Model1.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\Database1.mdf;integrated security=True;connect timeout=30;MultipleActiveResultSets=True;App=EntityFramework"" providerName="System.Data.EntityClient" />
</connectionStrings>
<entityFramework>
<defaultConnectionFactory type="System.Data.Entity.Infrastructure.LocalDbConnectionFactory, EntityFramework">
<parameters>
<parameter value="v11.0" />
</parameters>
</defaultConnectionFactory>
</entityFramework>
</configuration>
Upvotes: 0
Views: 429
Reputation: 54618
It is working as intended. This is a simple misunderstanding of the |DataDirectory|
option.
When you compile your solution, the database is being copied to the \Debug\|DataDirectory|
and there the file is being modified.
Most likely you are looking at the file in the solution (visual studio), which is not being modified by the executable in the Debug
directory.
Upvotes: 1