locked
Entity Framework 5 code first problem after installing VisualStudio 2012 RRS feed

  • Question

  • Hi,

    I have VS2010 SP1 and EF5 RC, I was working on a project without problems so far but this morning I installed VS2012 and parts of my project stopped working as expected, this is part of the code that is not working any more:

    [Update]
            public void ModifyRoadKind(RoadKindModel model)
            {
                using (MedicDbContext dbContext = new MedicDbContext())
                {
                    RoadKindModel originalModel = this.ChangeSet.GetOriginal(model);
                    RoadKindEntity originalEntity = dbContext.RoadKindEntities.Find(originalModel.Kind);
                    dbContext.RoadKindEntities.Remove(originalEntity);
    
                    //dbContext.SaveChanges();
                    dbContext.ConfigurationSets.Find(Constants.DefaultConfigurationSetId).RoadKinds.Add(new RoadKindEntity
                                                                                                               {
                                                                                                                   Kind = model.Kind
                                                                                                               });
                    dbContext.SaveChanges();
                }
            }

    what is happening is that the "Remove" line is working but the "Add" line is not, if I inspect the database I see that the entity was removed but the new entity was not added, if I run the code step by step then every thing get saved, also it get saved if I remove te comments from the line in bold (//dbContext.SaveChanges(); , just after the Remove line) . This was working perfectly before I installed VS2012.

    Any ideas on a solution for this, other than the unnecesary repetition of dbContext.SaveChanges().

    This code is part of a RIA Service and I'm using code first but my services inherits from DomainService since DbDomainService is not compatible with EF5

    Thanks a lot in advance

    Juan Carlos

    Thursday, August 30, 2012 8:48 PM

Answers

  • Hello
    Alexander, I solved the problem yesterday night following some advices from
    Colin Blair in the RIA Services forum, the problem was solved when I removed
    all using statements from al domain operations and instead of creating a
    DbContext instance on every DomainService operation I created a private field
    and created a single instance of DbContext on the DomainService constructor,
    then removed all DbContext.SaveChanges() calls from all operations and made a
    single call on DomainService.PersistChageSet() override, for details on this
    please go: http://forums.silverlight.net/p/260114/650104.aspx/1?p=True&t=634824370191685672<o:p></o:p>

    I don´t
    know if this is the normal behavior, well I know I was not using the
    DomainService in the correct way but still I don't know why the same code works
    on a console or windows form app and it doesn't on a DomainService. Also don't
    know why it was working on VS2010. I tried to go back to VS2010 and try the
    solution there but that installation seems to be broken; it does not recognized
    SilverLight 5 anymore. Anyway, now the problem is solved.<o:p></o:p>

    Thank you
    very much for all the help and for all the trouble.<o:p></o:p>



    Juan Carlos

    • Marked as answer by jcgalveza Wednesday, September 5, 2012 2:34 PM
    Wednesday, September 5, 2012 2:34 PM

All replies

  • Hi Juan,

    Welcome to the MSDN forum.

    I’d like to clarify some details:

    Could you please let me know how you migrate your project to Visual Studio 2012?

    Did you install the Entity Framework from NuGet?

    What exception you got when your program executed to “Add” line?

    Is it true that “Add” line runs without any problem if you run your program step by step?

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Monday, September 3, 2012 2:45 AM
  • Hello Alexander, thank you for your response

    Well I just opened my VS2010 solution with VS2012.

    Entity framework was originally installed in VS2010 with NuGet (Ver 5 RC), I did not try to update  in VS2012.

    I'm not receiving any exception, simply the entity I'm trying to add it's not being added, it's silently failing.

    Yes, if I run the code step by step then everything works fine.

    I have another computer at home in which I also installed VS2012 having previously installed VS2010, so I have both VS2010 and VS2012 installed at the same time on that machine, there I migrate the solution to Framework 4.5 and Entity Framework to version 5 using NuGet but the code is still failing in the same way.

    Hope this helps.

    Monday, September 3, 2012 1:04 PM
  • Sorry Alexander, I've previously posted that every thing returned to normal after removing VS2010 and reinstalling VS2012 but that was a mistake, the problem persited, I've deleted the post.
    Monday, September 3, 2012 3:38 PM
  • Hi Jcgalveza,

    Never mind that. I got it. Could you please let me know if you encountered any exception? Or could you please provide with some specific error message?

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Tuesday, September 4, 2012 2:27 AM
  • Hello Alexander, there is NO EXCEPTIONS, it's simply no working as expected, let's see:

    after these 3 lines of code:

    dbContext.RoadKindEntities.Remove(originalEntity);
    dbContext.ConfigurationSets.Find(Constants.DefaultConfigurationSetId).RoadKinds.Add(new RoadKindEntity
                                                                                            {
                                                                                                Kind = model.Kind
                                                                                            });
    dbContext.SaveChanges();

    I expected to see the entity "originalEntity" removed from the table "RoadKindEntities" and a new entity added to the same table, well that was what was happening BEFORE I installed VS2012, but AFTER I installed VS2012 the new entity that is supposed to be added in the second line is not being added, NO EXCEPTIONS THROWN, on the other hand, if I run that same code step-by-step then the code works just find, the same way it was working before I installed VS2012.

    Also, if I modify the code the following way:

    dbContext.RoadKindEntities.Remove(originalEntity);
    dbContext.SaveChanges();
    dbContext.ConfigurationSets.Find(Constants.DefaultConfigurationSetId).RoadKinds.Add(new RoadKindEntity
                                                                                            {
                                                                                               Kind = model.Kind
                                                                                            });
    dbContext.SaveChanges();
    

    Then the "originalEntity" is removed from the underlying database and also the new entity is added to the underlying database.

    Please note there are two calls to dbContext.SaveChanges() (in bold in the previous code segment), the first call should be completely unnecessary since the second call should do the job.

    Once again THERE ARE NO EXCEPTIONS THROWN

    Please, also notice that the code I presented is part of a previously working Silverlight project, specifically, that code is part of an [Update] operation in a DomainService. This project was working perfectly until I decided to install VS2012.

    So far I have uninstalled VS2010 and the problem persists, also I have uninstalled SQL server Express 2008 R2 and use LocalDB instead with the same results, that is, the problem persisted.

    Also I have updated Entity Framework 5RC to Entity Framework 5 and again, the same problem persisted.

    Please if there is something I can do to help you reproduce this error please let me know.

    P.S.: This is my DbContext:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Data.Entity;
    using System.ComponentModel.DataAnnotations;
    
    
    namespace MedicDb
    {
        /// <summary>
        /// El contexto code first de la base de datos
        /// </summary>
        public class MedicDbContext : DbContext
        {
            public DbSet<UserFullNameEntity> UsersFullName { get; set; }
    
            public DbSet<Staff.StaffDataEntity> StaffDataEntities { get; set; }
    
            public DbSet<MedicDb.Physician.PhysicianDataEntity> PhysicianDataEntities { get; set; }
    
            public DbSet<MedicDb.Patient.PatientDataEntity> PatientDataEntities { get; set; }
    
            public DbSet<MedicDb.Patient.Personal.PersonalPatientDataEntity> PersonalPatientDataEntities { get; set; }
    
            public DbSet<MedicDb.Patient.Financial.FinancialPatientDataEntity> FinancialPatientDataEntities { get; set; }
    
            public DbSet<MedicDb.Patient.Medical.MedicalPatientDataEntity> MedicalPatientDataEntities { get; set; }
    
            //Configuration
    
            public DbSet<MedicDb.Configuration.ConfigurationSetEntity> ConfigurationSets { get; set; }
    
            public DbSet<MedicDb.Configuration.IsapreEntity> IsapreEntities { get; set; }
    
            public DbSet<MedicDb.Configuration.PatientKindEntity> PatientKindEntities { get; set; }
    
            public DbSet<MedicDb.Configuration.GenderEntity> GenderEntities { get; set; }
    
            public DbSet<MedicDb.Configuration.MaritalStateEntity> MaritalStateEntities { get; set; }
    
            public DbSet<MedicDb.Configuration.TitleEntity> TitleEntities { get; set; }
    
            public DbSet<MedicDb.Configuration.EducationEntity> EducationEntities { get; set; }
    
            public DbSet<MedicDb.Configuration.GeneralProfessionEntity> GeneralProfessionEntities { get; set; }
    
            public DbSet<MedicDb.Configuration.UniversityEntity> UniversityEntities { get; set; }
    
            public DbSet<MedicDb.Configuration.MedicalProfessionEntity> MedicalProfessionEntities { get; set; }
    
            public DbSet<MedicDb.Configuration.MedicalProfession.MedicalSpecialty> MedicalSpecialties { get; set; }
    
            public DbSet<MedicDb.Configuration.RoadKindEntity> RoadKindEntities { get; set; }
    
            public DbSet<MedicDb.Configuration.GeographicInfoEntity> GeographicInfoEntities { get; set; }
    
            public DbSet<MedicDb.Configuration.Geographic.Municipality> Municipalities { get; set; }
    
            public DbSet<MedicDb.Configuration.Geographic.PostCode> PostCodes { get; set; }
    
            public DbSet<MedicDb.Configuration.Geographic.Province> Provinces { get; set; }
    
            public DbSet<MedicDb.Configuration.Geographic.Region> Regions { get; set; }
             
        }
    }

    This context and all asociated entities are located in a separate project inside the solution ( a class library project ), also the solution have an MVC4 project and a Silverlight 5 project.

    Please sorry for my bad English.


    • Edited by jcgalveza Tuesday, September 4, 2012 12:29 PM
    Tuesday, September 4, 2012 12:24 PM
  • Hello again Alexander, I've being trying to reproduce the error on a new Windows form project and has been imposible to reproduce, on my silverlight project it continues to fail but on the new project it does not.

    So right now I'm not sure on what to do.

    Please read my previous post (which appears after this post).

    There is something I could send, perhaps parts of my code, so you can reproduce my problem? I've been trying here for several days and right now I'm not sure what else to do to solve the problem.

    Thanks

    Juan Carlos


    • Edited by jcgalveza Tuesday, September 4, 2012 4:09 PM
    Tuesday, September 4, 2012 4:08 PM
  • Hi Juan,

    I tried to reproduce your problem on a console application, but it works on my side. Then I find this article. I am not sure if it describes the same problem as you, but you can take a look: http://support.microsoft.com/kb/2745294

    Good day.


    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us

    Wednesday, September 5, 2012 6:47 AM
  • Hello
    Alexander, I solved the problem yesterday night following some advices from
    Colin Blair in the RIA Services forum, the problem was solved when I removed
    all using statements from al domain operations and instead of creating a
    DbContext instance on every DomainService operation I created a private field
    and created a single instance of DbContext on the DomainService constructor,
    then removed all DbContext.SaveChanges() calls from all operations and made a
    single call on DomainService.PersistChageSet() override, for details on this
    please go: http://forums.silverlight.net/p/260114/650104.aspx/1?p=True&t=634824370191685672<o:p></o:p>

    I don´t
    know if this is the normal behavior, well I know I was not using the
    DomainService in the correct way but still I don't know why the same code works
    on a console or windows form app and it doesn't on a DomainService. Also don't
    know why it was working on VS2010. I tried to go back to VS2010 and try the
    solution there but that installation seems to be broken; it does not recognized
    SilverLight 5 anymore. Anyway, now the problem is solved.<o:p></o:p>

    Thank you
    very much for all the help and for all the trouble.<o:p></o:p>



    Juan Carlos

    • Marked as answer by jcgalveza Wednesday, September 5, 2012 2:34 PM
    Wednesday, September 5, 2012 2:34 PM