[英]Store list of object in SQL Server using Entity Framework
I use Entity Framework to connect my SQL Server database in my C# app. 我使用实体框架在C#应用程序中连接SQL Server数据库。
I have a Location
class, which looks like this : 我有一个Location
类,它看起来像这样:
[Table("DBLocation")]
public class DBLocation {
// ...
public List<DBPicture> pictures{ get; set; }
// ...
}
My problem is, how am I supposed to store the list into the database, do I create another table to match Location IDs and pictures, if yes how do I tell the code to use that ? 我的问题是,如何将列表存储到数据库中,是否创建另一个表以匹配位置ID和图片,如果是,我如何告诉代码使用该表?
Thanks! 谢谢!
Your code will be something like this, 您的代码将是这样的,
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
{
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.