0

I'm having difficulty getting this to work.

I have a product table with a version column that is of type binary fixed length 8, ConcurrencyMode=fixed, nullable=false, store generated pattern = computed.

Now when I update a product entity in the code I don't see this version column incremented. I don't see how to make the type of it timestamp in the database unless I change it in there directly or fiddle about with the metadata for the entity, but then I thought it would be enough to set "store generated pattern". I just want this to work with model first approach setting the properties of the field as mentioned.

My C# code is :

_db = new ProductsContext();

_db.Products.Load();

try
{ 
    using (var db2 = new ProductsContext())
    {
        db2.Products.Load();

        Product p2 = db2.Products.Find(3);
        p2.quantity = 2;

        db2.SaveChanges();
    }

    Product p = _db.Products.Find(3);
    p.quantity = 3;

    _db.SaveChanges();
}
catch (DbUpdateConcurrencyException ex)
{
    string a = "";
}

I don't see the exception caught if I run it in debug mode. Thank you.

Ok I found another question with a similar thing being asked and someone on there said to change the database generation template...

Concurrency timestamp column stays null with computed

I assume this is the best way of doing this as the functionality by default doesn't seem to be there. Thankyou for your replies.

indigo
  • 84
  • 6
  • 3
    What is the **table structure** in your database? In order for the column to be used as a "concurrency token", it **must be** of type `timestamp` or `rowversion` - anything else doesn't work. – marc_s Oct 01 '17 at 17:12
  • The type of that column I set in the properties for that model in the designer as binary. In the drop-down there is no option for timestamp or rowversion. This seems strange to me that by version 6 of this thing that is not possible. Is that the case? Where do I set the type of this column to timestamp or rowversion then? In the database after I generate it from the model? Doesn't seem right to me. – indigo Oct 01 '17 at 18:56
  • Wait, you're working database-first right? (Since you're talking about "metadata for the entity"). That means you're *supposed* to change the column type in the database. – Gert Arnold Oct 01 '17 at 19:26
  • No model-first as it says in the title. I mentioned metadata as that would be the only other option unless I change the database after I generate it from the model. – indigo Oct 01 '17 at 20:06
  • An [inconvenient truth](https://stackoverflow.com/a/3184628) for EF4. Not sure how much of this is still required, but Model First isn't developed so actively anymore. – H H Oct 01 '17 at 22:03
  • What about EF6? – indigo Oct 02 '17 at 03:03
  • There's nothing wrong with editing a t4 template. In EF6 it's still the same. – Gert Arnold Oct 02 '17 at 07:41
  • What is the type of the column in the **database**? If you are doing model first, the **database** is what matters first. (Not the EF property type of binary.) – Vaccano Oct 04 '17 at 13:53

0 Answers0