Only3lue
Only3lue

Reputation: 255

entity framework add data to multible tables

for my current project i would like to add new data from an ObservableCollection. The Collection contains a big object with all the attributes of the three tables i use. I use the Entity Framework 5.0 in Visual Studio 2015, the newest version of MariaDB and the newest version of the mysql-connector. Unfortunately i haven´t found an example for multiple tables; only for one. So i tried this, but it throws always an DbEntityValidationException in the EntityFramework.dll:

foreach(ZeichnungInDB zeichnungInDB in zeichnungen)
        {
            zeichnungInDB.Volante_Index = getVolCountByDrawingNumber(zeichnungInDB.Zeichnungsnummer)+1;
            using (DMSContext db = new  DMSContext())
            {
                var zeichnung = new zeichnung()
                {
                    Zeichnung_ID = zeichnungInDB.Dateiname + "_" + zeichnungInDB.Index + "_VOL_" + zeichnungInDB.Volante_Index + "_" + new DateTime().ToShortDateString(),
                    Baugruppe = zeichnungInDB.Baugruppe,
                    Baugruppe_Hauptzeichnung = zeichnungInDB.Baugruppe_Hauptzeichnung,
                    Zeichnungsnummer = zeichnungInDB.Zeichnungsnummer,
                    Index = zeichnungInDB.Index,
                    Dateiname_Org = zeichnungInDB.Dateiname,
                    Aenderung_Ext = zeichnungInDB.Aenderung_Ext,
                    Aenderung_Int = "AE_" + zeichnungInDB.Projektnummer + new DateTime(),
                    Dokumententyp = zeichnungInDB.DokumentenTyp,
                    Dateiendung = zeichnungInDB.Extension,
                    Volante_Index = zeichnungInDB.Volante_Index,
                    MMS_Sachmerkmal = zeichnungInDB.Mms_Sachmerkmal,
                    Status = zeichnungInDB.Status,
                    Aenderung_Bemerkung_Txt = zeichnungInDB.Aenderung_Bemerkung_Text,
                    Einzel_Bemerkung_Txt = zeichnungInDB.Einzel_Bemerkung,
                    Ahang_Link = zeichnungInDB.Anhang_Link,
                    Einzel_Link = zeichnungInDB.Einzel_Link,
                };

                var projekt = new projekt()
                {
                    Projektnummer = zeichnungInDB.Projektnummer,
                };

                var tag = new tag()
                {
                    Tag1 = zeichnungInDB.Tag,
                };

                //var zeichnung = new zeichnung();
                //zeichnung.

                db.zeichnungs.Add(zeichnung);
                db.projekts.Add(projekt);
                db.tags.Add(tag);
                db.SaveChanges();
            }
        }

I know that the code is not that performant. But i just want that it works. The best way would be just to give the database the collection in one step or how to insert the new data in one database access but i dont know that works because i quit my job after finishing my education for three years now. It would be very helpful if someone has an answer for my problem.

Cheers, Only3lue

Upvotes: 0

Views: 63

Answers (1)

Yanga
Yanga

Reputation: 3012

You should try to catch the DbEntityValidationException and see which input throw the exception :

try
{
    db.SaveChanges();
}
catch (System.Data.Entity.Validation.DbEntityValidationException dbEx)
{
    Exception raise = dbEx;
    foreach (var validationErrors in dbEx.EntityValidationErrors)
    {
        foreach (var validationError in validationErrors.ValidationErrors)
        {
            string message = string.Format("{0}:{1}", 
            validationErrors.Entry.Entity.ToString(),
            validationError.ErrorMessage);
            // raise a new exception nesting
            // the current instance as InnerException
            raise = new InvalidOperationException(message, raise);
        }
    }
    throw raise;
}

Upvotes: 1

Related Questions