2014-03-05 56 views
3

這裏是我的表結構和上下文:如何生成具有多個集合的表的控制器?

using Microsoft.AspNet.Identity; 
using Microsoft.AspNet.Identity.EntityFramework; 
using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Data.Entity; 
using System.Linq; 
using System.Web; 

namespace AspnetIdentitySample.Models 
{ 
    public class Employee : IdentityUser 
    { 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public virtual ICollection<MyTask> MyTasks { get; set; } 
     public virtual ICollection<Customer> Customers { get; set; } 

    } 
    public class MyTask 
    { 
     public int MyTaskId { get; set; } 
     public string Description { get; set; } 
     public DateTime OldDate { get; set; } 
     public DateTime NewDate { get; set; } 
     public bool IsDone { get; set; } 
     public virtual Employee User { get; set; } 
    } 

    public class Customer 
    { 
     public int CustomerId { get; set; } 
     public string FirstName { get; set; } 
     public string LastName { get; set; } 
     public virtual Employee Employee { get; set; } 
    } 



    public class MyDbContext : IdentityDbContext<Employee> 
    { 
     public MyDbContext() 
      : base("DefaultConnection") 
     { 
     } 

     protected override void OnModelCreating(DbModelBuilder modelBuilder) 
     { 
      base.OnModelCreating(modelBuilder); 

      // Change the admin of the table to be Users instead of AspNetUsers 
      modelBuilder.Entity<IdentityUser>() 
       .ToTable("Employee"); 
      modelBuilder.Entity<Employee>() 
       .ToTable("Employee"); 
     } 

     public DbSet<MyTask> MyTasks { get; set; } 
     public DbSet<Customer> Customers { get; set; } 
     public DbSet<Employee> Employees { get; set; } 

    } 


} 

當我試圖創建一個使用實體框架的CRUD控制器,它顯示了以下錯誤消息。任何幫助,將不勝感激。 :)

enter image description here

回答

4

OP增加了更多的細節和代碼。原來的答案是過時的,錯誤的

/編輯:

你正試圖把2款同一個表:

modelBuilder.Entity<IdentityUser>() 
      .ToTable("Employee"); 
modelBuilder.Entity<Employee>() 
      .ToTable("Employee"); 

,另外您嘗試添加public DbSet<Employee> Employees { get; set; }這已經應該在數據庫作爲您的上下文類定義所定義的標識表。 我沒試過重命名aspnetusers表,但我會嘗試刪除下面幾行:

modelBuilder.Entity<IdentityUser>() 
      .ToTable("Employee"); 

public DbSet<Employee> Employees { get; set; } 

這應該做的伎倆。

+0

我不在我的應用程序中的用戶表,但。 –

+1

請將您的整個Dbcontext課程發佈在您的問題中。 – Marco

+0

增加了...... :) –

相關問題