简体   繁体   中英

entity framework add data to table

I'm trying to insert data into a MariaDB with EntityFrameWork 5 in Visual Studio. I created the database on the server itself and imported the model in Visual Studio. I'm using three tables and they are linked.

A draing table, project table and tag table.
Entities are: drawing (many---1) project (1---many) tag.

For the insert I use the code below. But it throws an DbUpdateEntityException who tells that he can't match the key between the drawing and project table because two or more primary keys are the same. I use a specific primary key for dat drawing which is definitely not the same with a other object in the table. The other primary keys are AUTO_INCREMENT So I guess that it tries to insert a new project but with the same project number which don't work but I don't really get it why it is doing this.

DateTime aktuellesDatum = DateTime.Now;
using (DMSContext db = new DMSContext())    
{
     foreach (ZeichnungInDB zeichnungInDB in zeichnungen)
    {
        zeichnungInDB.Volante_Index = getVolCountByDrawingNumber(zeichnungInDB.Zeichnungsnummer) + 1;
        var zeichnung = new zeichnung()
        {
            Zeichnung_ID = zeichnungInDB.Dateiname + "_" + zeichnungInDB.Index + "_VOL_" + zeichnungInDB.Volante_Index + "_" + aktuellesDatum.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 + aktuellesDatum,
            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,
        };
       
        db.zeichnungs.Add(zeichnung);
        db.projekts.Add(projekt);
        db.tags.Add(tag);
    }
    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;
    }
}

maybe someone has an idea or a solution where the problem is. (The "ZeichnungInDB" is an custom object and "zeichnungen" is ObservableCollection)

The other question I have is, I implemented a field for date time which is CURRENT_TIMESTSAMP but it only shows 0000-00-00 00:00:00 why is this?

Are you using fluent API? if you are, you should mark the id's properties as Identity like the example:

 Property(p => p.DrawingId).
            HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);

Or you can mark with an annotation in the primary key of your entities like the example:

public class drawing {

   [Key]
   [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
   public int ind DrawingId {get; set;}
   ....

}

link: http://www.opten.ch/blog/2014/03/5/configuring-entity-framework-with-fluent-api/

regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM