Thanks
With Regards
Abhishek Rajiv Luv
"Helpful then please Mark as Answer"
http://www.codeabstract.com/
http://pluralsight.com/training/users/abhishekluv
I too once had the same prob...u need to be careful while selecting the Payroll.model for the model and the Dbcontextpayroll for the dbcontext
Thanks
With Regards
Abhishek Rajiv Luv
"Helpful then please Mark as Answer"
http://www.codeabstract.com/
http://pluralsight.com/training/users/abhishekluv
Can u share the payroll.cs files code here ,even if its simple
Thanks
With Regards
Abhishek Rajiv Luv
"Helpful then please Mark as Answer"
http://www.codeabstract.com/
http://pluralsight.com/training/users/abhishekluv
1) Below is my Model class which i am using to create controller
namespace WVIPayRoll.Models
{
using System;
using System.Collections.Generic;
public partial class MonthlySalary
{
public int MonthlySalaryID { get; set; }
public int EmployeeNumber { get; set; }
public Nullable<int> ResettlementAllownce { get; set; }
public Nullable<int> AdditionalRespAllownce { get; set; }
public Nullable<int> OverTime { get; set; }
public Nullable<int> Arrears { get; set; }
public Nullable<int> FestivalBonus { get; set; }
public int TotalMonthlySalary { get; set; }
public int TotalBasicSalary { get; set; }
public int Month { get; set; }
public int Year { get; set; }
public virtual Month Month1 { get; set; }
public virtual MonthlySalary MonthlySalary1 { get; set; }
public virtual MonthlySalary MonthlySalary2 { get; set; }
public virtual Year Year1 { get; set; }
}
}
2) and below is my data context class
namespace WVIPayRoll.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class ASPNETDBEntities : DbContext
{
public ASPNETDBEntities()
: base("name=ASPNETDBEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<aspnet_Applications> aspnet_Applications { get; set; }
public DbSet<aspnet_Membership> aspnet_Membership { get; set; }
public DbSet<aspnet_Paths> aspnet_Paths { get; set; }
public DbSet<aspnet_PersonalizationAllUsers> aspnet_PersonalizationAllUsers { get; set; }
public DbSet<aspnet_PersonalizationPerUser> aspnet_PersonalizationPerUser { get; set; }
public DbSet<aspnet_Profile> aspnet_Profile { get; set; }
public DbSet<aspnet_Roles> aspnet_Roles { get; set; }
public DbSet<aspnet_SchemaVersions> aspnet_SchemaVersions { get; set; }
public DbSet<aspnet_Users> aspnet_Users { get; set; }
public DbSet<aspnet_WebEvent_Events> aspnet_WebEvent_Events { get; set; }
public DbSet<ContactDetail> ContactDetails { get; set; }
public DbSet<Deduction> Deductions { get; set; }
public DbSet<EmergencyContact> EmergencyContacts { get; set; }
public DbSet<Employee> Employees { get; set; }
public DbSet<JobDetail> JobDetails { get; set; }
public DbSet<SalaryDetail> SalaryDetails { get; set; }
public DbSet<Month> Months { get; set; }
public DbSet<MonthlySalary> MonthlySalaries { get; set; }
public DbSet<Year> Years { get; set; }
}
}
asad.malik
Member
218 Points
103 Posts
Error: An item with the same key has already been added.
May 07, 2012 07:06 AM|LINK
When i try to create a new controller (Controller with read/write actions using EF ), i am getting this error,
"An item with the same key has already been added",
Model class: MonthlySalary (WVIPayRoll.Models)
DataContext class: ASPNETDBEntities (WVIPayRoll.Models)
Thanks
Regards,
Asad
Regards
Asad Malik
Mark as answer if it helps
Abhishek Luv
Participant
1746 Points
468 Posts
Re: Error: An item with the same key has already been added.
May 07, 2012 07:19 AM|LINK
Can u show the code ?
With Regards
Abhishek Rajiv Luv
"Helpful then please Mark as Answer"
http://www.codeabstract.com/
http://pluralsight.com/training/users/abhishekluv
asad.malik
Member
218 Points
103 Posts
Re: Error: An item with the same key has already been added.
May 07, 2012 07:23 AM|LINK
Which code??
Regards
Asad Malik
Mark as answer if it helps
ignatandrei
All-Star
135148 Points
21679 Posts
Moderator
MVP
Re: Error: An item with the same key has already been added.
May 07, 2012 07:28 AM|LINK
restart VS
asad.malik
Member
218 Points
103 Posts
Re: Error: An item with the same key has already been added.
May 07, 2012 07:32 AM|LINK
ignatandrei
Same problem..
Regards
Asad Malik
Mark as answer if it helps
Abhishek Luv
Participant
1746 Points
468 Posts
Re: Error: An item with the same key has already been added.
May 07, 2012 07:36 AM|LINK
I too once had the same prob...u need to be careful while selecting the Payroll.model for the model and the Dbcontextpayroll for the dbcontext
With Regards
Abhishek Rajiv Luv
"Helpful then please Mark as Answer"
http://www.codeabstract.com/
http://pluralsight.com/training/users/abhishekluv
asad.malik
Member
218 Points
103 Posts
Re: Error: An item with the same key has already been added.
May 07, 2012 07:39 AM|LINK
i am not complicating things, it is just simple
i am selecting my model class and simply selecting my datacontext class which i think is same for all other controllers as well.
Regards
Asad Malik
Mark as answer if it helps
Abhishek Luv
Participant
1746 Points
468 Posts
Re: Error: An item with the same key has already been added.
May 07, 2012 08:07 AM|LINK
Can u share the payroll.cs files code here ,even if its simple
With Regards
Abhishek Rajiv Luv
"Helpful then please Mark as Answer"
http://www.codeabstract.com/
http://pluralsight.com/training/users/abhishekluv
asad.malik
Member
218 Points
103 Posts
Re: Error: An item with the same key has already been added.
May 07, 2012 09:25 AM|LINK
1) Below is my Model class which i am using to create controller
namespace WVIPayRoll.Models
{
using System;
using System.Collections.Generic;
public partial class MonthlySalary
{
public int MonthlySalaryID { get; set; }
public int EmployeeNumber { get; set; }
public Nullable<int> ResettlementAllownce { get; set; }
public Nullable<int> AdditionalRespAllownce { get; set; }
public Nullable<int> OverTime { get; set; }
public Nullable<int> Arrears { get; set; }
public Nullable<int> FestivalBonus { get; set; }
public int TotalMonthlySalary { get; set; }
public int TotalBasicSalary { get; set; }
public int Month { get; set; }
public int Year { get; set; }
public virtual Month Month1 { get; set; }
public virtual MonthlySalary MonthlySalary1 { get; set; }
public virtual MonthlySalary MonthlySalary2 { get; set; }
public virtual Year Year1 { get; set; }
}
}
2) and below is my data context class
namespace WVIPayRoll.Models
{
using System;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
public partial class ASPNETDBEntities : DbContext
{
public ASPNETDBEntities()
: base("name=ASPNETDBEntities")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
throw new UnintentionalCodeFirstException();
}
public DbSet<aspnet_Applications> aspnet_Applications { get; set; }
public DbSet<aspnet_Membership> aspnet_Membership { get; set; }
public DbSet<aspnet_Paths> aspnet_Paths { get; set; }
public DbSet<aspnet_PersonalizationAllUsers> aspnet_PersonalizationAllUsers { get; set; }
public DbSet<aspnet_PersonalizationPerUser> aspnet_PersonalizationPerUser { get; set; }
public DbSet<aspnet_Profile> aspnet_Profile { get; set; }
public DbSet<aspnet_Roles> aspnet_Roles { get; set; }
public DbSet<aspnet_SchemaVersions> aspnet_SchemaVersions { get; set; }
public DbSet<aspnet_Users> aspnet_Users { get; set; }
public DbSet<aspnet_WebEvent_Events> aspnet_WebEvent_Events { get; set; }
public DbSet<ContactDetail> ContactDetails { get; set; }
public DbSet<Deduction> Deductions { get; set; }
public DbSet<EmergencyContact> EmergencyContacts { get; set; }
public DbSet<Employee> Employees { get; set; }
public DbSet<JobDetail> JobDetails { get; set; }
public DbSet<SalaryDetail> SalaryDetails { get; set; }
public DbSet<Month> Months { get; set; }
public DbSet<MonthlySalary> MonthlySalaries { get; set; }
public DbSet<Year> Years { get; set; }
}
}
Regards
Asad Malik
Mark as answer if it helps
asad.malik
Member
218 Points
103 Posts
Re: Error: An item with the same key has already been added.
May 08, 2012 04:45 AM|LINK
What actually was the problem:
1- One of the entities had a duplicate foreign key, which I confirmed was defined this way in the database as well.
How to solve:
1- First i remove duplicate foreign keys from database.
2- Then i deleted the effected tables from my model.
3- Then i build the solution, it shows errors.
4- Then i update my model from database.
5- Then i tried to add controller, and the issue was solved
6- Happy endings... :D
Regards
Asad Malik
Mark as answer if it helps