Is it possible to change relationships between tables in EF6 without losing data? From my research, my understanding is that many-to-many relationships can be configured using the Fluent API in the onModelCreating method.
What wasn't clear was whether I would be able to change a table from a one-to-many relationship to a many-to-many relationship with a junction table.
There isn't an explicit migration operation to move the data around like that, what you do is:
Alter your model as desired, either through the fluent API, POCO objects or designer.
In the Up method of the generated migration class, use the DbMigration.Sql method to move the data around in beween the generated Create/Add and Drop statements. Yes, this does involve manually writing the required SQL statements.
Related
I will be using Dapper as ORM within my .NET Core web application. My only concern is we have to do relational mapping within application as well. If I already have implemented Primary Key and Foreign keys for my tables within SQL database, then will it conflict when I create relational mapping within my web application. For example, If I have a class Person has Education class with one-to-many mapping. In .NET class within Person class, I written relation
as:
List<Education> edu = new List<Education>;
Is it OK or am I doing something wrong?
Dapper plays no hand in the formation of the relationships within your app itself, in context of your classes and the relationships between them.
You can think of it as a set of database utilities to make using ADO.NET simpler. It will work seamlessly with whatever classes you design, and map your database records to you pocos for you.
It will not however hydrate your relationships for you. That will be on you to perform.
I manage complex entities, with multiple and different relationships with other entities, which themselves are linked to multiple other entities sometime.
I am making an edit form, and would like to avoid having to code manually different Doctrine 2 queries to udpate every entity linked to the modified entity.
Is it possible to cascade merge entities in Doctrine 2 ? By that, I mean modifying an entity and its linked entities (oneToMany, ManyToMany... relationships) and then applying the changes to the entity and the linked entities in cascade.
If not, what is the 'clean' way to apply modifications to all the concerned entities ? Does it need to be manually done, by calling merge or update on every entity ?
It seems you are working on Symfony (correct me if I'm wrong).
On symfony forms, when you are working with underlying objects, you sometimes need to put 'by_reference' option to 'false' in order to correctly handle the underlying modified objects.
See that : https://symfony.com/doc/current/reference/forms/types/form.html#by-reference
Hope it helps.
Most probably the problem has nothing to do with symfony but more with your doctrine relationships. You should check your associations as changes made only to the inverse side of an association are ignored by doctrine. Refer to the documentation.
I have a bigger project that uses multiple edmx files to manage the data model for an asp.net application. What I want to achieve is to link two entities accross two edmx models.
So basically, just a foreign key like this: EntityA.ForeignEntityId --> EntityB.Id
However, since the enitites are not in the same model, I cannot add this foreign key in the model designer. I know that I could do it manually by either modifying the auto-generated SQL-file or by adding the relation in the SQL Server Manager. Both these approaches have the massive downside that they are not easily maintainable because if I do "Create database from model" and run the resulting SQL script, my manual modifications are dropped (I thnink?).
So is there a way of referencing a foreign entity from another edmx model in the model designer?
I know this is old but i found myself with the same problem. If you create the reference in SQL and update your model from the database it will pull in the table that is referenced. I don't think this really solves the problem we have but it will at least allow for you to go back and forth without losing any FKs.
I'm creating a lightweight database to rent movies.
I really like LINQ, so want to stick to that. My forms need validation so that is a requirement (ComponentModel.DataAnnotations).
Is there a model tool/template/thingy that combines them all, giving me the opportunity to create classes, generate them to the database (like the ADO.NET Entity Data Model), giving me LINQ (like LINQ to SQL Classes) and form validation (letting me implement ComponentModel.DataAnnotations)
Simply use Entity Framework - it supports all you need. Check this ScottGu's article for more info.
I am wondering if it is possible to create a relationship between two entities that reside in separate databases.
For example if we took the solution found here http://symfony.com/doc/current/cookbook/doctrine/multiple_entity_managers.html and created a one to many relationship with Users in the customer database to Posts in the default database.
Is this something that is supported by Symfony2 and Doctrine?
Using different object managers (entity managers) doesn't allow the object graphs to intersect. That case is too complex and isn't managed by Doctrine ORM.
If you need such a case, keep the object graphs disconnected by saving the identifiers of the related objects (old style) instead of a reference to them, then manually get the objects through services. You can find a fairly good example of how this would work in an example of connection between Doctrine2 ORM and Doctrine2 MongoDB ODM. Alternatively, you could also use a #PostLoad event listener that populates data in your entities by creating the link through the repositories I've linked in the example. Same for #PostPersist (which should instead extract the identifiers for the related objects), but beware that this technique can become really messy.
Also, if your RDBMS supports cross-database operations on a single host, you can just use a single EntityManager and reference the other table with #ORM\Table(name="schemaname.tablename").