违反了多重约束。 “Repository.Person_Projects”关系的“Person_Projects_Source”角色具有多重性1或0..1

时间:2014-02-12 15:38:08

标签: c# asp.net asp.net-mvc entity-framework

Hello之前遇到此问题的另一种数据类型。有关详细信息,请参阅此处。 Multiplicity constraint violated Entity framework 5

但现在该解决方案不起作用或在网上找到任何其他解决方案。

我的课程是:

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; }
    }
}

并且

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; }
}

当我遇到上一个问题时,它是一个类结果和同一个人,并被告知,如果我想要一个多对多的关系,我必须添加一个集合到人和一个集合到结果,并做了特技。

但现在你可以看到我已经有了

public virtual ICollection<Person> People { get; set; } in Project

public virtual ICollection<Project> Projects { get; set; } in Person

每当我尝试向项目添加多个用户时,我从Save()

调用DbContext方法时会收到该标题中的错误

我不知道该怎么做。 感谢。

意外地发布了错误的类,这是结果而不是Person,现在它是正确的

更新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
}

3来自我的存储库类

对他们的打电话是

Repo.AddProjectForUsers(team, project);
Repo.Save();

团队成员为IEnumerable<int>,项目属于Project

1 个答案:

答案 0 :(得分:3)

看起来您可能需要InverseProperty数据注释属性来执行此操作:

namespace Prometheus.Models
{
    [Table("People")]
    public class Person : IPerson, INamedEntity
    {
        ...
        [InverseProperty("People")]
        public virtual ICollection<Project> Projects { get; set; }
    }
}

public class Project :INamedEntity
{
    ...
    [DisplayName("Echipa proiect")]
    [InverseProperty("Projects")]
    public virtual ICollection<Person> People { get; set; }
}

您收到的异常消息非常有说服力。由于EF抱怨这种关系应该是一对一或零,这让我相信EF将People - Projects关联与PersonInCharge - Project或ProjectManager - Project关联混淆。

解决此问题的一种方法是在Project实体上公开外键属性,无论如何都应该这样做 - 如果不这样做,你可能会遇到奇怪的瞬态错误。另一种方法是使用流畅的API(a.k.a。模型构建器)而不是数据注释。