0

I have a function to writes a bunch of "ImageSignatures" to a DbContext:

using (var db = new ImageContext())
{
    foreach (var file in files)
    {
        var sig = new ImageSignature
        {
            FileName = file,
            Signature = Signature(file),
        };
        Console.WriteLine("{0}: {1}", Path.GetFileName(sig.FileName), Sig2Str(sig.Signature));
        if (sig.Signature != null)
        {
            db.Images.Add(sig);
        }
    }
    try
    {
        records = db.SaveChanges(); // where the heck is this saving to!?
    }
    ...

Where the Signature property is defined as

[MinLength(420)]
[MaxLength(420)]
[Required]
public sbyte[] Signature { get; set; }

If I put a breakpoint just before I Add the sig, I can see that it's not null, but a 420 byte array as I expect.

On a later run of the application I try to loop over the ImageSignatures I inserted,

foreach (var img1 in db.Images)
{
    var set = new List<string> { img1.FileName };
    foreach (var img2 in db.Images)
    {
        if (Distance(img1.Signature, img2.Signature) < 0.6)
        {
            set.Add(img2.FileName);
        }
    }
    if (set.Count > 1)
    {
        dupeSets.Add(set);
    }
}

But Signature is always coming back as null. What happened to it? How did it become null, when it wasn't null when I saved it?

mpen
  • 272,448
  • 266
  • 850
  • 1,236

1 Answers1

0

Make sure you have saved the changes to your DbContext before disposing it which will ensure that those changes got persisted in the underlying data store:

using (var db = new ImageContext())
{
    foreach (var file in files)
    {
        var sig = new ImageSignature
        {
            FileName = file,
            Signature = Signature(file),
        };
        Console.WriteLine("{0}: {1}", Path.GetFileName(sig.FileName), Sig2Str(sig.Signature));
        db.Images.Add(sig);
    }
    db.SaveChanges();
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • No...that was a bit further down in my code. I cut it out for brevity. The filenames are coming back fine, it's just the signatures that are null. – mpen May 23 '13 at 06:21
  • What if you try with a `byte[]` instead of `sbyte[]`? Also what's the underlying column type in your database? What happens when you used the SQL profiler? What SQL query did the `db.SaveChanges()` generate? Was there any trace of the image? – Darin Dimitrov May 23 '13 at 06:22
  • I can't use `byte[]`. My data contains negative numbers. I haven't the faintest clue what it's represented as in the DB, as [I still haven't managed to find the DB](http://stackoverflow.com/questions/16706852/where-is-my-database-and-how-do-i-view-it). – mpen May 23 '13 at 06:23
  • I can see now that my database has 133 entries in it, but it only has 1 column, "FileName". The "Signature" column is completely missing from the DB. – mpen May 24 '13 at 01:11