Get Key After Identity Insert

Posts   
 
    
Sam avatar
Sam
User
Posts: 95
Joined: 30-Jun-2004
# Posted on: 17-Nov-2004 01:04:05   

I have a table with an auto increment identity column. So, I create a new entity and populate it with info (but not the key). Then using the adapter method I call the SaveEntity method. Everything works fine except I need to know the auto increment key that sql server assigned to the newly inserted col. Is there anyway to do this?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 17-Nov-2004 09:43:15   

Sure, just read the property simple_smile

Example: CustomerEntity c = new CustomerEntity(); // fill c's properties, except the autonumber CustomerID field adapter.SaveEntity(c);

int customerID = c.CustomerID;

Frans Bouma | Lead developer LLBLGen Pro
Sam avatar
Sam
User
Posts: 95
Joined: 30-Jun-2004
# Posted on: 17-Nov-2004 19:33:44   

That is what I thought but here is my problem: As stated before I am using the adapter method. I have also written a class that looks like this:


    public class ExpenseReportEntity:DAL.EntityClasses.ExpenseReportEntity
    {
        
        private DAL.DatabaseSpecific.DataAccessAdapter mAdapter = new ExpenseReimbursment.DAL.DatabaseSpecific.DataAccessAdapter();
        
        public ExpenseReportEntity():base(){}
        public ExpenseReportEntity(int Expensereportid):base(Expensereportid){}
        public bool Save()
        {
            return mAdapter.SaveEntity(this);
        }
        public bool Delete()
        {
            return mAdapter.DeleteEntity(this);
        }
    }

So in my PL Code I this is what I do


            BLL.EntityClasses.ExpenseReportEntity exr = new ExpenseReimbursment.BLL.EntityClasses.ExpenseReportEntity();
            exr.EmployeeId = SessionManager.Instance.Employee.EmployeeId;
            exr.SupervisorId = SessionManager.Instance.Employee.SupervisorId;
            exr.Costcenterid = int.Parse(ddlCostCenters.SelectedValue);
            exr.Approvalstatusid = "n";
            exr.Description = txtDescription.Text;
            exr.Save();
            lblResults.Text = exr.Expensereportid.ToString();

lblResult is 0. Baically after calling the save method my object is not getting updated. I know that is is not an LLBLGen probem but rather a desigen problem on my part. Hoping you could help.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 17-Nov-2004 20:31:51   

Well, it shouldn't be 0 if the save succeeds, so please check if Save() returns true.

if it does, then I've to ask you if you're using a database other than SqlServer or access. This is important as you have to specify the sequence in the designer for firebird and oracle for each entity.

I assume you're using the latest runtime libraries as you don't specify true for refetch after the save but don't get exceptions when reading the ID field back.

Frans Bouma | Lead developer LLBLGen Pro
Sam avatar
Sam
User
Posts: 95
Joined: 30-Jun-2004
# Posted on: 17-Nov-2004 21:15:11   

Using SQL server 2000. Newest LLBLGen versions (pretty sure) disappointed . The save method returns true and the entity is added to the database. The only problem is that the entity is not updated. I think the problem is that it is updating the wrong reference of the entity or something. Does this BLL look like a common/correct implementation?

That said it just started working correctly???? I don't think that I changed anything but I did do a refresh on the database and regenerated the code??? Sorry to waste your time. But I was curious about your thoughts on my BLL implementation. Does this look like "good" design?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 18-Nov-2004 10:18:09   

Sam wrote:

Using SQL server 2000. Newest LLBLGen versions (pretty sure) disappointed . The save method returns true and the entity is added to the database. The only problem is that the entity is not updated. I think the problem is that it is updating the wrong reference of the entity or something. Does this BLL look like a common/correct implementation?

That said it just started working correctly???? I don't think that I changed anything but I did do a refresh on the database and regenerated the code??? Sorry to waste your time. But I was curious about your thoughts on my BLL implementation. Does this look like "good" design?

It could be your initial generated code had the entity field not flagged as being an identity field, then you changed it in the database but you didn't update the generated code/project.

What you effectively did was making adapter act like selfservicing wink . It can be handy to do it this way, but in general it isn't in line with the philosophy behind adapter which is: persistence logic as a service: apply a service to an entity. My question to you is then: why did you pick adapter and changed it into selfservicing? (just curious)

Frans Bouma | Lead developer LLBLGen Pro
Sam avatar
Sam
User
Posts: 95
Joined: 30-Jun-2004
# Posted on: 18-Nov-2004 18:37:38   

The reason that I used adapter was for future expansion. It is possible that I will use these methods in a web service or something. With the adapter it seems like it will be easier to separate out businsess and data access logic. It is also possible that I will be needing access to more than one database in which case the adapter method is a better fit. Also I wanted to isolate the business logic. I don't call generated DAL code directly but call inherited BLL classes. (so pretty much I am calling the DAL code but in the future I could change this right now I am passing entities across the tiers.simple_smile