none
[Entity Framework]Insert operation was not performed? RRS feed

  • Question

  • My aim is to insert an entry into Association table named as AssnCaseType.

    And DataBase Diagram is like this:

    Image

    and my Insert method like this:

      public void AddIntoAssnCaseType(int CaseTypeID)
            {
                Case c = db.Cases.Find(this.CaseId);
                CaseType ct = new CaseType();
                ct.CaseTypeID = CaseTypeID;
                ct.Cases.Add(c);
                db.SaveChanges();
            }

    But entry is not going into AssnCaseType table.Please help me to find solution.

    I did like this also but it Create  New entry in CaseType table:

    Case c = db.Cases.Find(this.CaseId);
                CaseType ct = new CaseType();
                ct.CaseTypeID = CaseTypeID;
                c.CaseTypes.Add(ct);
                db.SaveChanges();



    • Edited by dvaibhav Tuesday, July 12, 2016 6:35 AM
    Tuesday, July 12, 2016 6:31 AM

Answers

  • Hi dvaibhav,

    According to your description, it seems that Case and CaseType have added in your database, and you just want to add a records in your AssnCaseType table. If it is the case, please refer the following code:

    using (var db = new Model1())
                {
                    Case c = db.Cases.Find(1);
                    CaseType ct = db.CaseTypes.Find(1);
                    c.CaseTypes.Add(ct);
                    db.SaveChanges();
    
                }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, July 13, 2016 7:44 AM
    Moderator

All replies

  • Hi dvaibhav,

    According to your description, it seems that you could use many-to-many relationship to achieve it. I create a simple demo as below for your reference.

    #models

    public class Case
        {
            public Case()
            {
                this.CaseTypes = new HashSet<CaseType>();
            }
    
            public int CaseID { get; set; }
            public string Description { get; set; }
    // .... other property
            public virtual ICollection<CaseType> CaseTypes { get; set; }
        }
    
        public class CaseType
        {
            public CaseType()
            {
                this.Cases = new HashSet<Case>();
            }
    
            public int CaseTypeID { get; set; }
            public string Type { get; set; }
       // .... other property
            public virtual ICollection<Case> Cases { get; set; }
        }


    #DbContext

    namespace ConsoleApplication2
    {
        using System.Data.Entity;
    
        public partial class Model1 : DbContext
        {
            public Model1()
                : base("name=Model1")
            {
            }
    
            public DbSet<Case> Cases { get; set; }
            public DbSet<CaseType> CaseTypes { get; set; }
            protected override void OnModelCreating(DbModelBuilder modelBuilder)
            {
                modelBuilder.Entity<Case>()
                    .HasMany<CaseType>(s => s.CaseTypes)
                    .WithMany(c => c.Cases)
                    .Map(cs =>
                    {
                        cs.MapLeftKey("CaseID");
                        cs.MapRightKey("CaseTypeID");
                        cs.ToTable("AssnCaseType");
                    });
            }
        }
    }
    

    #Usage:

    using (var db = new Model1())
                {
                  
                    Case c = db.Cases.Find(1);
    
                    CaseType ct = new CaseType() { Type = "Test" };
    
                    c.CaseTypes.Add(ct);
                    db.SaveChanges();
    
                }

    For more information many-to-many relationship, please refer to :

    http://www.entityframeworktutorial.net/code-first/configure-many-to-many-relationship-in-code-first.aspx

    http://www.entityframeworktutorial.net/EntityFramework4.3/add-many-to-many-entity-using-dbcontex

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place. <br/> Click <a href="http://support.microsoft.com/common/survey.aspx?showpage=1&scid=sw%3Ben%3B3559&theme=tech"> HERE</a> to participate the survey.

    Wednesday, July 13, 2016 1:59 AM
    Moderator
  • Hi cole wu,

    Thanks for your reply.

    According to your solution,

    1st Column of AssnCaseType table named as CaseID is ok.But,

    In CaseType table it creates new entry and that newly created  entry's Id's inserted into AssnCaseType table of column CaseTypeID.

    My goal is to refer Id's of Case and CaseType and Insert accordingly in AssnCaseType table only. And no new entry in CaseType table.

    Please can you suggest some modifications according I mentioned?


    • Edited by dvaibhav Wednesday, July 13, 2016 7:00 AM
    Wednesday, July 13, 2016 6:59 AM
  • Hi dvaibhav,

    According to your description, it seems that Case and CaseType have added in your database, and you just want to add a records in your AssnCaseType table. If it is the case, please refer the following code:

    using (var db = new Model1())
                {
                    Case c = db.Cases.Find(1);
                    CaseType ct = db.CaseTypes.Find(1);
                    c.CaseTypes.Add(ct);
                    db.SaveChanges();
    
                }

    Best regards,

    Cole Wu


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Wednesday, July 13, 2016 7:44 AM
    Moderator
  • Thanks a lot.

    It works fine now.

    Thursday, July 14, 2016 6:35 AM