0

Title says most of it.

I've added this to my Web.Debug.config file:

<connectionStrings>
  <add name="Sharpix"
    connectionString="Server=localhost; Database=sharpix; Uid=root; Pwd=;"
    providerName="MySql.Data.MySqlClient"/>
</connectionStrings>

I'm using the MySQL driver, which I found here. Presumably if I installed it incorrectly, or there was an error with it, I would get an exception, but as far as I can tell, it isn't being used, and there is no attempt at using it.

However, I can add data to my site just fine:

So where's it storing my data, and how do I get it to use my MySQL database?

If relevant, my model looks like this:

public class ImageModel
{
    public int Id { get; set; }
    public string FileName { get; set; }
}

}

And my context looks like this:

public class SharpixContext : DbContext
{
    public DbSet<ImageModel> ImageModels { get; set; }
}

I'm pretty much a beginner when it comes to the ASP.NET MVC, but I'm very familiar with the concept of MVC in general, and the Django framework.

Community
  • 1
  • 1
mpen
  • 272,448
  • 266
  • 850
  • 1,236

1 Answers1

1

You need to give the connection string name in the constructor. Otherwise EF will try to create a database named namespace.SharpixContext in your local server.

public class SharpixContext : DbContext
{
    public SharpixContext()
     : base("Sharpix")
    {}

    public DbSet<ImageModel> ImageModels { get; set; }
}
Eranga
  • 32,181
  • 5
  • 97
  • 96
  • Hrm...that didnt' seem to do it. Data I had in there before went missing, but the new stuff I'm adding doesn't appear to be saving in that DB. – mpen Aug 30 '12 at 04:49
  • @Mark strange. Are you running on `Debug` mode? Does your `Web.Config` file has another connection with the same name? – Eranga Aug 30 '12 at 04:53
  • Yes, I'm running in debug mode, no, no other connections. It was a fresh project. I pretty much just added the one model and then used the generator to create the controller, the context and the views. – mpen Aug 30 '12 at 05:06
  • @Mark Check [this article](http://msdn.microsoft.com/en-US/data/jj592674) on msdn for setting up connections. – Eranga Aug 30 '12 at 06:26