0

I am new to EF and I am trying to work with a many-to-many relationship.

I have a Developers table and I have a Applications table.

Each developer can have many applications, and each application can have many developers.

I have tried to follow examples on StackOverflow where it mentions to attach, but I've tried everything and it still creates duplicate developers for an application.

public class Developer 
    {
        public int DeveloperId { get; set; }
        [Required]
        public string DeveloperName { get; set; }
        public virtual List<Application> Applications { get; set; }
    }

public class Application 
    {
        public int Id { get; set; }
        [Required]
        public string Name { get; set; }
        [Required]
        public string Description { get; set; }
        public string Note { get; set; }
        public Developer Owner { get; set;}
        public virtual List<Developer> Developers { get; set; }
    }

protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {

            modelBuilder.Entity<Developer>()
                .HasMany<Application>(d => d.Applications)
                .WithMany(a => a.Developers)
                .Map(ad =>
                {
                    ad.MapLeftKey("DeveloperRefId");
                    ad.MapRightKey("ApplicationRefId");
                    ad.ToTable("DeveloperApplication");
                });
        }

Above describes my setup. I've tried so many things, but any time a new application is added, EF creates a new developer to associate with it.

                context.Applications.Add(app);
                   // no matter what I try here I get a duplicate developer
                context.SaveChanges();
                return Ok(app.Id);

Any suggestions would be greatly appreciated! Thank you!

2
  • Where is the Application.Developers collection? And what's the purpose of Application.Owner property, which defines another one-to-many relationship between Developer and Application? Your issue might be related to that property rather than the may-to-many relationship. Commented Feb 16, 2018 at 19:50
  • Sorry, I was missing the developers list on this post and have updated my question accordingly. The purpose of Application.Owner is the 1 developer who is the lead on the project. I suspect this may be indeed causing the issue? Commented Feb 16, 2018 at 19:58

1 Answer 1

1

Change:

public class Application 
{
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Description { get; set; }
    public string Note { get; set; }
    public Developer Owner { get; set;}
}

To:

public class Application 
{
    private List<Developer> _Developers; 
    public int Id { get; set; }
    [Required]
    public string Name { get; set; }
    [Required]
    public string Description { get; set; }
    public string Note { get; set; }

    [Required(ErrorMessage="The Id Of Owner(owner as a developer) is Required")]
    public int DeveloperId { get; set; } 

    [ForeignKey("DeveloperId")]
    public virtual Developer Owner { get; set;}


    public virtual List<Developer> Developers { get{return _Developers;}} // This create Many to many

    public application(){
        _Developers= new List<Developer>();
    }
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your reply! I've made these changes, but I am still getting duplicate developers whenever I add a new Application.
when you create the Application, do not use owner = new Developer (), this creates a new developer and it is inserted into the database, better use DeveloperId = (developer's id) *
example: var app= new Application{DeveloperId=1, Name= "ExampleApp", Description= "an Example app" }; context.Applications.Add(app); // no matter what I try here I get a duplicate developer context.SaveChanges(); return Ok(app.Id); if on database not existe an develop with Id 1, the application dont save, but if existe this asigned to the developer with id 1
check my previous comment

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.