2016-08-06 11 views
0

Ich verwende Entity Framework, um meine SQL Server-Datenbank in meiner C# -App zu verbinden.Liste der Objekte in SQL Server mit Entity Framework speichern

Ich habe eine Location-Klasse, die wie folgt aussieht:

[Table("DBLocation")] 
public class DBLocation { 
    // ... 
    public List<DBPicture> pictures{ get; set; } 
    // ... 
} 

Mein Problem ist, wie kann ich die Liste in der Datenbank speichern soll, erstelle ich eine andere Tabelle Location-IDs und Bilder entsprechen, Wenn ja, wie sage ich dem Code, das zu verwenden?

Danke!

+0

Sie könnten ein Modell erstellen und die Variable Objekt zuweisen Felder des Modells – Sajeetharan

+0

Sie meinen, so etwas wie eine LocationPicture Klasse, mit tho ganzen Zahlen Felder wie idLocation und idPicture ? – Rogue

+0

ja! gleiche Sache :) – Sajeetharan

Antwort

0

Ihr Code wird so etwas wie dieses,

public void savePicture(List<DBPicture> pics) 
     { 
      //Getting a list of pics of type DBPicture as you mentioned in class 
      try 
      { 
       foreach(DBPicture pic in pics) 
       //Using the model created from database 
       using (DataModel.PicEntities picContext = new PicEntities()) 
       { 

        DataModel.DBPicture dbPic = new DBPicture(); 
        dbPic.idPic = pic.idPicture; 
        dbPic.idLoc = pic.idLocation; 
        picContext.Meetings.Add(dbPic); 
        picContext.SaveChanges(); 
       } 
      } 
      catch 
      { 

      } 

     } 
Verwandte Themen