0

I'm trying to make a simple project in ASP.NET, 'code first'.

So I made a class EFDbContext:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;

namespace MVCRubenWouters.Models
{
  public class EFDbContext: DbContext
  {
    public EFDbContext() : base("name=rubenwoutersdbCF")
    {
      Database.SetInitializer<EFDbContext>(new EFDbInitializer());
    }
      public DbSet<Types> Types { get; set; }
  }
}

And added a connectionstring in 'web.config'

<connectionStrings>
    <add name="rubenwoutersdbCF" connectionString="Data Source=.\SQLSERVER2012;Initial Catalog=rubenwoutersdbCF;Integrated Security=True;MultipleActiveResultSets=true"
      providerName="System.Data.SqlClient" />
  </connectionStrings>

Then I created a class "EFDbInitializer to add something to the database:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Entity;

namespace MVCRubenWouters.Models
{
  public class EFDbInitializer: DropCreateDatabaseAlways<EFDbContext>
  {
    protected override void Seed(EFDbContext context)
    {
      Types t1 = new Types()
      {
        PK_TypeNr = 1,
        TypeKeuken = "Belgisch",
        TypeZaak = "Restaurant",
        Vegetarisch = false
      };

      context.Types.Add(t1);
      context.SaveChanges();
    }
  }
}

When I run the project and go to the SQL server management studio (and refresh), no new database is there..

Am I doing something wrong here? :/

RW24
  • 624
  • 2
  • 10
  • 19
  • Did you connect your Mgmt Studio to the `.\SQLSERVER2012` instance?? – marc_s Dec 27 '14 at 15:19
  • Try making a call to the entity framework, if there is a problem, you should get an Exception about it. Try to call this: `new EFDbContext().Types.ToList();` – Dan Dec 27 '14 at 16:20
  • @Dan Where should I put that? – RW24 Dec 27 '14 at 16:22
  • @RW24 Anywhere will do, Page_Load method, whatever works. – Dan Dec 27 '14 at 16:23
  • @Dan 'An unhandled exception of type 'System.StackOverflowException' occurred in MVCRubenWouters.dll' – RW24 Dec 27 '14 at 16:27
  • @RW24 So where is that error originating from? You really need to provide more information for us to help you. – Dan Dec 27 '14 at 16:33
  • I'm sorry. That error originated from the code you gave me: `new EFDbContext().Types.ToList();` I pasted the code in my EFDbContext class. (When I ran the project and browsed to localhost/Types, the error occured) – RW24 Dec 27 '14 at 16:36
  • @RW24 You cannot put that code in the EFDbContext class, it will become a recursive call that result in the Stackoverflow exception you are getting. Put it in the `Main` program that you have. – Dan Dec 27 '14 at 16:39
  • How about connection string, set Integrated Security to SSPI. – Aria Dec 27 '14 at 18:02

2 Answers2

0

I would suggest you create your database in SQL server, build your tables, then fill them using your code.

  • 1
    I thought that Code first meant that you create your database via code? (It's a school assignement, so it needs to be like that :/) – RW24 Dec 27 '14 at 15:14
  • @user1418016 The question is related to code first, your answer doesn't make any sense. – Calvedos Dec 27 '14 at 16:29
0

The way that runs on my system is force the database in Application_Start()

try this:

Database.SetInitializer(new CreateDatabaseIfNotExists<EFDbContext>());
        var context = new EFDbContext("Data Source=(local);Integrated Security=SSPI;Initial Catalog=myDB;");
        context.Database.Initialize(true);

I think the seed method is never called, To ensure set a brakpoint

You can develop your custom intializer something like below link:

Seed in entity framework and then call it in Application_Start()

 Database.SetInitializer<EFDbContext>(new EFDbInitializer());
Community
  • 1
  • 1
Aria
  • 3,724
  • 1
  • 20
  • 51