数据库不是由Entity Framework创建的

时间:2015-05-15 17:57:29

标签: asp.net-mvc entity-framework

我正在使用教程创建项目管理应用程序。没有创建数据库。我在每个我能想到的地方都设置了断点,但没有一个被击中。但是,该项目编译。我甚至尝试更改模型以触发数据库创建。

我有:

TaskContext.cs

public class TaskContext : DbContext
{
    public DbSet<TaskDetail> Tasks { get; set; }
    public DbSet<Employee> Employees { get; set; }
    public DbSet<Status> Statuses { get; set; }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        modelBuilder.Conventions.Remove<System.Data.Entity.ModelConfiguration.Conventions.PluralizingTableNameConvention>();
    }  
}

TaskInitializer.cs:

public class TaskInitializer : DropCreateDatabaseIfModelChanges<TaskContext>
{
    protected override void Seed(TaskContext context)
    {
        var employees = new List<Employee>
        {
            new Employee { EmployeeName = "James Dean" },
            new Employee { EmployeeName = "Linda Berry" }
        };
        foreach (var temp in employees)
        {
            context.Employees.Add(temp);
        }
        context.SaveChanges();

        var statuses = new List<Status>
        {
            new Status {StatusName = "Open"},
            new Status {StatusName = "Code Reveiw"}
        };
        foreach (var temp in statuses)
        {
            context.Statuses.Add(temp);
        }
        context.SaveChanges();

        var tasks = new List<TaskDetail>
        {
            new TaskDetail {TaskName="Learn c#", TaskDescription="Learn C# from Video", AssignDate=DateTime.Parse("01/01/2015"), EmployeeId=1, StatusId=2},
            new TaskDetail {TaskName="Learn MVC", TaskDescription="Learn MVC Using Sample Projects", AssignDate=DateTime.Parse("02/02/2015"), EmployeeId=1, StatusId=1}
        };
        foreach (var temp in tasks)
        {
            context.Tasks.Add(temp);
        }

        context.SaveChanges();
        base.Seed(context);
    }
}

我的Web.config有:

<add name="TaskContext" connectionString="Data Source=(LocalDb)\v11.0;AttachDbFilename=|DataDirectory|\TaskProject.mdf;Initial Catalog=aspnet-TaskProject-20150515093801;Integrated Security=True"
  providerName="System.Data.SqlClient" />

我的global.asax有:

Database.SetInitializer<TaskContext>(new TaskInitializer());

Employee.cs:

public class Employee
{
    public int EmployeeId { get; set; }
    public string EmployeeName { get; set; }
}

Status.cs:

public class Status
{
    public int StatusId { get; set; }
    public string StatusName { get; set; }
}

TaskDetail.cs

public class TaskDetail
{
    public int TaskDetailId { get; set; }
    public string TaskName { get; set; }
    public string TaskDescription { get; set; }
    public DateTime AssignDate { get; set; }
    public int EmployeeId { get; set; }
    public int StatusId { get; set; }
    public virtual Status status { get; set; }
    public virtual Employee employee { get; set; }
}

我错过了什么?

0 个答案:

没有答案