Hello had this problem before with another data type. For more details you can see here. Multiplicity constraint violated Entity framework 5
But now that solution doesn't work or any other found on the net.
My classes are:
namespace Prometheus.Models
{
[Table("People")]
public class Person : IPerson, INamedEntity
{
[Key]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public virtual int Id { get; set; }
[DisplayName("Prenume")]
public virtual string FirstName { get; set; }
[DisplayName("Nume")]
public virtual string LastName { get; set; }
public string Name
{
get
{
return FirstName + " " + LastName;
}
set { }
}
[DisplayName("Email")]
public virtual string Email { get; set; }
public DateTime? LastModified { get; set; }
public virtual ICollection<Result> Results { get; set; }
public virtual ICollection<Project> Projects { get; set; }
}
}
And
public class Project :INamedEntity
{
public int Id { get; set; }
public DateTime? LastModified { get; set; }
[DisplayName("Titlu Proiect")]
[Required]
public string Name { get; set; }//Title
[Required]
[DisplayName("Tip proiect")]
public virtual ProjectType ProjectType{ get; set; }
[DisplayName("Status proiect")]
public virtual ProjectStatus ProjectStatus { get; set; }
[Required]
[DisplayName("Tip program")]
public virtual ProgramType ProgramType { get; set; }
[Required]
[DisplayName("Numar luni")]
public int NumberOfMonths { get; set; }
[Required]
[DisplayName("Project title")]
public string NameEn { get; set; }
[Required]
[DisplayName("Acronim")]
public string Acronym { get; set; }
[Required]
[DisplayName("Numar contract")]
public string ContractNo { get; set; }
[Required]
[DisplayName("Domeniu de activitate")]
public virtual ActivityField ActivityField {get;set;}
[Required]
[DisplayName("Summary")]
public string SummaryEn { get; set; }
[Required]
[DisplayName("Rezumat")]
public string SumarryRo { get; set; }
[Required]
[DisplayName("Data inceput")]
public virtual DateTime StartDate { get; set; }
[Required]
[DisplayName("Data sfarsit")]
public virtual DateTime EndDate { get; set; }
[Required]
[DisplayName("Institutie coordonatoare")]
public virtual string CoordInstitution { get; set; }
[DisplayName("Valoare totala proiect")]
public decimal TotalValue { get; set; }
[DisplayName("Valuta")]
public virtual Currency Currency { get; set; }
[DisplayName("Rol in proiect")]
public virtual RoleInProject RoleInProject { get; set; }//coord proiect/partener/in afara inst
[DisplayName("Echipa proiect")]
public virtual ICollection<Person> People { get; set; }
[DisplayName("Director proiect")]
public virtual Person ProjectManager { get; set; }
public virtual ICollection<Institution> Partners { get;set;}
public virtual Person PersonInCharge { get; set; }
[DisplayName("Functie/Rol Expert In Proiect")]
public string UserFunctionInProject { get; set; }
[DisplayName("Pagina proiectului")]
[Required]
public string Website {get;set;}
public virtual ICollection<Result> Results { get; set; }
}
When i had the previous problem it was with a class Result and the same Person and was told that if i want a many-to-many relationship i have to add a Collection to Person and a Collection to Result and that did the trick.
But now as you can see i already have
public virtual ICollection<Person> People { get; set; } in Project
and
public virtual ICollection<Project> Projects { get; set; } in Person
And every time i try to add more than one User to the project i get the error in that title when calling the Save()
method from DbContext
I have no idea what to do. Thanks.
Accidentally posted the wrong class, it was the result instead of the Person, now it's correct
Update 1
public void AddProjectForUsers(IEnumerable<int> userIds, Project project)
{
foreach (var id in userIds)
{
AddProjectForUser(id,project);
}
}
public void AddProjectForUser(int userId, Project project)
{
var user = _ctx.Users.SingleOrDefault(u => u.Id == userId);
if (user != null)
{
if (!user.Projects.Contains(project))
{
user.Projects.Add(project);
}
}
}
public bool Save()
{
return _ctx.SaveChanges() > 0; // the error is thrown here
}
The 3 are from my repository class
And the call to them is
Repo.AddProjectForUsers(team, project);
Repo.Save();
Where team is an IEnumerable<int>
and project is of type Project