facade.edit does not change the database - facade

I am working on a JSF project that deals with MySQL DB.
in my backbean method I did:
entityFacade.edit(entity object);
after that I wanted to make sure of the changes, I queried my database to retrieve the edited record, However, the retrieved values are the old ones. it is like entityFacade.edit(entity object); was not made.
I solved this problem by doing this
entityFacade.remove(entity object);
entityFacade.create(entity object);
But the problem is that the object has ID which is auto-increment & I want to keep the old ID.
I don't get why the edit does not change values. what are the possible causes of this problem?
Looking forward to your answers, experts.

For those who are facing the same problem, I solved it as follows:
The problem was not with the facade.edit()
it was with the queries.
Try facade.find(id) to retrieve the records from the database instead of queries. The retrieved records will show the edited data.

Related

Entity Framework returns different result with AsNoTracking

I use Entity Framework in combination with an Oracle database. If I create a query like
myLinqStatement.ToListAsync()
I get wrong data returned as a result. If I change the statement to
myLinqStatement.AsNoTracking.ToListAsync()
I get the correct data.
I also checked the native SQL query, which is generated by myLinqStatement.ToListAsync(). The generated SQL query is correct, because I get the correct data.
So is there a problem in the mapping? And why is it working with AsNoTracking?
Thanks!
What AsNoTracking does is to retrieve the data without attaching it to the context, hence any changes you apply over the data do not take effect unless you attach it again so that EF knows it should track its changes.
The code snippets you've provided do not show the whole picture, from the moment a context is created, but is it possible that other parts of your code mutate data before you call myLinqStatement.ToListAsync()?
As you mention that myLinqStatement.AsNoTracking.ToListAsync() returns expected data, makes me assume that there are some side effects in your code that AsNoTracking simply is not aware so just returns whatever it finds in your db
I came across this question because I had a similar issue with Entity Framework Core querying a DB view, the issue was cause because view didn't have a key defined, after defining a key for the entity that map to that DB view, the query returned the same result in both cases (using AsNoTracking or without using it).
In T-SQL a key for a DB view can be defined this way:
CREATE UNIQUE CLUSTERED INDEX UQ_MyDBViewName_ColumnKey
ON dbo.MyDBViewName (ColumnKey);
And in code, you can map the key using the [Key] attribute in the corresponding property of the entity or using the EF fluent API. It will depend of what the project is using.
Either way, using AsNoTracking on a query that goes directly to a DB view makes a lot of sense. Also, if for some reason the query of the view does not allow us to define a key for that view, then the option is to use AsNoTracking.
Hope it helps anyone else having the same issue.

The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update

I am getting error
"The model backing the 'ApplicationDbContext' context has changed since the database was created. Consider using Code First Migrations to update the database"
I could find few answers but i do lose my data.
I am using Identity 2.0.
What is solution to this?
Try to write:
Database.SetInitializer<ApplicationDbContext>(
null
);
into your Application_Start() method in the global.asax file...
(Rename the ApplicationDbContext name to the name you use in your application)
The problem is that you're working with a model that already created the tables, then you changed the model (but no the database), so when it goes and tries to work with the database there is a mismatch between the model and the database.
I don't know if you're working with code first or not, but I suggest you to MigrateDatabaseToLatestVersion initializer which will update the database automatically to match the changes in the model you've done without loosing data.
Hope it helps. Guillermo.

prevent Linq-To-Nhibernate from updating Related tables

I'm using Fluent NHibernate, and am attempting to update all the records for entity type "Files". I have eager loaded FileTypes along with it. By practice all tables have an UpdatedTime even if they are not necessary for use.
The problem I'm having is the following classic:
SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM
This is coming from the FileType's "UpdatedTime = null" even though my update method is being called on a File.
I do not want to overwrite the null in FileType, in fact I don't want to update it at all.
It seems to me that i should be able to either elect to not include the related entities in the update via mapping, or I should be able to get it to leave that field as null instead of DateTime.min.
EDIT
Below I have provided a solution to the the datetime issue, however, it would be a superior answer to prevent the second table from updating in the first place.
The answer turned out being relatively simple after more messing around. I changed the domain to use DateTime? instead of DateTime.
While this solved the problem, It is a bit 'hacky' and I would prefer to not update the second table.

Model changed during database created

I have uploaded my MVC3 project , it's s simple blog , at first it works well but after couple hours! following error appears (I've made custom error to Off to see the error)
The model backing the 'SiteContext' context has changed since the database was created. Either manually delete/update the database, or call Database.SetInitializer with an IDatabaseInitializer instance. For example, the DropCreateDatabaseIfModelChanges strategy will automatically delete and recreate the database, and optionally seed it with new data.
to solve this I have to manually delete my database and create again and then restore to the backup that I have created. but after after 2 hours again I get the error!
I really don't have any idea , what caused that ??
When you create a model and ask EF to create a database from it, EF would hash the model and store the hash value with the database. Whenever the context is created, EF recomputes the hash and matches it against what is stored at the database. If the model changes in any way, the resulting hash will be different and EF will throw the exception you have just seen. This is done in order to keep the model in sync with the database.
Is there any way the model could have changed during runtime?
One thing you could do to figure out the difference is to
1.Re-create the database from the model as you are doing now and get it scripted (script1.sql).
2.Wait till the error happens and delete the db and re-create it again and script it (script2.sql)
3.Try to compare the two and see whether you can spot a difference in the schemas.
This should give you an idea of what has changed in the model.
Goodluck

mutual exclusion in joomla

I created an extension for joomla using:
$id=$database->insertid();
I just covered that if two users are logged on to the site will fit together perform two records in the database and then this statement will return in both cases the same value.
in php you can solve this problem with the transactions.
In joomla how do I solve this problem?
If you have a table you are working with that extends JTable then make sure that you included the check out functionality that is optionally a part of that. THis must means adding a couple of fields like what is in the content table. This will prevent two people from editing the same row at the same time which creates a race condition in which one of the other will lose their data.
Please note that both php and joomla functions to return the last insert id rely on the mysql implementation, and mysql returns the last id inserted on the currently open connection so concurrency is not an issue
#iacoposk8 Your are right it might possible that in very rear case. Such time try to add current logged in user id in your sql query or any where so that it doesn't make any confict. I hope you get it what i want to say. Thanks

Resources