How to process N-N relationship in EF? - entity-framework-4

Suppose I have 3 tables in DB for a many to many relationship:
TabA(id1, ...): Primary Key: id1
TabB(id2, ...): Primary Key: id2
TabAB(Id1, id2,..): Foreign Key: id1, id2
So when create edmx with VS 2010 from DB, I only get two entities TabA and TabB in the model because TabAB has no primary key.
How to process this case with EF?

Are you sure EF didn't just turn TabAB into a relationship? It won't appear as a table in the model if there are no other columns. EF figures out that TabAB is a join table and treats it accordingly.
If not, the best way would be to alter TabAB to have a compound primary key of both id1 and id2. If there is some reason that combination of values is non-unique, it might be good to examine why.

The common way is to handle many-to-many relationships in EF - to have three tables in storage and only two tables in conceptual model. Third table in storage contains of columns that represent the foreign keys referencing to the main tables, and the primary key of the intermediate table is built over these reference columns. Designer just hides it :)
Read more here - http://weblogs.asp.net/zeeshanhirani/archive/2008/08/21/many-to-many-mappings-in-entity-framework.aspx.

Related

How do you trigger one to many relationship between (many to many) table and a normal table?

I am trying to make one to many relationship between Contacts table and DepartmentTitle table.
I was thinking of introducing surrogate key on DepartmentTitle table so that I can reference this DepartmentTitle to Contacts table to trigger one to many relationship between these two tables. But I don't want to register same combination of the composite keys in the DepartmentTitle and that has prevented me from introducing the surrogate key to the table. I want the combination of composite keys in DepartmentTitle table to be unique.
To remedy the situation, I thought of implementing below ER diagram, where departmentTitleID would be unique and is used as reference id to the table (but is not primary key). Would this work? If not, what would be the solution?
If you're going to introduce a surrogate key, use it as your primary key. However, I would rather have Department_ID and Title_ID as separate columns in Contacts, since that allows Contacts to be joined directly to Department and/or Title as needed, without always needing to join DepartmentTitle. You can still have a composite foreign key constraint from the two columns in Contacts to the same in DepartmentTitle.

Should every table have a primary key using ADO.NET?

I'm using database-first approach with Oracle. One of my table doesn't have a primary key. It only has 2 columns which are foreign keys of other tables.
I have generated model in ASP.NET MVC project from database (Add - New Item - ADO.NET Entity Data Model).
But there is a problem - I get an error:
Error 159: EntityType 'DbModel.Store.SomeTableWithoutPK' has no key defined. Define the key for this EntityType. E:\Git_repo\ZZ\ZZ.Domain\DAL\DbModel.edmx
Does this mean that each table must have a primary key? Can I avoid this? Or I will be forced to add new column with a primary key to this table? Of course there is also possibility to apply primary key to multiple column, but is it necessary?
Every table should have a primary key for database efficiency and so that you can edit records.
You don't need to create a new column for the primary key in your 2 column table
In designer, select both columns and use both together as the primary key. As long as nulls are not allowed and there are no duplicates you should be OK.
Since this is a many to many table and you are using EF, you may find later that adding a datetime column to the table with getdate() as the default value will make data maintenance easier

Rails multitable id incrementation

I have a rails application using Postgres - I need to have some tables that have the same data in essence. So I need to have a unique key that increments for all my tables, so there is virtually a pk constraint over all the table's ids.
Now, the question is - how do I do it? can I write a migration that defines the id of all the tables to increment for each insert to any of those tables? or must I do it on the database level?
If you have such a link between two tables, you shouldn't make them have the same primary key. This is not a good use of a database.
You should instead give one of these tables a foreign key to the other one, and use this relation to identify the linked rows between the two tables.
In Rails, it's called a "has_one" relation, and it's very handy : http://guides.rubyonrails.org/association_basics.html#the-has-one-association

Entity Framework Database First - Not relating models

I'm trying to create an MVC project using Datbase First Modelling in Entity Framework. The issue I have is when I have selected the tables I wish to use, the models are created but relationships are not generated. I have read somewhere that EF does not work well with composite keys which unfortunately a number of my tables are made up of.
I tried adding the associations myself through the designer but because my table has a composite key and its mapping to a table on the first portion of the composite key to a column on another table I get an error. This is because it insists on having a mapping for the second item of the composite key
e.g.
Table 1 Table 2
D_ID A_ID
FC SomeField
Description SomeField
etc... D_ID
I need to map Table 1 and 2 based on D_ID but Table 1 is made up of a composite Key of D_ID & FC
Using database first how do I get around this?
Thanks

Join queries in Doctrine on tables without specified relations

I have two tables that do not have a relation defined with each other in the schema.yml.
However, table 1 has a foreign key reference to the primary key of table 2. Clearly, I goofed up by not designing the database well, but now it's mitigation time.
I must do a left join between the two tables coupled with a where clause that will retrieve the select rows I want. And to do this, I do:
Doctrine_Query::create()->select('t.*, l.lid')->from('Taxonomy t')->leftJoin('t.Cid c') ->leftJoin('c.Lesson l')->where('t.section = ?','Critical reading');
This should typically do it, but it does not because what it returns is all the rows from taxonomy table irrespective of the where condition. I am thinking, is this because of the relation not being specified in the column? That would be ridiculous cause the query works, only in a doctrine context it does not.
Thanks
In doctrine you can only join using the relations you defined on your schema, this is a know limitation. You may use the Native SQL feature as a workaround.

Resources