I am trying to persist/flush my doctrine2 entities, but I am gettig an error every time:
Entity of type Test\Main\MainBundle\Entity\ProductVariantAssociation has identity
through a foreign entity Test\Main\MainBundle\Entity\Product, however this entity has
no ientity itself. You have to call EntityManager#persist() on the related entity and make
sure it an identifier was generated before trying to persist
'Test\Main\MainBundle\Entity\ProductVariantAssociation'. In case of Post Insert ID
Generation (such as MySQL Auto-Increment or PostgreSQL SERIAL) this means you have to call
EntityManager#flush() between both persist operations.
I posted my ArticleController at pastebin:
http://pastebin.com/iN0BpGFc
Does anybody know, how to solve that problem?
Related
The full error message:
InvalidOperationException: Cannot use table 'AspNetRoles' for entity type 'AspNetRoles' since it is being used for entity type 'IdentityRole' and there is no relationship between their primary keys.
Each time I am trying to access the database using Entity FrameWork Core to read/write data I am getting this exception.
The code generating the exception could be as simple as
var p = _context.Platforms.Where(c=> c.PlatformId == 1);
I am using Identity and my database context is inheriting from IdentityDbContext<AspNetUsers>
I am using Entity Framework Core 2.2
I have read the documentation many times at http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html but I still don't get it.
What's the purpose of persiting entities?
I have the following code
$em = $this->getDoctrine()->getManager();
$user = $this->container->get('security.context')->getToken()->getUser();
if ($user) {
$user->enabled(1);
$em->flush();
}
It works very well.
Why should I add the
$em->persist($user);
before the flush ?
persisting an entity just means that the entity manager can manage the entity. otherwise it doesn't know about it.
EDIT: if you are working with an entity that has been pulled from the entity manager to begin with (in your case, $user), then persisting is not required because the entity manager already "knows" about it. So persisting is only required when creating a NEW instance.
An entity can be made persistent by passing it to the EntityManager#persist($entity) method. By applying the persist operation on some entity, that entity becomes MANAGED, which means that its persistence is from now on managed by an EntityManager. As a result the persistent state of such an entity will subsequently be properly synchronized with the database when EntityManager#flush() is invoked.
http://doctrine-orm.readthedocs.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#persisting-entities
I've created an ASP.NET Dynamic Data Entities Web Application project with visual studio 2010.
I've added a ADO.NET Entity Data Model connected to a sql Server database.
The application works fine.
I'd like to handle an exception when deleting a row in a table that has a column which is a foreign key for another table.
The exception is :
The DELETE statement conflicted with the REFERENCE constraint "FK_name". The conflict
occurred in database "NAME", table "dbo.dbname", column 'Column_name'.
What I'd like to do is display a user friendly message to explain that the operation cannot be made before other rows in other table are delete.
I did some step by step debugging, but I can't find where the application does the database request, so that I can customize the code.
Thanks.
I did some research and i found two options :
- I can handle the delete directly with gridview attribute OnRowDeleting. This mean a lot of entity manipulation
- I can handle entity framework SavingChanges and check for entities on EntityState.Deleted, then check entity navigation properties and throw an explicit message if necessary. This works with a custom validation error to show user friendly message.
How do I go about marking an entity bean (let us say that I have an "Country" entity bean which holds a record about a country that never changes) read-only on a Glassfish 3.1 with EJBs 3.1?
There seems to be a way to mark column immutable, but information is very scarce and whatever I tried didn't really work.
Any ideas?
Using EclipseLink you can use the JPA exensions of EclipseLink and put the #ReadOnly on the entity. For query you can add the query hint READ_ONLY to the query:
query.setHint(QueryHints.READ_ONLY, HintValues.TRUE);
For details on this, see http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_%28ELUG%29#Using_EclipseLink_JPA_Extensions_for_Declaration_of_Read-Only_Classes
and
http://wiki.eclipse.org/Using_EclipseLink_JPA_Extensions_%28ELUG%29#Read_Only
I have two tables
Users (Userid, Name, PhoneNumber)
Applications (ApplicationsId,UserId, ApplicationName, ActiveDate)
Every user will have more than 1 application.
In Nhibernate using lazy loading I can get the users data along with all the applications for every user. So, I used to do something like user.applications[i].Applicationname to get all the applications.
But, Now how do i retrieve all the applications along with the users data using oracle commands. I know how to get one application using joins. But, how do i retrieve multiple applications and store it in a IList. Any help is greatly appreciated. Thank you.
First you should download the Oracle Data Provider for .NET in
http://www.oracle.com/technology/tech/windows/odpnet/index.html
after you make sure that you can open a connection to your oracle database then
define mapping file of your entities in Nhibernate and map table columns of your oracle table to .NET object. after that you can use the following code snippet to get the list of your entity from database
// create the query...
IQuery query = session.CreateQuery( "from Post p where p.Blog.Author = :author" );
// set the parameters...
query.SetString("author", (String) instance);
// fetch the results...
IList results = query.List();
You should define the (Post) entity and (Blog) entity to your mapping file and as you can see (Post) has relation to (Blog) entity which is defined in the mapping file too.