简体   繁体   中英

Entity framework - how to insert into many to many table with additional properties?

I have 2 entities.

Project entity

[Table("Project")]
public class Project
{
    public Project()
    {
        Participants = new List<Participation>();
    }

    [Key]
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int ID { get; set; }

    [StringLength(225)]
    [Index(IsUnique=true)]
    public string Name { get; set; }

    public ProjectVisibility Visibility { get; set; }

    public virtual List<Participation> Participants { get; set; }
}

And UserProfile entity

[Table("UserProfile")]
public class UserProfile
{
    [Key]
    [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
    public int UserId { get; set; }

    public string UserName { get; set; }

    public virtual List<Participation> Participations { get; set; }


}

Each User can participate in a project with different access levels to this project, so in the linking table I store not only 2 IDs, but also - 3rd property for the access level. (Accesibility is just an enumeration with 1/2/3/4 available values) Here it is :

 [Table("Participations")]
public class Participation
{
    public virtual Project ProjectReference { get; set; }
    public virtual UserProfile UserReference { get; set; }

    [Key, Column(Order = 0)]
    public int ProjectID { get; set; }
    [Key, Column(Order = 1)]
    public int UserID { get; set; }
    public Accessibility AccessLevel { get; set; }
}

I am trying to create a new Project and assign the current user as the owner of the project, however I keep getting different kind of errors. Here is my 'CreateProject' code :

public void CreateProject(Project model, UserProfile owner)
    {
        var participation = new Participation { ProjectReference = model, UserReference = owner, AccessLevel = Accessibility.Owner };
        Participations.Add(participation);
        Projects.Add(model);
        SaveChanges();
    }

Can anyone tell me how to do this please :) ? Thanks in advance.

Add foreign key attributes to inform EF about relations between keys and navigation properties:

[Table("Participations")]
public class Participation
{
    [ForeignKey("ProjectID")]
    public virtual Project ProjectReference { get; set; }
    [ForeignKey("UserID")]
    public virtual UserProfile UserReference { get; set; }

    [Key, Column(Order = 0)]
    public int ProjectID { get; set; }
    [Key, Column(Order = 1)]
    public int UserID { get; set; }
    public Accessibility AccessLevel { get; set; }
}

Also make sure that you have attached project and user to current context. Otherwise EF will add them with same state as root participation entity, ie with Added state. And EF will treat all entities as newly added and it will try to insert them to database.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM