Reputation: 713
I try to create an Equation
entity and to add it to SQL Server db, i do not get any errors in my code but i do not see any changes in the db.
I check the db in the server explorer in visual studio, and there are not added values. The db were added to the project when i was creating the ef data model for it in my project so the visual studio made a copy of the db in my project. But i don't know why it created two copies, in the project directory and /bind/Debug directory.
Entity Class:
public partial class Equation
{
public Equation()
{
this.Results = new HashSet<Result>();
}
public int id { get; set; }
public double a { get; set; }
public double b { get; set; }
public double c { get; set; }
public virtual ICollection<Result> Results { get; set; }
}
id is primary key and has identity(1,1)
My code:
private EquationDBEntities dbcontext = new EquationDBEntities();
Equation eq = new Equation()
{
a = 1, b = 2, c = 3
};
dbcontext.Equations.Add(eq);
dbcontext.SaveChanges();
DbContext:
public partial class EquationDBEntities : DbContext
{
public EquationDBEntities()
: base("name=EquationDBEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public virtual DbSet<Equation> Equations { get; set; }
public virtual DbSet<Result> Results { get; set; }
conectionString:
<connectionStrings>
<add name="EquationDBEntities" connectionString="metadata=res://*/EquationDBModel.csdl|res://*/EquationDBModel.ssdl|res://*/EquationDBModel.msl;
provider=System.Data.SqlClient;
provider connection string="
data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\EquationDB.mdf;
integrated security=True;connect timeout=30;
MultipleActiveResultSets=True;App=EntityFramework"
" providerName="System.Data.EntityClient" />
</connectionStrings>
App.Config of project with ef model(i reference it from my current project)
<?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>
<connectionStrings>
<add name="EquationDBEntities" connectionString="metadata=res://*/EquationDBModel.csdl|res://*/EquationDBModel.ssdl|res://*/EquationDBModel.msl;provider=System.Data.SqlClient;provider connection string="data source=(LocalDB)\v11.0;attachdbfilename=|DataDirectory|\EquationDB.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>
<providers>
<provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
</providers>
</entityFramework>
</configuration>
Any ideas ?
Upvotes: 0
Views: 1291
Reputation: 713
It happens because |DataDirectory| substitution string means that the database used by your application is located in the subfolder BIN\DEBUG. But when you add a database to your project it creates two copies, one in project folder and another in BIN\DEBUG and you expect that the changes will be made in database from the project folder that's why you don't see anything in server explorer, because it probably has a wrong connection.
Upvotes: 2
Reputation: 162
try opening the .mdf file from the bin/Debug folder and see if it has your data
Upvotes: 0
Reputation: 3640
Wrap the following in a try... Catch and set a breakpoint to see if there are any exceptions raised:
try
{
dbcontext.Equations.Add(eq);
dbcontext.SaveChanges();
}
catch (Exception ex)
{
// Set a breakpoint and see if any problems are being raised
}
Upvotes: 0
Reputation: 4987
I don't know the context of your context (no pun intended) so try to set its state to added explicitly before calling SaveChanges
eq.EntityState = System.Data.EntityState.Added;
Although, if you have tracking enabled, it should know its been added.
Upvotes: 0