违反多重性约束。关系“ Repository.Person_Projects”的角色“ Person_Projects_Source”具有多重性1或0..1 [英] Multiplicity constraint violated. The role 'Person_Projects_Source' of the relationship 'Repository.Person_Projects' has multiplicity 1 or 0..1

查看:71
本文介绍了违反多重性约束。关系“ Repository.Person_Projects”的角色“ Person_Projects_Source”具有多重性1或0..1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,在使用其他数据类型之前遇到了此问题。有关更多详细信息,请参见此处。
违反多重性约束的实体框架5

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.

我的课程是:

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

当我遇到之前的问题时,它是与Result类相同的人员并被告知,如果我要建立多对多关系,则必须向人员添加一个集合,向结果添加一个集合,然后就可以了。

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

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

每次我尝试向项目中添加多个用户时,我都会收到错误消息从 DbContext

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.

偶然发布了错误的类,它是结果而不是Person,现在是正确的

Accidentally posted the wrong class, it was the result instead of the Person, now it's correct

更新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个来自我的存储库类

The 3 are from my repository class

对他们的呼叫是

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

团队是 IEnumerable< int> 并且项目的类型为 Project

Where team is an IEnumerable<int> and project is of type Project

推荐答案

似乎您可能需要 InverseProperty 数据注释属性可以做到这一点:

It looks like you may need the InverseProperty data annotation attribute to do this:

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关联。

The exception message you are receiving is very telling. Since EF is complaining that the relation is expected to be a one-to-zero-or-one, this leads me to believe that EF is confusing the People - Projects association with either the PersonInCharge - Project or ProjectManager - Project association.

解决此问题的一种方法是在您的Project实体上公开外键属性,无论如何,您都应该这样做-如果不这样做,则会出现奇怪的瞬时错误。另一种方法是使用流畅的API(又称模型构建器)代替数据注释。

One way to solve this would be to expose foreign key properties on your Project entity, which you should be doing anyway - you can get weird transient errors when you don't. Another way would be to use the fluent API (a.k.a. model builder) instead of data annotations.

这篇关于违反多重性约束。关系“ Repository.Person_Projects”的角色“ Person_Projects_Source”具有多重性1或0..1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