none
Entity Framework 5.0, SaveChanges() Method not working using a projection Class with winforms DataGridView RRS feed

  • Question

  • Hey there!

    I got a problem with the EF 5.0 Method SavingChanges(). For the reason that i want to have an editable DataGridView (Winforms) i used a frojection Class calle "ProjectionClass.cs" to "map" the data receiving through EF. This Projection class looks like:

      public partial class ProjectionClass
        {
            public int? SFakturenNummer { get; set; }
            public int SRapporteId { get; set; }
            public DateTime SRapporteDatum { get; set; }
            public string SMitarbeiterKurzeichen { get; set; }
            public int SLeistungenLeistungenCode { get; set; }
            public string SKommentar { get; set; }
            public string SLeistungenBezeichnung { get; set; }
            public double? SAnzahl { get; set; }
            public double? SAnsatzBetrag { get; set; }
            public double SLeistungenFaktor { get; set; }
            public bool SVisum1 { get; set; }    
        }

    The linq query i set to the DB looks like this:

         var query = (from rapporte in context.mRapporte
    
                                where rapporte.ProjekteId == lProjekteId
                                select new ProjectionClass  { SFakturenNummer = rapporte.FakturenNummer, SRapporteId = rapporte.RapporteId, SRapporteDatum = rapporte.RapporteDatum,
                                    SMitarbeiterKurzeichen = rapporte.mMitarbeiter.Kurzzeichen, SLeistungenLeistungenCode = rapporte.mLeistungen.LeistungenCode, 
                                    SKommentar = rapporte.Kommentar, SLeistungenBezeichnung = rapporte.mLeistungen.Bezeichnung, SAnzahl = rapporte.Anzahl, 
                                    SAnsatzBetrag = rapporte.AnsatzBetrag, SLeistungenFaktor = rapporte.LeistungenFaktor, SVisum1 = rapporte.Visum1 });

    then i bind the query to the binding Source like this:

        bsDataSource.DataSource = new SortableBindingList<ProjectionClass>(query.ToList());

    Has somebody any advice how i can affect that SaveChanges() works with my ProjectionClass. Any implementation needed to make it working with EF 5.0 ?

    Thanks a lot for any hint :)

    Leezer

    Friday, May 16, 2014 7:40 AM

Answers

  • A projected class/object is not an EF class/object on the EF model. You have to map the projected class/object to an EF class/object that's on the model,  and the SaveChanges() is used on the EF class/object on the model to persist the EF class/object to the database. 
    • Marked as answer by Leezer Friday, May 16, 2014 12:28 PM
    Friday, May 16, 2014 7:55 AM
  • http://www.codeproject.com/Articles/61629/AutoMapper

    You can use AutoMapper,  or you make a new Entity/Class/object off of the model and manually map the properties of the projected object to the EF object and persist the EF object to the database.

    • Marked as answer by Leezer Friday, May 16, 2014 12:28 PM
    Friday, May 16, 2014 9:00 AM

All replies

  • A projected class/object is not an EF class/object on the EF model. You have to map the projected class/object to an EF class/object that's on the model,  and the SaveChanges() is used on the EF class/object on the model to persist the EF class/object to the database. 
    • Marked as answer by Leezer Friday, May 16, 2014 12:28 PM
    Friday, May 16, 2014 7:55 AM
  • Hi darnold924, thanks for your reply!

    do you have me a tipp how to "re"-map the class to EF-Classes, e.g. in which event i could do this?

    Friday, May 16, 2014 8:10 AM
  • http://www.codeproject.com/Articles/61629/AutoMapper

    You can use AutoMapper,  or you make a new Entity/Class/object off of the model and manually map the properties of the projected object to the EF object and persist the EF object to the database.

    • Marked as answer by Leezer Friday, May 16, 2014 12:28 PM
    Friday, May 16, 2014 9:00 AM