Question

I am returning to programming after focusing on databases for a number of years. I used to be a gun in VB6 but .Net has me floundering.

I am attempting to build a web app in MVC from an already existing database. I am roughly following this: http://msdn.microsoft.com/en-us/data/gg685489.aspx

I'm using Visual Web Developer Express 2010 10.0.40219.1 SP1Rel I'm not sure how to work out the versions of the various templates and plugins.

I am generating an entity data model from an exsting database. The three tables I'm using have Primary and Foreign keys defined, however when I import it, no associations are created in the data model

There seems to be a lot of different ways to build a database enabled web app. If I was more familiar with the syntax I could have hand coded it by now.

Q1: Any idea why associations are not created?

Q2: Can you verify that I need associations to tell the code generator how everything fits together? (i.e. autogenerate the appropriate methods in code). I can't see how it would work otherwise.

Q3: If I create associations manually and Build, I get some errors. It appears that creating associations creates a property against the entity ('CustomerCustomer_ID) and this needs to be mapped to something. However when I press 'Table Mapping' this isn't in the list to map and I can't add it.

Error   1 Error 3004: Problem in mapping fragments starting at line 198:
No mapping       specified for properties Task.CustomerCustomer_ID in Set Tasks.
An Entity with Key (PK) will not round-trip when: Entity is type [zzzz.Task]
Error   2 Error 11009: Property 'CustomerCustomer_ID' is not mapped.

Here's the abridged DDL for two of the tables that I would expect would generate associations

CREATE TABLE [dbo].[Customers](
    [Customer_ID] [int] IDENTITY(1,1) NOT NULL,
    [Customer_Name] [varchar](50) NOT NULL,
........
....
CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED 
([Customer_ID] ASC))

CREATE TABLE [dbo].[Tasks](
    [Task_ID] [int] IDENTITY(1,1) NOT NULL,
    [TaskType_ID] [int] NOT NULL,
    [Emp_ID] [int] NOT NULL,
    [Customer_ID] [int] NOT NULL,
    [Status_ID] [int] NOT NULL,
    [Task_Desc] [varchar](300) NULL,
.....
..
CONSTRAINT [PK_Tasks] PRIMARY KEY CLUSTERED 
(
    [Task_ID] ASC
)

ALTER TABLE [dbo].[Tasks]  WITH CHECK ADD  CONSTRAINT [FK_Tasks_Customers] 
FOREIGN KEY([Customer_ID])
REFERENCES [dbo].[Customers] ([Customer_ID])
GO
Was it helpful?

Solution

As posted above, this is how I worked around this issue:

I created the remaining missing associations like this:

  1. Right click on design surface, Add Associations
  2. Define Associations, give it a name, set up the correct cardinality
  3. UNTICK 'Add foreign key properties the the....' and press OK
  4. Click on the association on the work service and in the properties page click on the Referential Constraint property and define it (principal is your 'lookup' side)

Thanks for trying to help everyone but I suspect my issue might be hard to reproduce.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top