Scaffold-DbContext chokes on unrelated keys - entity-framework-core

When I try to scaffold models out of an existing DB2 database, I get this error:
The types of the properties specified for the foreign key {'LIST_ID' : int} on entity type 'BW_LIST_AID (Dictionary<string, object>)' do not match the types of the properties in the principal key {'AUTO_ID' : long} on entity type 'ACCOUNT (Dictionary<string, object>)'. Provide properties that use the same types in the same order.
...when the tables mentioned in the error are not even referencing each other, they are connected only by the parent of the parent of the BW_LIST_AID table, and the parent of the ACCOUNT table.
I'm using this command for scaffolding:
Scaffold-DbContext -connection "myconnstring" -provider IBM.EntityFrameworkCore -context DbcProd -outputdir .\Models -schemas PROD -UseDatabaseNames -NoPluralize -force
I should also mention that EF6 successfully managed to create models and relationships of all tables in the same database.

Related

How can I map one custom Entity to some database tables in Entity Framework?

I have a database first model in my project. The 3 tables Document, DocumentItem and Product are imported from the database.
I want to create a new Entity named Order that joins some fields of these three database tables. I created that Entity as you see in the first picture and filled its table mapping as you see in the second picture.
After the build of the project I get the following errors:
Severity Code Description Project File Line Suppression State
Error Error 3025: Problem in mapping fragments starting at line 193:Must specify mapping for all key properties (Product.Id) of table Product. EFTest C:\Users\Me\documents\visual studio 2015\Projects\EFTest\EFTest\Data\EfTest.edmx 194
Error Error 3025: Problem in mapping fragments starting at line 186:Must specify mapping for all key properties (Document.Id) of table Document. EFTest C:\Users\Me\documents\visual studio 2015\Projects\EFTest\EFTest\Data\EfTest.edmx 187
Error Error 3024: Problem in mapping fragments starting at line 193:Must specify mapping for all key properties (Orders.Id) of the EntitySet Orders. EFTest C:\Users\Me\documents\visual studio 2015\Projects\EFTest\EFTest\Data\EfTest.edmx 194
Error Error 3024: Problem in mapping fragments starting at line 186:Must specify mapping for all key properties (Orders.Id) of the EntitySet Orders. EFTest C:\Users\Me\documents\visual studio 2015\Projects\EFTest\EFTest\Data\EfTest.edmx 187
I could not find the source of the problem. How can I solve these errors?
The error tells there's a foreign key missing in your project !
Considering that you know You cannot simply add tables from the database into your model and then create a new association in the model. By default it uses independent association which must be mapped to its database counterpart(e.g the relation must exists in the database as well). You must model your relation as FK association but it allows only one-to-one and one-to-many associations. In another words, when you add a new table with "Include foreign key columns in the model" checked in EF4 and the table doesn't contain any foreign key relationships then you try to add a association it will trigger this error. The solution is to Define Constraints in an EF4 model that don’t exist in the database.
Sometimes we have unwanted conflicts which can cause such this problems when you can delete the table from the EF designer and also delete everything related to that table manually (in the XML file), then add the table again and put the right mappings again.

Entity Framework Model First and Inheritance

I am using Model First to create my database with Entity Framework 6.
I have a class as follows:
public partial class User : IdentityUser
{
}
Where User is a class generated by EF in the designer. However, how can I map the in inherited properties of User to the columns of a table?
My biggest issue is that despite the fact that I am adding the inheritance of IdentityUser to the User class/table, when I go back into the EF designer, the properties of IdentityUser do not show up.
If you are using a non-abstract base class and do not specify a [Table] on your User, and therefore your User and IdentityUser map to a single table, then you are using the TPH (Table Per Hierarchy: Enable polymorphism by denormalizing the SQL schema, and utilize a type discriminator column that holds type information) scenario.
So, it seems like this is the one you need. You don't have to do anything special in Code First to enable TPH. It's the default inheritance mapping strategy. Just map it as any other entity and you're done!
Mapping example:

Entity Framework Cardinality Issue on a 0...1 Association

