Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

EntityFramework CodeFirst Database doesn't update

One day ago I started using Entity Framework CodeFirst in simple Windows Forms project (C#). I created two models:

[Table("SavesSet")]
public partial class Saves
{
    public Saves()
    {
        this.SkillsSet = new HashSet<Skills>();
    }

    [Key]
    public int SaveID { get; set; }

    public string Player { get; set; }
    public int Age { get; set; }
    public int Money { get; set; }

    public virtual ICollection<Skills> SkillsSet { get; set; }
}

[Table("SkillsSet")]
public partial class Skills
{
    [Key]
    public int SkillID { get; set; }

    public string Name { get; set; }
    public int Value { get; set; }
    public int SavesSaveID { get; set; }

    public virtual Saves SavesSet { get; set; }
}

Then I added one row into my database using Visual Studio Database Explorer (local database using SqlServerCe 3.5):

SaveID = 1

Player = "TEST"

Age = 1

Money = 1

I also created form with ListView and wrote some code:

private SavesContext db = new SavesContext();
foreach (Saves s in db.SavesSet.ToList())
{
    ListViewItem l = new ListViewItem();
    l.Name = s.SaveID.ToString();
    l.Text = s.Player;
    ListViewSaves.Items.Add(l);
}

But when I started program debugging ListView was empty. I set breakpoint, viewed local variables and saw that SavesSet count was 0.

I added one row via code:

Saves s = new Saves();
s.Player = "TestName";
s.Age = 5110;
s.Money = 200;
db.SavesSet.Add(s);
db.SaveChanges();

And ListView displayed this one. But in my database there was nothing (and its size didn't change). I restarted Visual Studio - and my trouble was still actual: ListView shows item, but database was empty. I checked ConnectionString in App.Config and in VS Database Explorer - they were equal.

So, my question is: How can I explore my real database and where does it stores?

Update.

My DbContext:

    public SavesContext()
        : base("Saves")
    {
    }

    public DbSet<Saves> SavesSet { get; set; }
    public DbSet<Skills> SkillsSet { get; set; }

And my App.Config:

<?xml version="1.0" encoding="utf-8"?>
    <configuration>
        <configSections>
        </configSections>
        <connectionStrings>
            <add name="TextSim.Properties.Settings.Saves" connectionString="Data          
            Source=C:\Documents and Settings\My\My Documents\visual studio       
            2010\Projects\TextSim\TextSim\Saves.sdf"
            providerName="System.Data.SqlServerCe.3.5" />
        </connectionStrings>
     </configuration>
like image 975
ahawkthomas Avatar asked Nov 13 '22 21:11

ahawkthomas


1 Answers

Entity Framework uses something called Convention over configuration, which means that if you don't supply values for certain things, it uses default values.

If you do not specify your connection string, it will use a default connection string derived from your EF's Namespace and DbContext name. If this is the case, then it would create a new (blank) database using the default system and not use the one you may have configured in Web.Config.

Thus, when you save things, it does save, just to the wrong database.

like image 92
Erik Funkenbusch Avatar answered Nov 15 '22 10:11

Erik Funkenbusch