2012-12-08 51 views
4

我一直想在我的項目添加一個控制器,但我一直得到錯誤ASP.NET MVC3/VS2010錯誤:具有相同鍵的項已被添加

An item with the same key has already been added. 

而這樣做。我仍然是這個初學者,所以我可能沒有注意到一些東西,但我沒有看到我的模型中有任何重複的鍵。這是我的數據庫的圖,以獲得我想要的一般想法要做到:

Database Diagram

我使用ApplicationsModel ClassApplicationServices作爲data Context Class嘗試創建我的ApplicationController並得到錯誤

An item with the same key has already been added. 

任何想法我可能做錯了什麼?

我創建的模型如下:

Entity.cs:

using Microsoft.VisualBasic; 
using System.Data.Entity; 
using System.Data.Entity.ModelConfiguration.Conventions; 
using System.ComponentModel.DataAnnotations; 

namespace PostGraduate.Models 
{ 
    public class ApplicationServices : DbContext 
    { 
     public DbSet<Application.Users> Users { get; set; } 
     public DbSet<Application.Addresses> Addresses { get; set; } 
     public DbSet<Application.Applications> Applications { get; set; } 
     public DbSet<Application.ForeignLanguages> ForeignLanguages { get; set; } 
     public DbSet<Application.Gmat> Gmat { get; set; } 
     public DbSet<Application.PostGradStudies> PostGradStudies { get; set; } 
     public DbSet<Application.PreGradStudies> PreGradStudies { get; set; } 
     public DbSet<Application.Schoolarships> Schoolarships { get; set; } 

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

      modelBuilder.Entity<Application.Users>().HasKey(a => a.UserId); 
      modelBuilder.Entity<Application.Addresses>().HasKey(a => a.Addresses_Id); 
      modelBuilder.Entity<Application.Applications>().HasKey(a => a.Applications_Id); 
      modelBuilder.Entity<Application.Applications>().Property(a => a.Applications_Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 
      modelBuilder.Entity<Application.ForeignLanguages>().HasKey(a => a.ForeignLanguages_Id); 
      modelBuilder.Entity<Application.Gmat>().HasKey(a => a.Gmat_Id); 
      modelBuilder.Entity<Application.Gmat>().Property(a => a.Gmat_Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 
      modelBuilder.Entity<Application.PostGradStudies>().HasKey(a => a.PostGradStudies_Id); 
      modelBuilder.Entity<Application.PostGradStudies>().Property(a => a.PostGradStudies_Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 
      modelBuilder.Entity<Application.PreGradStudies>().HasKey(a => a.PreGradStudies_Id); 
      modelBuilder.Entity<Application.Schoolarships>().HasKey(a => a.Schoolarships_Id); 
      modelBuilder.Entity<Application.Schoolarships>().Property(a => a.Schoolarships_Id).HasDatabaseGeneratedOption(DatabaseGeneratedOption.None); 

      modelBuilder.Entity<Application.Users>().HasRequired(a => a.Applications).WithRequiredPrincipal(i => i.Users); 
      modelBuilder.Entity<Application.Applications>().HasMany(a => a.Addresses).WithRequired(i => i.Applications).HasForeignKey(i => i.Application_Id); 
      modelBuilder.Entity<Application.Applications>().HasMany(a => a.ForeignLanguages).WithRequired(i => i.Applications).HasForeignKey(i => i.Application_Id); 
      modelBuilder.Entity<Application.Applications>().HasOptional(a => a.Gmat).WithRequired(i => i.Applications); 
      modelBuilder.Entity<Application.Applications>().HasOptional(a => a.PostGradStudies).WithRequired(i => i.Applications); 
      modelBuilder.Entity<Application.Applications>().HasMany(a => a.PreGradStudies).WithRequired(i => i.Applications).HasForeignKey(i => i.Application_Id); 
      modelBuilder.Entity<Application.Applications>().HasOptional(a => a.Schoolarships).WithRequired(i => i.Applications); 

     } 
    } 
} 

Applications.cs:

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Web; 

namespace PostGraduate.Models.Application 
{ 
    public class Applications 
    { 

     public Applications() 
     { 
      this.Addresses = new HashSet<Addresses>(); 
      this.PreGradStudies = new HashSet<PreGradStudies>(); 
      this.ForeignLanguages = new HashSet<ForeignLanguages>(); 
     } 

     internal void BuildAddress(int p) 
     { 
      for (int i = 0; i < p; i++) 
      { 
       Addresses.Add(new Addresses()); 
      } 
     } 

     internal void BuildPreGradStudies (int p) 
     { 
      for (int i = 0; i < p; i++) 
      { 
       PreGradStudies.Add(new PreGradStudies()); 
      } 
     } 

     internal void BuildForeignLanguages(int p) 
     { 
      for (int i = 0; i < p; i++) 
      { 
       ForeignLanguages.Add(new ForeignLanguages()); 
      } 
     } 

     public virtual Users Users { get; set; } 
     public virtual Gmat Gmat { get; set; } 
     public virtual PostGradStudies PostGradStudies { get; set; } 
     public virtual Schoolarships Schoolarships { get; set; } 
     public virtual ICollection<Addresses> Addresses { get; set; } 
     public virtual ICollection<PreGradStudies> PreGradStudies { get; set; } 
     public virtual ICollection<ForeignLanguages> ForeignLanguages { get; set; } 

     [ScaffoldColumn(false)] 
     public string Applications_Id { get; set; } 

