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.
Related
I have the following Table Structure
Facility
PK Facility ID
AccountID
Accounts
PK NameID
PK AccountID
I can't touch the DB so my changes need to be in Entity Framework. Essentially the AccountIDs are linked so I want to create an association between them. So when I create an association I map the AccountIDs together, however I can't map FacilityID to anything and NameID to anything so when I save Visual Studio complains that the mapping is not set correctly.
My main question is how do I ignore the mappings for FacilityID and NameID? I've tried added [NotMapped] to both FacilityID and NameID but that does not work. I've also tried creating a scalar property for Facility and Accounts and used the Referntial Constraint to map them however when I try to map the columns under Table Mapping, the columns I added do not show up which causes VS to complain as well.
Here is my table, I removed most of the fields because they are unnecessary
Assuming Account.AccountID is unique (ie no two rows in Account actually have the same AccountID), just declare that as the only Key Property on the Account entity.
The Key of an entity does not have to be declared as the PK in the database. But you can only have one Key per entity (the Key can, of course, have multiple columns, and EF Core does support alternate keys). The entity Key should be unique, and should have a unique index in the database on the corresponding columns, but that's not enforced by EF.
I have a program using Entity Framework (EF) Database First in the data access layer. I know that in order for EF to auto-generate the navigation properties of a many to many relationship, the mapping table needs to contain only a composite primary key:
Project
- ProjectId (PK)
- Name
ContentType
- ContentTypeId (PK)
- Name
ProjectContentTypeMapping
- ProjectId (PK)
- ContentTypeId (PK)
In this case everything works fine, and I can access Projects from ContentTypes and the other way around with navigation properties.
However I have a requirement to have extra fields that are particular to the relation between Projects and ContentTypes, and that would be extra columns in the ProjectContentTypeMapping table. Once I add these I loose the navigation properties, and EF shows the mapping table in the designer.
Is there any way I can manually configure the mapping between these two tables in EF (Database First)? Alternatively, how can I represent this? I was thinking of maybe having an extra "metadata" table with a FK to the mapping table, but it looks "hacky" to me...
Thanks
No, you cannot have extra columns in your mapping table in entity framework.
Because, having an extra column implies you intend to use it, but Mapping tables are not part of the entity, so, entity framework, no longer treats your mapping table with extra columns as a Mapping table. You will have to manipulate the Mappings manually.
Lets take example of your classes:
Project
- ProjectId (PK)
- Name
- ProjectContents
ContentType
- ContentTypeId (PK)
- Name
- ProjectContents
ProjectContentTypeMapping
- ProjectId (PK)
- ContentTypeId (PK)
- OtherRelevantColumn
where ProjectContents is of type ProjectContentTypeMapping
so whenever you add a Project with Certain ContentType, you would be doing this:
Project prj = new Project();
//fill out scalar properties
ProjectContentTypeMapping pctm = new ProjectContentTypeMapping();
pctm.ContentTypeId = 1; //or whatever you want, or selected from UI
prj.ProjectContents = new ProjectContentTypeMapping();
prj.ProjectContents.Add(pctm);
dataContext.Projects.Add(prj);
Now in case of editing (adding ContentTypes) to an existing project, you won't do prj.ProjectContents = new ... rather, you would do it only when it is null i,e,
if(prj.ProjectContents==null)
prj.ProjectContents = new ProjectContentTypeMapping();
also, one very very important thing, since now your ProjectContentTypeMapping is no longer a mapping table, so, deleting a Project would give you an error, if its id is being present in ProjectContentTypeMapping table; you will have to remove them manually i.e
foreach(var pctm in prj.ProjectContents.ToList())
{
prj.ProjectContents.Remove(pctm);
datacontext.ProjectContentTypeMappings.Remove(pctm);
}
datacontext.Remove(prj);
datacontext.SaveChanges();
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.
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
I have a database with multiple tables, and some basic relationships. Here is an example of the problem I am having:
My Database:
**Org**
ID
Name
etc
**Detail1**
ID
D1name
**Org_Detail1**
Org_ID
Detail1_ID
**Detail2**
ID
D2Name
**Org_Detail2**
Org_ID
Detial1_ID
BooleanField
My problem is, the Org_detail1 table is not showing up in the entity model, but the Org_Details2 table does.
I thought it may have been because the Org_Detail1 table only contains two ID fields that are both primary keys, while the Org_Details2 table contains 2 primary key ID fields as well as a boolean field.
If I add a dummy field to Org_detail1 and update it, it still won't show up and wont allow me to add a new entity relating to the Org_Detail1 table. The table won't even show up in the list, but it is listed under the tables.
Is there any solution to get this table to appear in my model?
Seems like I may just need to completely delete the model and recreate it. Adding dummy fields is the only solution I've found.
Not sure this is an MVC problem.
Does a Detail1 collection turn up in your Org entity and an Org collection show up in your Detail1 entity. This is the normal behaviour for Entity framework for a many to many intersecting table with no other tangible data.
If not then maybe the foreign key constraints are not defined.