未创建实体框架表(代码优先)

时间:2017-07-04 08:40:29

标签: c# entity-framework ef-code-first

我总是在使用DBfirst之后第一次尝试使用代码,但由于某些原因,当我运行我的项目时它不会创建我的表,即使阅读了很多stackoverflow帖子后我仍然无能为力。

我有以下代码:

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>

我用一个空的控制台应用程序开始。任何帮助都感激不尽。振作!

1 个答案:

答案 0 :(得分:2)

检查您是否在SQL Management Studio中查看正确的数据源。在连接字符串中,它是:

  

(的LocalDB)\ MSSQLLocalDB