简体   繁体   English

实体框架过帐记录

[英]Entity Framework Posting records

I've got this model: 我有这个模型:

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; }
}

And this DbContext: 而这个DbContext:

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

I'd like to send the entire FinalizedWebinarAttendeesList to a function vs. having to send each of them (like below). 我想将整个FinalizedWebinarAttendeesList发送给一个函数,而不必发送它们中的每一个(如下所示)。 Is this possible? 这可能吗?

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

Sure, why not ? 当然可以,为什么不呢? Adapt this example to better fit your needs : 修改此示例以更好地满足您的需求:

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

        context.SaveChanges();
    }
}

A context in Entity Framework will remember all changes to tracked objects and apply them all at once when saved. 实体框架中的上下文将记住对跟踪对象的所有更改,并在保存时立即全部应用。 You can perform as many operations as you wish inside the context. 您可以在上下文中执行任意数量的操作。

Note: pushing this to the extreme will likely have an undesirable performance impact, but as long as you're doing a reasonable amount of work, everything should be fine. 注意:将其推到极限可能会对性能产生不良影响,但是,只要您在做合理的工作量,一切就可以了。 As always, measure for performance impacts if you're worried there's a problem. 与往常一样,如果您担心存在问题,请衡量性能影响。

Do you want something like DbSet.AddRange() ? 是否需要DbSet.AddRange()之类的东西? You can add items to an IEnumerable and add the IEnumerable to your DbSet. 您可以将项目添加到IEnumerable并将IEnumerable添加到您的DbSet。

DbSet<T> has an .AddRange(IEnumerable<T>) method that you can use: DbSet<T>具有一个.AddRange(IEnumerable<T>)方法,您可以使用:

context.WebinarFinalAttendee.AddRange(attendees);

More info at https://msdn.microsoft.com/en-us/library/system.data.entity.dbset.addrange(v=vs.113).aspx 有关更多信息, 访问https://msdn.microsoft.com/zh-cn/library/system.data.entity.dbset.addrange(v=vs.113).aspx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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