My problem is simple but I can't seem to get EF to understand my database schema (Oracle).
Problem
I want to model more than 1 M-M relationship see diagram below:
As you can see I have a book and a journal - and I want to store a URI against both of these, by way of using a linking table (junction table, intersection table, you get it!). In this case it happens to be "www.google.com" that I want to store against Journal 16 and Book 75, they would be stored in the linking table in the follwoing format marked out in blue. For completeness I have added a green example for link with an ID of 5.
So you can see how the data knits together, I think you'll agree it's nothing too crazy.
Anyway, when I try and update EF model, it complains (in Visual Studio) with an error along the lines of.
Problem in mapping fragments starting at line 210:Foreign key constraint 'REF_URI_JOURNAL_FK1' from table REF_URI (REF_ID) to table JOURNALs (REF_ID): The columns of table REF_URI are mapped to AssociationSet BOOK_URISet's End BOOKs but the key columns of table JOURNALs are not mapped to the keys of the EntitySet BOOKs corresponding to this End.
Yeh - not really getting that error message!
I can get EF to work with 1 M-M relationship, e.g.
and it all works "hunky dory", but when I added a second M-M relationship it fell over with that error message above e.g.
Anyone know how I can get round this?
For two many-to-many relationships your need two linking tables (junction tables, intersection tables) REFBOOK_URI and REFJOURNAL_URI. I think that is what the exception is saying in a cryptic fashion. If you only had one table it would mean that if Journal 123 links to URI 789 then also Book 123 must also link to URI 789 because the REF_ID column in the link table would be a non-nullable foreign key to both the Journal and the Book table.
Related
I am working on an API web project, in ASP.net, and on the recommendation of several stackoverflow users, with a description of my problem, I have decided to use EntityFramework.
Work on a database already created, and formed, with all relationships. I have already generated all the corresponding entities, and do the first sql, to get a small example ql that will return all the data of a given table.
But when I get the query and execute it, I get the error 3002 with the following description:
error 3002: Problem in mapping fragments starting at line 12441:Potential runtime violation of table propuestaMarca's keys (propuestaMarca.marca, propuestaMarca.propuesta): Columns (propuestaMarca.propuesta, propuestaMarca.marca) are mapped to EntitySet propuestaMarca's properties (propuestaMarca.propuesta.cod_propuesta, propuestaMarca.marca.cod_marca) on the conceptual side but they do not form the EntitySet's key properties (propuestaMarca.marca.cod_marca, propuestaMarca.propuesta.cod_propuesta, propuestaMarca.propuesta.empresa).
When I review the .emdx model, I see the following:
The table propuestaMarca does not exist in the model. It is not exported correctly
When I review the .emdx model, I see the following:
The proposed tableBrand does not exist in the model. It is not exported correctly.
I have searched for information about error 3002, and I have found that removing a certain primary key from one of the two tables solves the problem, and had tried to eliminate the primary Empleado key in the propuesta table, but it has not worked.
This error can also occur when the primary key is made by the combination of more than one attribute (composite key). It looks like you have two "keys" in the propuesta table.
From the error message above:
(propuestaMarca.propuesta, propuestaMarca.marca) -> 2 columns
are mapped to EntitySet propuestaMarca's properties
(propuestaMarca.propuesta.cod_propuesta, propuestaMarca.marca.cod_marca) -> 2 columns
on the conceptual side but they do not form the EntitySet's key properties
(propuestaMarca.marca.cod_marca, propuestaMarca.propuesta.cod_propuesta, propuestaMarca.propuesta.empresa). -> 3 columns
So I think you should check the mapping.
I have two tables, one containing patient information, the other, the notes for each patient.
(One patient, many notes for a patient).
Given this, in the Designer (which you access by right-clicking on the chosen DataSet), how do I create a one-to-many relationship? I have never performed this before.
Secondly, for the patient notes table, how would I add a note to a patient record using SQL syntax? Note, this is not updating an existing one, but adding a completely new one to the patientNotes table using the unique patient ID number as the reference (so only that specific patient has that note added to them, not them and everyone else).
Very technically speaking, you don't need to do anything to create a one-to-many relationship. You just have to have the two tables set up as you have them and use them as you intend on using them. I work in data warehousing and unfortunately a great many of our relationships like this are not formalized with any sort of key or constraint.
The correct way to do it is to implement a foreign key constraint on the patient ID column on the patientNotes table. A FK will only allow you to insert data into patientNotes IF the patient ID exists in the patient table. If you would try to insert a note into your table that has a patient ID that doesn't exist in the patient table, the insert would fail and the SQL engine would give you an error. Note that the column on the patients table that you are creating the FK to must be a primary key.
Inserting data will really go as any other insert would:
INSERT INTO dbo.patientNotes (patientId, NoteText)
VALUES(4265, 'During his 8/14/2014 visit, Mr. Cottinsworth complained of chest pains. Evidently he has been wearing a lady''s corset to hide his large gut. Advised the very portly Mr. Cottinsworth to discontinue corset use'
You could toss that in a SP, put it in your code and use parameters for the patientId and NoteText, however you wanted to do it.
As far as doing this all in Visual Studio graphically, I can't be of much help there. I typically use the TSQL editor and type out what I want to do to the DB. I'm sure there are tutorials abound on how to set up FKs on Visual Studio.
Further reading:
http://msdn.microsoft.com/en-us/library/ms189049.aspx
http://www.scarydba.com/2010/11/22/do-foreign-key-constraints-help-performance/
what are the advantages of defining a foreign key
I'm trying to design a snackbar automation system (to answer the first question - No, it's not a homework, it's for learning purposes) and I have an issue with with the recipes and how to present them in the database. I have two options:
Option 1:
[Ingredients] -> [IngrID, IngrName]
[Recipe] -> [RecipeID, RecipeName]
[IngRecipe] -> [IngrID, RecipeID]
In this case the third table is a typical many-to-many table, the model looks correct and it's a piece of cake to manipulate with the data using Entity Framework. But I want to keep track of the amounts as well. Generally I use Ingredients as a table to insert the new purcheses. If the ingredient exist, just update the amount.
Option 2
Now if I add column "amount" as a column to IngRecipe, the whole idea of many-to-many table vanishes and I no longer can use the entity model to fill the fields automatically. But I can't seem to find a more apropriete place for this column. Where and how will I say "Well, get me 100 gr of chicken breast and add it to whatever recipe"
Any help is appreciated. Thanks in advance!
It's a solid model start, consider:
RecipieIngredients -> Recipe (FK), Ingredient (FK), IngredientQuantity
Key over (Recipe, Ingredient)
Note that it is still a M-M relationship (the quantity is not part of the PK nor involved in a FK), just with more relevant data for this relationship pair. The names can be changed, but at some point, this must be represented as a M-M relationship in a normalized relational model.
Don't let the framework ruin a good normalized design - and I hope EF can cope with such trivial scenarios; even LINQ2SQL can.
I'm using an Oracle datasource, reading data only, not writing, I can not alter anything on the database.
Imagine this scenario of 2 tables, a Courses Table and a Memos Table.
The Courses table has 2 links to the memo (MemoId) table.
So...
Course table
CourseId
Title
SummaryNarrationMemoId
PreCourseInformationMemo
MemoToString table
MemoId
MemoData (Binary)
So the course table joins to the Memo Table twice, for different reasons.
I looked down the EF Inheritance route for using the same table twice, using the Memo table as the base, and creating PreCourseMemo and SummaryNarrationMemo, but that throws these errors on build:
I updated the model to add the two tables to the model.
Error 3005: Problem in mapping fragments starting at line 613:Must specify mapping for all types in Set MemoToStringTables.
An Entity with Key (PK) will not round-trip when:
Entity is type [MyModel.MemoToStringTable]
Error 3005: Problem in mapping fragments starting at line 613:Must specify mapping for all types in Set MemoToStringTables.
An Entity with Key (PK) will not round-trip when:
Entity is type [MyModel.PreCourseMemo]
Error 3005: Problem in mapping fragments starting at line 613:Must specify mapping for all types in Set MemoToStringTables.
An Entity with Key (PK) will not round-trip when:
Entity is type [MyModel.SummaryNarrationMemo]
I know I could create a view for each on the database, but it doesn't feel right. :( Any thoughts?
If I understand your problem correctly, it will not work. You are trying to map inheritance to the Memo table but to do that you Memo table must contain special discriminator column which will tell EF if it is PreCourseMemo or SummaryNarrationMemo. You cannot differ those types just by relation to course.
I'm trying to create a 1:m relationship using Entity Framework (.net 4.0) and am getting the following error:
App_Code.Model.msl(36,6) : error 3007: Problem in mapping fragments
starting at lines 6, 36:Column(s) [ProductId] are being mapped in
both fragments to different conceptual side properties.
What i have is a Products table, and a Features table. The idea is that Products have many Features. Products each have a ProductId, and the Features have a ProductId foreign key.
Now the catch is that the foreign key doesn't exist in sql server, and i don't want it to. If it did, then it all automagically works nicely.
In the EDMX designer, i created an association from the product to the feature entity, then edited the mapping details of the ProductFeature association to be based on the Features table, which i think would make it work.
Any ideas? Thanks very much.
This is a M x N relationship. Why? Because a feature can be assigned to more than one type of product.
You should have a table ProductFeatures like so:
ProductId FeatureId
1 1
1 2
2 1
2 2
Found one solution: delete the scalar property 'ProductId' from the feature entity:
http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/a71901fb-97ec-4072-949a-c0c66a9775b1
However, in the auto-generated relationships that EF gives you if you set up the foreign key in the database, the eg 'ParentId' fields are present in the child as a scalar field.
So i'm a little confused still.
-edit- Further help:
http://www.hanselman.com/blog/CreatingAnODataAPIForStackOverflowIncludingXMLAndJSONIn30Minutes.aspx