0

I´m using an existing database from our ERP.

In all my database tables, there is a float field called "r_e_c_n_o_", but this field is not auto-incremented by the database and I can´t change it.

For all added entities I would like to increment this field "r_e_c_n_o_", how could I acomplish that in DbContext´s SaveChanges() method?

Using ADO.NET I´d do something like that:

    public static int GetNext(string tableName, string fieldName)
    {
        var cmd = _conn.CreateCommand(string.Format("SELECT MAX({0}) + 1 FROM {1}", fieldName, tableName));
        var result = (int)cmd.ExecuteScalar();
        return result;
    }

UPDATE: Please take a look in the comment below, its just what I need to solve my problem:

    public override int SaveChanges()
    {
        var entries = this.ChangeTracker.Entries();
        Dictionary<string, int> lastRecnos = new Dictionary<string, int>();
        foreach (var entry in entries)
        {
            var typeName = entry.Entity.GetType().Name;

            if (lastRecnos.ContainsKey(typeName))
                lastRecnos[typeName]++;
            else
                lastRecnos[typeName] = 0;//How can i get the max here?

            int nextRecnoForThisEntity = lastRecnos[typeName];

            var entity = entry.Entity as EntityBase;
            entity.Recno = nextRecnoForThisEntity;


        }
        return base.SaveChanges();
    }

Tks, William

will
  • 1,002
  • 2
  • 9
  • 20
  • That is pretty dangerous because if concurrent executions happens prior to inserting a new record you will get same values (+ SQL injection danger). – Ladislav Mrnka Jun 05 '12 at 12:20
  • I know that, but I have no choice....the third party company that provides our ERP use this approach, they don´t use auto-incremented fields at all.. So, i need to follow the same approach in my application..... What would be the best way to implement that? I need to do that using EF-codefirst. – will Jun 05 '12 at 12:22
  • Correct approach requires intensive locking on your tables allowing only single transaction to read value and insert record in atomic operation. – Ladislav Mrnka Jun 05 '12 at 12:32
  • Ok, i understand the risks of doing that..... but I need to... All my entity classes inherits from a base class that has a "Recno" property... in the dbcontext on SaveChanges() methods, I would like to set this property with the 'max value' + 1 for all added entities, how can i do that? – will Jun 05 '12 at 12:45
  • If you need to know where to put your logic check [this answer](http://stackoverflow.com/questions/5275306/does-entity-framework-4-code-first-have-support-for-identity-generators-like-nhi/5277642#5277642) but you still have to solve the concurrency issue to enforce uniqueness otherwise you can corrupt your ERP database = the best way to get fired. Also be aware that in `SaveChanges` you have to set all ids prior to inserting any record to the database. – Ladislav Mrnka Jun 05 '12 at 12:52
  • I updated my question, please take a look. – will Jun 05 '12 at 13:11
  • What kind of application are you creating? Dictionary is not thread safe and incrementing value in dictionary is not thread safe as well and if your database can be use by another process it will not work at all. – Ladislav Mrnka Jun 05 '12 at 13:24
  • DBContext is not thread safe either.....so we have no problem with dictionary. Its a client/server application, so my services and data layer is in the server side, I´m using WCF. I´d like to know how to get the max of an property to each entity dynamically. – will Jun 05 '12 at 13:43

0 Answers0