0

I tried to use code first for the first time after always having used DBfirst but for some reason it does not create my tables when I run my project and I am clueless what I am missing even after reading many stackoverflow posts.

I have the following code:

Class.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EdulySoft.Models
{
    public partial class Class
    {
        public int Id { get; set; }
        public int ClassRoomId { get; set; }
        public string ClassName { get; set; }
        public int MaxStudents { get; set; }

        public virtual Classroom classrooms { get; set; }
    }
}

classroom.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EdulySoft.Models
{
    public partial class Classroom
    {
        public Classroom()
        {
            this.classes = new HashSet<Class>();
        }
        public int Id { get; set; }
        public string ClassRoomName { get; set; }

        public ICollection<Class> classes { get; set; }
    }
}

SchoolContext

using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EdulySoft.Models
{
    public class schoolContext : DbContext
    {

        public schoolContext() : base()
        {
        }

        public DbSet<Class> Classes { get; set; }
        public DbSet<Classroom> Classrooms { get; set; }
    }
}

Program.cs

using EdulySoft.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EdulySoft
{
    class Program
    {
        static void Main(string[] args)
        {

            using (var ctx = new schoolContext())
            {
                Class stud = new Class() { Id = 1, ClassName = "Loquat", MaxStudents = 12, ClassRoomId = 1 };

                ctx.Classes.Add(stud);
                ctx.SaveChanges();
            }
        }
    }
}

App.config

  <?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="EdulySoft.Properties.Settings.schoolContext"
      connectionString="Data Source=(localdb)\MSSQLLocalDB;Initial Catalog=Eduly;Integrated Security=True"
      providerName="System.Data.SqlClient" />
  </connectionStrings>
  <startup>
    <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2" />
  </startup>
  <entityFramework>
    <defaultConnectionFactory type="System.Data.Entity.Infrastructure.SqlConnectionFactory, EntityFramework" />
    <providers>
      <provider invariantName="System.Data.SqlClient" type="System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer" />
    </providers>
  </entityFramework>
</configuration>

I used an empty console app to start with. Any help will be much appreciated. Cheer!

5
  • 1
    Did you create a migration and updated the database? Commented Jul 4, 2017 at 8:41
  • I haven't created a migration yet, is that necessary for creating the database? Commented Jul 4, 2017 at 8:43
  • 1
    Yes. After your changes you need to go to the Package manager console and type "Add-Migration your-migration-name". You will see the code of the created migration and then you can type "update-database" in the console. This should update your database. However make sure you have your EF-Project selected under "Default project" in the console. Commented Jul 4, 2017 at 8:45
  • Your connection string name in app config is EdulySoft.Properties.Settings.schoolConnectionString which should be schoolContext Commented Jul 4, 2017 at 8:45
  • I made both changes but still nothing gets created. The migrations says that the database ran and even did the seed method. but still I got no tables. Commented Jul 4, 2017 at 8:54

1 Answer 1

2

Check if you are looking at the correct data source in SQL Management Studio. In the connection string it is:

(localdb)\MSSQLLocalDB

.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes I was in (localdb)\MSSQLLocalDB, but for some reason it created it onder \SQLEXPRESS. I am clueless why it did that since I specified (localdb)\MSSQLLocalDB. Anyways thanks for letting me check it as I now found my database. Cheers

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.