简体   繁体   中英

Add new record hangs on WinfForm - and I don't understand why

I have inherited a Windows Forms Application. We are having some performance issues, when trying to save a new record to the SQL Table. It hangs.

For the most part, I have ruled out the database or Table Structure as the problem. I don't believe that is the case here.

I am very new to this, and am having trouble stepping through and finding the root of the problem.

The form basics: (Only included what I thought was relevant code, but can add more if needed)

public partial class frmBadgeCreate : daBaseForm
{
private boBadger_History oBadger_History;
Form mainFormHandler;
private string whome;
}

public frmBadgeCreate(String cutomItem)
{
InitializeComponent();

if (daAppDesktop.IsRunning)
{
oBadger_History = new boBadger_History();
oBadger_History.GetAll(); ///  This line right here seems to have some importance
whome = customItem;
}
}

public void saveitall() /// Save Action that hangs
{
// listing of form variables to be saved to table columns
var vlast = textbox_H_lname.Text;
var vfirst = textbox_H_fname.Text;
. . .and on and on . . . 

var badger_History = new Badger_History() {hlastname = vlast, vfirstname = vfirst . . . and on and on . . };
oBadger_History.Add(badger_History);
oBadger_History.Save();  /// This is where things just hang forever.

Because this is a 'Lone Ranger App' that was handed to me, I am struggling to grasp it. What really confuses me is that when I comment out the 'oBadger_History.GetAll()' line, the save works very fast! Instantly. When I add the line back in, it hangs. I only know this, because I have spent days commenting out each line, one by one, and testing results.

The oBadger_History.GetAll(); looks like it is somehow used for the auto complete feature, so it is needed.

What has me totally scratching my head is that I can't see the connection between the 'GetAll' and the save. Why would the getAll impact the save function at all?

Here is the GetAll code, if that sheds any light:

public daBindingList<Badger_History> GetAll()
{
BadgerContext cn = (BadgerContext)this.context;
List<Badger_History> ents = cn.Badger_History.ToList();
this.EntityList = new daBindingList<Badger_History>(ents);
return this.EntityList;
}

Again, I don't believe that the SQL database/tables are the problem, seeing that I can get the save to work properly by removing the one line of code. I just can't seem to find a way to resolve

This line:

List<Badger_History> ents = cn.Badger_History.ToList();

Will load EVERY row in the Badger_History table into memory. How many records are in this table?

If there are lots of rows, then when you call Save(); (which I assume is some sort of wrapper around SaveChanges(), then EF will look through every row for anything that has changed. In your case, there may be 0 rows changed, as all you are interested in, is the row you are adding.

To speed things, you could change loading the data into a 'non-tracking' query

List<Badger_History> ents = cn.Badger_History.AsNoTracking().ToList();

This will still load in all the records, but they will no longer be counted when trying to save.

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