getting Greendao to return object on insertOrUpdate? - greendao

When using the insertOrUpdate method in Greendao, is it possible to return the inserted object. I basically want to get at the newly created key.
I see references to rowId but i don't think that is going to help me...

You can use the returned rowId to get the entity with: EntityDao.loadByRowId(rowId). From the entity you can then get the key with EntityDao.getKey(entity).

Related

Dynamically Selecting Data from Table in Entity Framework

I know this question might have been asked before but I have not found a single answer yet.
Basically I am using entity framework and I am in need of selecting data from a database without knowing the name of the table, as this will be generic.
Now if I do not know the table name, I do not know the type too as I have tried using context.Database.SQLQuery or context.Database.ExecuteSQLStatement but these all require the type of the object it should be expecting.
All I am receiving as parameters are the name of the table and the row ID.
Could anybody give me further advice?
Thanks.
#
Edit:
I have just been notified that the only property I would need from this table is the Name field...

Entity Framework 4.1: HOWTO know the next identifier assigned by Database automatically

I have POCO objects which their identifiers are unique and generated automatically by the database, so the problem is when you want to know for some reason which will be the next identifier that the database is going to assign to the next record you are inserting. As far as I know it is only possible after performin dbContext.SaveChanges() so I would like to know if I am right or is there a way to know the next identifier assigned by database automatically.
is there a way to know the next identifier assigned by database
automatically
Well, the next one NO. And if your code depends on it, you really need to change your design.
If you need the identifier to insert related objects, you should check some other questions because you can assign entities to eachother instead of ID's and it will be fine.
I agree with the general purport of the comments that having to know an identity value is "smelly". But on the other hand sometimes you have to live with a given design.
You can't really get the value of the next id, but you can get the value of the assigned id in time by using a TransactionScope.
using (var trans = new TransactionScope())
{
// Create new object
...
context.SaveChanges();
int id = newEntity.Id;
dependentEntity.IdString = string.Format("{0:0000000}", id);
context.SaveChanges();
trans.Complete();
}

Violation of unique key constraint while inserting row using EF 4

My object which I am going to insert has a parent object as a navigational property.
When I "add" the object to insert it, it also set the ObjectStateManager of my parent object (which is already inserted) to Added and try to insert it. I've verified it in SQL Profiler and thus raises the exception of unique key violation.
I am getting rid of this in two ways
Before adding the object I set all the navigational properties to null
Set the ObjectStateManager of parent object to Modified.
But this seems more like hack than a solution. I believe Entity Framework must have some elegant solution to this.
Kindly suggest.
The second approach is correct solution for this problem. When you call AddObject EF will attach all entities from the object graph in Added state. If you also have existing entities in the graph you must tell EF about them by either setting their state to Unchanged or Modified.

Cannot insert NULL into a non-identity column in Entity Framework. Funny thing... it's not null?

I have a SalesOrder table with columns for ID and OrderID. ID is an auto-generated int. OrderID is a non-nullable string with a max length of 20, and we use it to store the customer's order number for reference.
After adding my new SalesOrder and calling SaveChanges, I get the following error:
Cannot insert the value NULL into column 'OrderID', table 'SalesOrder'; column does not allow nulls. INSERT fails.
The statement has been terminated.
Problem is, the object that I'm saving actually does have an OrderID! It's almost like it's trying to save the entity first before it saves all the values. Is this how EF handles things?
My setup is EF4.1, using an EDMX model-first approach. StoreGeneratedPattern is set to None. Default Value is currently set to (None) but I've tried various values. Entity Key is False, since it's not part of the key. I've also tried deleting the SalesOrder entity and regenerating it from the database.
I would also like to see your code...I had similar problems when filling objects in a loop then saving them with savechanges. I thought all the fields were populated, but they were not.
I'd have to see your code that executes before the save changes before I can offer anything really helpful.
If your problem is like mine and you are calling savechanges after using an iterator to populate your objects, then you can find the bad data by moving savechanges into the iterator so that it is called with each iteration...but this is all hypothetical guesswork without seeing your code...

MyBatis: How to return the ID of the inserted object under Postgres?

I've got a postgres table where the ID is defined as bigserial. How
can I use #Insert and get back the id of the inserted entity? I am
expecting the mapper method to either return the id or populate the id
field inside the entity object. Any ideas?
The mapper will return you the number of records that were actually inserted.
In order to get back the id of the inserted record, you'll need to add a second annotation (that will populate the id) :
#Options(useGeneratedKeys=true, keyProperty="idSomething")
Note that keyProperty is not necessary if the identifiyng property is named "id" in your entity object.
NVM, i think i found the answer on the other thread,
http://mybatis-user.963551.n3.nabble.com/How-to-return-the-ID-of-the-inserted-object-under-Postgres-td1926959.html
There's the link for anyone else who lands here.