     [ScaffoldColumn(false)] 
     public DateTime ApplicationDate { get; set; } 

     [Required] 
     public string FathersName { get; set; } 

     [Required] 
     public DateTime? Birthdate { get; set; } 

     [Required] 
     public string Birthplace { get; set; } 

     [Required] 
     public string Identification { get; set; } 

     [Required] 
     public string Country { get; set; } 

     [Required] 
     public string MobileNumber { get; set; } 

     [Required] 
     public string Profession { get; set; } 

     public string Activity { get; set; } 

     public string PostGradExtra { get; set; } 

     public string PostGradReapplication { get; set; } 

     public string ExtraInformation { get; set; } 

     [Required] 
     public string PostGradSource { get; set; } 
    } 
} 

Addresses.cs:

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Web; 

namespace PostGraduate.Models.Application 
{ 
    public class Addresses 
    { 
     public virtual Applications Applications { get; set; } 

     [ScaffoldColumn(false)] 
     public string Addresses_Id { get; set; } 

     [ScaffoldColumn(false)] 
     public string Application_Id { get; set; } 

     [Required] 
     public string StreetAddress { get; set; } 

     [Required] 
     public string City { get; set; } 

     [Required] 
     public string PostalCode { get; set; } 

     [Required] 
     public string PhoneNumber { get; set; } 
    } 
} 

ForeignLanguages.cs:

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Web; 

namespace PostGraduate.Models.Application 
{ 
    public class ForeignLanguages 
    { 

     public virtual Applications Applications { get; set; } 

     [ScaffoldColumn(false)] 
     public string ForeignLanguages_Id { get; set; } 

     [ScaffoldColumn(false)] 
     public string Application_Id { get; set; } 

     public string Language { get; set; } 

     public string LanguageDegree { get; set; } 

     public string Admission { get; set; } 

     public bool Delete { get; set; } 
    } 
} 

Gmat.cs:

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Web; 

namespace PostGraduate.Models.Application 
{ 
    public class Gmat 
    { 
     public virtual Applications Applications { get; set; } 

     [ScaffoldColumn(false)] 
     public string Gmat_Id { get; set; } 

     public DateTime? GmatDate { get; set; } 

     public string GmatGrade { get; set; } 
    } 
} 

PostGradStudies.cs:

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Web; 

namespace PostGraduate.Models.Application 
{ 
    public class PostGradStudies 
    { 
     public virtual Applications Applications { get; set; } 

     [ScaffoldColumn(false)] 
     public string PostGradStudies_Id { get; set; } 

     public string Aei { get; set; } 

     public string PostGradTitle { get; set; } 

     public string PostGradLength { get; set; } 

     public string PostGradGrade { get; set; } 

     public string PostGradProject { get; set; } 

     public string PostGradProjectGrade { get; set; } 
    } 
} 

PreGradStudies.cs:

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Web; 

namespace PostGraduate.Models.Application 
{ 
    public class PreGradStudies 
    { 
     public virtual Applications Applications { get; set; } 

     [ScaffoldColumn(false)] 
     public string PreGradStudies_Id { get; set; } 

     [ScaffoldColumn(false)] 
     public string Application_Id { get; set; } 

     public string University { get; set; } 

     public string Department { get; set; } 

     public string Admission { get; set; } 

     public string Graduation { get; set; } 

     public string DegreeGrade { get; set; } 

     public string ThesisSubject { get; set; } 

     public string ThesisGrade { get; set; } 

     public bool Delete { get; set; } 
    } 
} 

Schoolarships.cs:

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Web; 

namespace PostGraduate.Models.Application 
{ 
    public class Schoolarships 
    { 
     public virtual Applications Applications { get; set; } 

     [ScaffoldColumn(false)] 
     public string Schoolarships_Id { get; set; } 

     public string Schoolar { get; set; } 

     public string SchoolarshipProfession { get; set; } 

     public string SchoolarshipInstitution { get; set; } 
    } 
} 

Users.cs:

using System; 
using System.Collections.Generic; 
using System.ComponentModel.DataAnnotations; 
using System.Linq; 
using System.Web; 

namespace PostGraduate.Models.Application 
{ 
    public class Users 
    { 
     public virtual Applications Applications { get; set; } 

     public string UserId { get; set; } 

     public string UserName { get; set; } 
    } 
} 

回答

0

您可以包括你的Application Controller代碼也?

但我懷疑,這可能是因爲你的一些實體被定義爲沒有自動生成的主鍵,當它們創建對象時,它們都將具有相同的鍵。

我將承擔所有的按鍵都設置爲要麼是空字符串或NULL,所以爲NULL關鍵別的檢查時,就已經獲得了比賽......

(對不起,我不有EF最好的理解,但只是一個想法!)

0

這可能發生的原因之一是,如果你有你的視圖模型兩次相同的屬性。例如,如果您的課堂上有UserName,並且您有另一課程,並且它們都存在於您的視圖中。這將創建一個重複的UserName密鑰。

我遇到過這種情況,當我在同一個類中有兩個同名的屬性時。一個公共字段成員和其他一個get/set訪問

public class User 
{ 
    public string userName; // <---- this should be private 

    public string UserName 
    { 
    get { return userName; } 
    set { userName = value; } 
    } 
} 

通知C#並沒有因爲區分大小寫的抱怨。我通過使字段變量解決了這個問題private

相關問題