user610217
user610217

Reputation:

EF Frustration mapping a view to an object

I have a table that hold records of part numbers for a specific project like so:

create table ProjectParts
    (
    PartNumber varchar(20) not null,
    ProjectID int not null,
    Description varchar(max) not null,
    primary key nonclustered (PartNumber, ProjectID)
    );

I have a view that will collect inventory information from multiple places, but for now I basically have a skeleton:

create view ProjectQuantities as
select distinct
    pp.PartNumber,
    pp.ProjectID,
    0 as QtyOnHand,
    0 as QtyOnOrder,
    0 as QtyCommitted
from
    ProjectParts pp;

So far, so good. I go into EF designer in Visual Studio (I already had an object model using the ProjectParts table) and update the model from the database. I select the ProjectQuantities view, click ok.

EF tries to divine the key on the table as a combination of all columns, but I fix that so the key for the object is the PartNumber and ProjectID columns. I check to make sure this validates, and it does.

Next, I add an 1:1 association between the ProjectPart object and the ProjectQuantity object in the EF UI and click OK. Now, when I try validating, I get the message Error 11008: Association 'ProjectQuantityProjectPart' is not mapped. Seriously? It can't figure this out? Alright, I select the link, go to the Mapping Details, and add the ProjectParts table. It adds both tables and meshes up the key relationships. My job is done. I run the validation.

No luck for me. Now I get the error Error 3021: Problem in mapping fragments starting at line (line number): Each of the following columnes in table ProjectParts is mapped to multiple conceptual side properties. The the message lists the ProjectID and the PartNumber columns and their references to the association I just created.

Well duh! Of course there are multiple references! it's a 1:1 compound key, it has to have multiple references!

This is stopping me from getting stuff done. Does anyone know a simple way to fix this so I can collect Quantity information when I'm collecting data about a project and its parts?

Thanks!

Upvotes: 2

Views: 437

Answers (1)

Related Questions