Hi All
I am havinga problem with Entity framework, basically as you can see in the above diagram class A is an abstract class that has two classes that inherit from it B & C.
Each Class has an ID which is not a requirement but was part of the relational structure of the Database design.
I am trying to generate using the EF tools in VS 2008 a model of my Database and then model in the inheritance. I get the model generated fine but the Foreign Keys are still present in the model diagram. I delete those and replace them with the correct inheritance, I also set the Entity Set Names to be the same.
and this is where my problems begin.
The classes do not seem to be working correctly the associations are not working?
Was the removal of the FK correct or should I have left it in place and used the inheritance as well?
I need classes B and C to inherit A in my business layer.
The model doesn't seem correct and I would be gtreatful for any help in making this model work.
You should not have a (separate) FK to the PK of A.
Inheritance expects the B.Id to be PK and an FK to A.Id.
I think the problem is that you have a separate PK and FK that relates your tables. EF expects that the child tables have a single "id" column, in the child tables, that is both the PK and the FK.
Here is a snapshot of an example of a database that I have (this was generated by EF, but you see what I'm talking about):
You'll see here that Jobs is the base and the other two derive from Jobs. You'll see that the Id field in Jobs is the PK, then in the other two tables the Id field is both a PK as well as a FK back to Jobs.
Like I said, this model was generated by EF (Model first), but I'm pretty sure this is how EF wants to see things, so I don't think your database will fit into EF's inheritance structure.
Related
In my application, I have an entity, say Customer which maps to Customer. This is working fine now.
Our plan is to provide our current project as a reference to another client project. The client project also has a Customer table, but with some additional columns. Creating another Customer entity in the client project throws the error saying "both types have simple names".
Can this be solved using inheritance. Or does EF allow us to create classes with different names and map it to same table, like Customer and CustomerClient both maps to Customer table, but CustomerClient has the additional columns added.
Edit: I will try to provide the code once I am in office.
It sounds like entity framework inheritance is what you are looking for,
and especially TPH (Table Per Hierarchy) which is the the way to implement inheritance in entity frameork where all derived entities mapped to the same table.
TPT (Table Per Type) is the other option where all dervied classes are mapped to their own tables beside the base class table.
If you are looking for more info about implementing TPH and you are using EF code first this a good link to start with, and if you are using the designer you can use this.
Here is a tutorial how to choose EF inheritance strategy.
Note: I did not mention the TPC (Table Per Concrete Class) strategy which is also described in this post becuase I do not think this is what you are looking for.
This is a great video lessons tutorials that cover EF inheritance using code first and the designer and much much more.
Hope it helps!
I am trying to do a many-to-many relationship. I have a Customer that can be connected to several CouponCollections but I don't get it to work. I didn't write the code myself and haven't worked with EF that much earlier.
The current error I have, tells me "Invalid column name 'CustomerId'" in CouponCollection when I try to access it. That table has never had a 'CustomerId' column either in the database or in the object, as far as I know.
Is it possible to see how EF is handling the columns and why it thinks that this column should exist? Maybe resetting it and force it to reevaluate all columns.
On a sidenote I don't get migrations to work either. Maybe the problems are connected, but the database just won't update when I change something in the model.
From the docs
Many-to-many relationships without an entity class to represent the join table are not yet supported. However, you can represent a many-to-many relationship by including an entity class for the join table and mapping two separate one-to-many relationships.
Emphasis mine
Given your issue, you probably need to create the entity to represent the join table as described above.
I generated the edmx for my database, and that worked great -- however the problem now is that on a few of my models with multiple relationships to the same table/model have dumb names.
Such as:
Book:
User
User1
User2
Those relationships aren't very descriptive. Here are the column names:
UserId_Owner
UserId_CreatedBy
UserId_UpdatedBy
The database I'm working with has all the foreign keys written this way:
{Table}Id_{RelationshipName}
So, using that pattern, I'd like to change the edmx generation to pull out the RelationshipName.
Is there a way to configure that?
I have junction tables all over my database so that I can create many to many relationships. In one case, I have tables NotificationFormat and Frequency and connect them with a junction called NotificationFormatFrequency.
I have tried to add the junction table to the list of Entity Types, but I have not found a way. 'Update Model from Database...' does not add the junction table to the list of Entity Types, but I do see it in the Entity Store.
After much trial and failure, I looked through and noticed that none of my purely junction tables show in the diagram, nor are they available in the Entity Types to be added to the Diagram. However, in each case I see an 'Association' between my two tables, and I even see a collection of NotificationFormats connected to a collection of Frequencys.
So, expected behavior for modeling many to many relationships in EF? If so, then perhaps I don't want to clutter my diagram with the junction tables, and so maybe I don't care if there's a way to do it. Thanks in advance.
Yes, EF will create intermediate tables for you. For a given relationship, just have both ends use a cardinality of > 1 (*, 1...*).
You won't see the intermediate tables as types or in the designer, but they will appear in the actual DB.
My model turns my join tables into a many-to-many relationship in the model editor. This means that when I query a table through the many-to-many relationship to a row in the other table, I get the EntityCollection instead of IQueryable.
It is my understanding that it is more efficient to stick with IQueryable because EntityCollection loads instances of the entity classes into memory then queries those.
I'm not sure it's exactly what you're looking for, but EntityCollection<T>.CreateSourceQuery() will return a new instance of ObjectQuery, which implements IQueryable<T>.
Check out http://www.asp.net/entity-framework/tutorials/creating-a-more-complex-data-model-for-an-asp-net-mvc-application and look in the Customizing the Database Context section. There is a fluent API example of how to use join tables on many-to-many relationships. When I have a DB already defined, I like to create my own model classes and use Fluent API and Data Annotations to link them to the appropriate tables. That way I control how the application uses those tables instead of trusting the framework to build the way it thinks it needs to be built. This does two things: 1) It ensures that you are familiar with how the app uses your data, and 2) gives you more control over how data is represented in the app.
Happy coding!