简体   繁体   中英

Entity Framework tables not created (Code first)

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!

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

(localdb)\\MSSQLLocalDB

.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM