Entity Framework Core mismatch in column name - c#

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.

Related

Entity Framework 6 add existing complex type to key columns

I'm working on a project using EF 6 built using database-first approach. There are several tables that previously had the same 3 properties mapped to a complex type (used in several places throughout code). These properties were designated as "foreign key" columns, although their parent-child relationships were never defined in the database.
Recently, a co-worker went through and formally defined the relationships between the FKs and their parent tables in SQL Server. I am now attempting to update the entity model (update from database), and the new associations did not appear. I have resorted to removing the tables from the model and re-adding them. They now show the proper associations.
However, the mapping to Complex Type is gone. When I try to add it back, the updated mappings destroy the associations on those properties (I get a model error when saving). I am aware that Complex Types do not support associations.
Is it recommended to first remove the associations for the columns prior to re-mapping them to the complex type? Will EF observe the underlying key relationship even though the columns are not visible on the entity?
While I would still like to get some feedback on my questions, here's what I did to solve my issues.
1) Removed the associations generated by EF when deleting and re-adding tables from the database. Note that these relationships are only for the foreign key fields which were to be replaced by the complex type.
2) Followed the MS steps (found here) to refactor the requisite key properties to a complex type
3) Optional: It was necessary for me to edit the EDMX directly as XML in order to remove some lingering associations that could not be repaired with the designer
So far, my model functions as intended. I believe the FK relationships are observed during CRUD operations.

Are Junction Tables Implicitly Hidden in Entity Framework Diagrams?

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.

ADO .NET Entity Framework inheritance modelling

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.

How do I make my entity model give me access to my many-to-many tables?

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!

missing Navigation property in auto-generated entity class

I am moving my first steps in the Entity Framework 4.0, and I am currently facing an annoying issue.
The authentication/authorization process of my application is based on the standard ASP.NET membership provider, in other words the database is the well-known ASPNETDB.MDF. In this database there are - amongst others - the tables aspnet_Users and aspnet_Roles, which are linked together by the table aspnet_UsersInRoles.
I generated a new "ADO.NET Entity Data Model", I selected Generate from Database, I provided all the necessary parameters, and the wizard generated for me the relative .EDMX file. I named this "SecurityModel". In the aspnet_User entity I can see there is a navigation property that should retrieve all linked Roles, and viceversa.
At this point I added a new "Domain Service", in my case SecurityDomainService.
When I was asked, I selected the SecurityModel, and all the tables it contains.
Even in this case the wizard generated the SecurityDomainService for me.
Apparently no problems at all. However, I realized that in the entity aspnet_User I have all navigation properties (Membership, Profile, Applications, etc.) but Roles.
I read somewhere that EntityFramework doesnt handle many-to-many relationships. However I can see in my Entity Data Model that an Association exists between aspnet_Roles and aspnet_Users, and it is based on aspnet_UsersInRoles. I can also see in the Data Model designer the "Roles" navigation property in the User entity.
So, my question is why has not this navigation property been generated?
Thanks in advance for all your help.
Cheers,
G.
The problem here is that the aspnet_UsersInRoles table contains only the primary key fields of the tables in the many to many relationship. Entity Framework 'inlines' this table and does not represent it as an entity. Entity Framework handles this fine - it is RIA services that does not support this type of relationship.
Simply adding one extra field to the table will prevent it from being inlined and result in an aspnet_UsersInRoles being generated. This will be supported within RIA Services.
You will need to be careful modifying the aspnet schema to ensure that you do not break any of the stored procedures etc but the addition of a nullable bit column should not cause too much disruption.

Categories

Resources