I have database tables that look like this:
A Task can be mapped to a Module, or not mapped at all (0...1). I'm using Entity Framework database-first, and when I generated the model from the database, the Task entity came through with Modules as a collection (0 or more). So I opened up my EDMX and changed the "Modules" navigation property on Task to 0...1.
Now, when I attempt to compile, I get this error:
Error 3003: Problem in mapping fragments starting at line 1241:Given the cardinality of Association End Member Task, it should be mapped to key columns of the table TaskModule. Either fix the mapping or change the multiplicity of this end.
I don't understand what I need to do to fix this. I've looked at the association details and can't see the issue. I know I'm probably missing something stupid, but am totally stuck. Association properties:
Visual Studio 2010 SP1, Entity Framework 4.3.1.0, SQL Server 2008 R2.
One way to do this is to redefine the primary key for the TaskModule table. Instead of the primary key being (TaskId, ModuleName) it needs to be just (TaskId). Then do an update model from database and change any of the associations manually that didn't get picked up from that update.
Well your database schema is not correct with the description you give :
the TaskModule table implicates a many-to-many relationship, not a many-to-oneOrZero.
In edmx, many-to-many relation tables are not displayed, but they still exist in database.
So you should fix your database, or be happy with the relation proposed by EF !

Entity Framework Conditional Mapping

I have a legacy database that has a table called Address. Now two other tables can have address information assigned to it. To determine which table it came from, there is a SourceID field. If the SourceID is 1 then it is associated with the first table, if it is 2, it is address information for the second table.
This legacy database doesn't have any foreign key constraints defined on the database, and it cannot be added.
I am wondering if I use Entity Framework to create a model that will have this association. Where table 1 can have an entity that has a navigation to address information (with the condition that the SourceID =1) and same with the second table.
I have tried created a conditional mapping and have it set "When SourceID = 1" I also removed the mapping from the column mapping as the column can only be mapped once. When I try to compile, I get the following error:
Error 3004: Problem in mapping fragments starting at line 683: No mapping specified for properties Address.SourceID in Set Addresses. An Entity with Key (PK) will not round-trip when: Entity is type [Model.Address]
Thanks for your help!
Don't use conditional mapping. Map your address to the entity without SourceID property and create two derived entities from the address entity. Use SourceID as discriminator (TPH inheritance - it works same as conditional mapping but you have multiple entities with different discriminator value). Relate your first and second entity to correct address sub entity.

Entity Framework Error 3002 / 3003 - Inheritance / key mapping issue

I have a data model as follows:
A Customer has Products and Payment Methods. Each Product can be assigned any or all of the Customer's Payment Methods, with one set as default.
Foreign Keys are:
Customer.CustomerId => Product.CustomerId
Customer.CustomerId => PaymentMethod.CustomerId
Product.ProductId => ProductPaymentMethod.ProductId
PaymentMethod.PaymentMethodId => ProductPaymentMethod.PaymentMethodId
I want to customise this model for presentation purposes, Customer to have a collection of Payment Methods and a collection of Products. Products to have a collection of ProductPaymentMethods which inherit from PaymentMethod.
I deleted the association between PaymentMethod and ProductPaymentMethod, added an inheritence from PaymentMethod to ProductPaymentMethod and deleted PaymentMethodId from ProductPaymentMethod.
This is now my model:
When I save the model or build the project I get 2 errors:
Error 3002: Problem in mapping
fragments starting at line
226:Potential runtime violation of
table ProductPaymentMethod's keys
(ProductPaymentMethod.ProductPaymentMethodId):
Columns
(ProductPaymentMethod.ProductPaymentMethodId)
are mapped to EntitySet
PaymentMethods's properties
(PaymentMethods.ProductPaymentMethodId)
on the conceptual side but they do not
form the EntitySet's key properties
(PaymentMethods.PaymentMethodId).
and
Error 3003: Problem in mapping
fragments starting at line 226:All the
key properties
(PaymentMethods.PaymentMethodId) of
the EntitySet PaymentMethods must be
mapped to all the key properties
(ProductPaymentMethod.ProductPaymentMethodId)
of table ProductPaymentMethod.
What am I doing wrong?
EDIT: Having done some further Googling, I have found several solutions, most of which don't quite fit this scenario. Most talk about inheritance requiring a 1-1 not 1-many relationship. However, because of the Customer to Product 1-many relationship, the model requires a 1-many between PaymentMethod and ProfilePaymentMethod. Is it not possible to do what I am attempting?
My only answer to this so far is to have some manually created POCO classes for presentation and a Mapper class to turn my data entity into a presentation entity