This question already has answers here:
Create code first, many to many, with additional fields in association table
(7 answers)
Closed 7 years ago.
When using many-to-many with fluent api, EF creates an extra couple-table for primary keys. This table is not in DBSet<>, so how do I access this table within my code? Should I add it to DbSet<>, or should I define this table and must not leave EF to create it?
If you let the model as is, not declaring any relation entity between A and B, then you can't access that table. But maybe you don't need it, because you can use navigation properties (collections) from A to B and B to A.
So if you need all B's associated with A, you will use a.Bs, and vice versa.
You can achieve any desired query result between A and B even if you don't access the relation table, and you would need to declare that table only if you need to add extra data to the relation.
Related
This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
1:1 relationship problems with EF Model First
Is there a way to define 1:1 relationships on a Entity Framework .edmx without get this annoying:
Error 1 Running transformation: Multiplicity is not valid in Role
'PESSOACPF' in relationship 'FK_CPF_PES'. Because the Dependent
Role properties are not the key properties, the upper bound of the
multiplicity of the Dependent Role must be
*.
Already defined the FK as PK on my table, removed the pk, tried to re-create the project over 10 times and doesn't help AT ALL.
Your Foreign Key must be defined as UNIQUE.
To enforce a 1:0 or 1:1 relationship.
You can create Table in SQL DB like this (Lets take Order and OrderDetails Tables)):
CREATE TABLE OrderDetails (
DetailsId INTEGER IDENTITY NOT NULL,
orderId INTEGER NOT NULL UNIQUE,
PRIMARY KEY (DetailsId),
FOREIGN KEY (orderId) REFERENCES Order(orderId)
)
For more details Implementing one-to-zero-or-one relation in SQL Server
I hope this will help to you.
Such a task: we have 2 tables in our L2S Entity classes. It needs to manage with current fields of current tables by numbering em somehow.
Exact question is How can I point to the exact field of exact table without using entity relation names? Such as TmpLinqTable[2] instead of TmpLinqTable.TableField.
Moreover if it can be managed by ids of the entity, not the table.
So my understanding of what you are trying to do is to log changes that happen to your entites. Is that correct? You might want to look into the GetModifedMembers method on the Table class. Here's an interesting link...
http://geekswithblogs.net/steveclements/archive/2008/04/15/linq-to-sql-property-changed--changing-logging.aspx
I am trying to come up with a database design that would work with Entity Framework 4 Code First. Actually, I have no experience yet of EF4 Code First but as I understand it, if I write the code, it will create the database and tables.
The issue is this. There are various types of auctions, they all have some common fields and some specific ones. In the code I envisage having a base abstract class called Auction and subclasses like LowestUniqueBidAuction and EnglishForwardAuction etc.
Nothing surprising there. The problem is that I imagine the database structure to mimic this. I imagine an Auction table and a LowestUniqueBidAuction table and a EnglishForwardAuction table. In the Auction table I imagine a foreign key into one of these two tables for each row depending on the type of auction that that row is. I also imagine another column in the Auction table with the name of the derived auction table (such as EnglishForwardAuction).
The problem is that whenever I've ever created a foreign key I've had to specify the name of the foreign table into which the key points (which makes sense). In this case, however, there is one of many tables that the key could point. So there are many issues here. Firstly, I could simply not use a foreign key and just use an ordinary field, but then the database will not be able to maintain data consistency for me. The second issue is how will EF Code First handle this? In other words, how will it know that if I ask for all EnglishForwardAuction rows from the Auction table that it should look at the column with the table name and then join on the EnglishForwardAuction table to get the extra fields?
Has anyone ever faced similar issues?
Thanks,
Sachin
This problem is solvable in Entity Framework in a number of ways - read up on how EF handles inheritance and what strategies are available.
There are basically three strategies how to handle this:
(1) Table per Hierarchy
You have only one single table, that represents all possible sub classes. Of course, this means, several rows (that only exist in a given subclass) must be nullable, since they don't show up / don't exist in super classes or other subclasses.
(2) Table per Type
Each subclass gets its own table, and by default, the sub-types table shares the PK with the base classes' table - e.g. PK = 1 in Auction will also be PK = 1 in EnglishForwardAuction. So your subclass tables reference the base table - not the other way around.
(3) Table per Concrete Type
Each concrete subclass (your separate auction types) gets its own table, but that table contains everything - all the columns, from that specific type, but also its base type.
Read more here:
Inheritance in the Entity Framework
Inheritance and Associations with Entity Framework Part 1
Entity Framework Modeling: Table Per Hierarchy Inheritance
Entity Framework Modeling: Table Per Type Inheritance
Searching for Entity Framework Inheritance and/or one of these strategies will reveal a lot more hits, too - that topic is very well covered and discussed on the interwebs! :-)
I know how to do SQL query to add foreign keys, connect to DataBase and fetch data in C# and such. But after long hours searching on Google, I cannot find any features (or classes, methods, API etc.) in C# that has anything to do with Foreign Key.
Back when I was in Rails, if two tables has relationship I can easily access a child table through the parent table by Teacher[0].Classes.Last . But these methods seems to be missing in C#? Am I right?
I'm working with C#, SQLCE and WPF.
I have two tables that has one-to-one relationship. But all the data including the reference key are inserted manually (e.g. this row, insert a "1", that row insert a "5" etc.) and to find a rows in the child table corresponding to a parent table's ID, I just have to do an if statement in C#.
So basically a Foreign Key to me is just another int column. I don't get what the reference actually does or is it just a naming convention? Just so reader of the code sees a reference and recongnize there is a relation, but the foreign key doesn't actually do anything substantially?
Back when I was in Rails, if two tables has relationship I can easily access a child table through the parent table by Teacher[0].Classes.Last, But these methods seems to be missing in C#?
You can do something similar with Entity Framework and many other ORMs out there.
So basically a Foreign Key to me is just another int column. ... Just so reader of the code sees a reference and recongnize there is a relation, but the foreign key doesn't actually do anything substantially?
Correct - the value of the foriegn key loses significance once you leave the database. Instead what is normal is to have nested objects. For example, you can have a Customer object which can contain an Address object. The Address object may also carry around its foriegn key value, but generally you wouldn't use it in the C# code (unless you were doing something like a LINQ query with it) - you would use the foriegn key value once you got back to the database.
A foreign key represents a relationship between objects in your Domain model. It enforces integrity.
(In some databases, foreign keys can also speed up queries because the query optimiser is able to make use of this information)
I am stuck here.
Is it possible to map data from 2 different tables to 1 entity in Entity Framework 4.
I have a bunch of employees in one table, and in the other I have som project information.
I would like to combine these 2 tables in one Entity, and keep the tracking features etc., is that possible?
I do not want to use a function import, but do it solely through the Entity Model.
Can anyone help - when I try to do it, i get the following error all the time:
Error 3024: Problem in mapping fragments starting at line 2354:Must specify mapping for all key properties (MyProjectTable.PSInitials, MyProjectTable.ProjectID) of the EntitySet MyProjectTable.
Both key are mapped to their respective tables.
The new Entity are made with MyProjectTable as the basetable.
The relation between the 2 tables is a 1-*
Hope you can help.
/Christian
You cannot map two tables with a one-to-many relationship to one entity. If you don't want projecting the results into one object in code, consider creating a view and mapping it instead.
According to http://msdn.microsoft.com/en-us/library/bb896233.aspx
You should only map an entity type to
multiple tables if the following
conditions are true:
The tables to which you are mapping share a common key.
The entity type that is being mapped has entries in each
underlying table. In other words,
the entity type represents data
that has a one-to-one correspondence between the two
tables; the entity type represents an
inner join of the two tables.
The reasons for doing this are quite straightforward - for example, a table of data points that all have one of five 'types'. Obviously the 'type' will be a separate table for the sake of normalisation, but from an application point of view (working with the data) it makes more sense to have all properties in a single entity.
So we can't do this with Entity Framework - a supposed Object-Relational-Mapper. What, then, is the point of using such a framework?