2016-11-03 58 views
0

我有了这个模型:实体框架发帖记录

public class FinalizedWebinarAttendeesList 
{ 
    public List<FinalizedWebinar> Webinars { get; set; } 
} 

public class FinalizedWebinar 
{ 
    public int ParticipantID { get; set; } 
    public bool AffidavitRecvd { get; set; } 
    public string EventCode { get; set; } 
} 

这的DbContext:

public class webinarFinalizedAttendeesListDbContext : DbContext 
{ 
    public webinarFinalizedAttendeesListDbContext(string nameOrConnectionString) : base(nameOrConnectionString) { } 
    public DbSet<FinalizedWebinar> WebinarFinalAttendee { get; set; } 
} 

我想整个FinalizedWebinarAttendeesList发送到功能与有互送(如下图)。 这可能吗?

public void InsertAttendee(FinalizedWebinar aa) 
{ 
    using (webinarFinalizedAttendeesListDbContext context = new webinarFinalizedAttendeesListDbContext(connectionString)) 
    { 
     context.WebinarFinalAttendee.Add(aa); 
     context.SaveChanges(); 
    } 
} 

回答

0

DbSet<T>有一个.AddRange(IEnumerable<T>)方法,您可以使用:

context.WebinarFinalAttendee.AddRange(attendees); 

更多信息以https://msdn.microsoft.com/en-us/library/system.data.entity.dbset.addrange(v=vs.113).aspx

+0

我真的想把整个列表发送到我的Oracle数据库中的一个过程,并通过它们循环插入与使用DbContext来添加它们。那可能吗?如果可以的话,我仍然必须编写我的Oracle过程。 – MB34

+0

如果你已经在使用实体框架,我不确定你希望通过在Oracle中直接插入来实现。实体框架将为您照顾。 – UtopiaLtd

+0

由于记录确实不符合需要插入的数据。这就是为什么我不得不编写一个Oracle存储过程来执行实际的插入操作。 '我想将整个FinalizedWebinarAttendeesList发送给一个函数,而不必发送它们中的每一个(如下所示)。 ' – MB34

0

当然,为什么不呢?适应这个例子中,以更好地满足您的需求:

public void InsertAttendee(List<FinalizedWebinar> webinars) 
{ 
    using (webinarFinalizedAttendeesListDbContext context = new webinarFinalizedAttendeesListDbContext(connectionString)) 
    { 
     foreach(var webinar in webinars) { 
      context.WebinarFinalAttendee.Add(webinar); 
     } 

     context.SaveChanges(); 
    } 
} 

在实体框架的上下文将记住所有变更跟踪对象,并保存在一次应用它们。您可以根据需要在上下文中执行尽可能多的操作。

注意:将其推到极限可能会产生不良的性能影响,但只要您在做合理的工作量,一切都应该没问题。与往常一样,如果担心出现问题,请评估性能影响。

+0

这也有遍历列表中添加他们。我想不必那样做。 – MB34

0

你想要类似DbSet.AddRange()的东西吗?您可以将项添加到IEnumerable并将IEnumerable添加到您的DbSet。