2

So I have SQLServerExpress 2008 R2 running, and Visual Studio 2010. I believe I have a SQL Server instance running (fig. 1). I have been informed that if I don't specify a connection string in my program, EF will create the database on a local SQL Server instance. It isnt. . .I'm getting the error

My Code is as follows:

In global.asax.cx:

protected void Application_Start()
{
    Database.SetInitializer(new DatabaseInit());

    AreaRegistration.RegisterAllAreas();

    RegisterGlobalFilters(GlobalFilters.Filters);
    RegisterRoutes(RouteTable.Routes);
}

Horse.cs:

public class Horse
{
    public int HorseID { get; set; }
    public string Name { get; set; }

    public virtual Participant Participant { get; set; }
}

Participant.cs:

public class Participant
{
    public int ParticipantID { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    [Required]
    public Horse Horse { get; set; }
}

MelbourneCupDbContext:

public class MelbourneCupDbContext : DbContext
{
    public DbSet<Horse> Horses;
    public DbSet<Participant> Participants;
}

DatabaseInit:

public class DatabaseInit : DropCreateDatabaseAlways<MelbourneCupDbContext>
{
    protected override void Seed(MelbourneCupDbContext context)
    {
        var Horses = new List<Horse>
        {
            new Horse{Name="Americain"},
            new Horse{Name="Jukebox Jury"},
            new Horse{Name="Dunaden"}
            ....
        };

        foreach (Horse h in Horses)
            context.Horses.Add(h);
        context.SaveChanges();

    }
}

Finally, when I try to use the database (SignUpController.cs):

private MelbourneCupDbContext dbContext = new MelbourneCupDbContext();

[HttpGet]
public ActionResult Index()
{
    IEnumerable<Horse> allHorsesList = dbContext.Horses.ToList();
    return View(allHorsesList);
}

I'm getting an error when I try to call the ToList that the source cannot be null.

HALP

fig 1 Server Instance Running

JohnW
  • 345
  • 1
  • 7
  • 15

3 Answers3

1

"If SQL Express is installed (included in Visual Studio 2010) then the database is created on your local SQL Express instance (.\SQLEXPRESS). If SQL Express is not installed then Code First will try and use LocalDb ((localdb)\v11.0) - LocalDb is included with Visual Studio 2012"

For more information : http://msdn.microsoft.com/en-us/data/jj591621.aspx

StackTrace
  • 9,190
  • 36
  • 114
  • 202
  • 1
    This doesn't explain why I'm getting the null source? It should be being created somewhere right? – JohnW May 24 '13 at 09:22
1

I think your are talking about EF code-first this is a quote from a good article

By convention DbContext has created a database for you.

If a local SQL Express instance is available (installed by default with Visual Studio 2010) then Code First has created the database on that instance If SQL Express isn’t available then Code First will try and use LocalDb (installed by default with Visual Studio 2012) The database is named after the fully qualified name of the derived context, in our case that is CodeFirstNewDatabaseSample.BloggingContext These are just the default conventions and there are various ways to change the database that Code First uses, more information is available in the How DbContext Discovers the Model and Database Connection topic.

But you can always chim in to change the settings...

Here is the full article http://msdn.microsoft.com/en-us/data/jj193542.aspx

and here

Other Ways to Change the Database

There are a number of other ways to specify which database should be connected to. We’ll cover these in more detail in a separate post in the future.

App.config Connection String Create a connection string in the App.Config file with the same name as your context. DbConnection There is a constructor on DbContext that accepts a DbConnection. Replace the Default Convention The convention used to locate a database based on the context name is an AppDomain wide setting that you can change via the static property System.Data.Entity.Database.DbDatabase.DefaultConnectionFactory.

from this article: http://blogs.msdn.com/b/adonet/archive/2010/12/14/ef-feature-ctp5-code-first-walkthrough.aspx

Did you saw this one? What is the connection string for localdb for version 11

Community
  • 1
  • 1
silverfighter
  • 6,762
  • 10
  • 46
  • 73
  • Correct. I have read through these articles, and I'm expecting Visual Studio to create the database on my instance of SQL Server (fig. 1). However it doesn't seem to be doing that / creating a database at all (on the local sql server instance or LocalDb). Thus the null source error. – JohnW May 24 '13 at 09:34
  • did you try to specify the connectionstring using (var db = new Context("connectionString"))? – silverfighter May 24 '13 at 09:40
  • "buttom" - best typo ever. – Michael12345 Jul 18 '17 at 01:20
0

Take a look at the App_Data folder. It's the default location.

"Contains application data files including .mdf database files, XML files, and other data store files. The App_Data folder is used by ASP.NET to store an application's local database, such as the database for maintaining membership and role information."

http://msdn.microsoft.com/en-us/library/ex526337(v=vs.100).aspx

Oscar
  • 13,594
  • 8
  • 47
  • 75