Solved

ADO.NET Entity Data Model constraint problem

Posted on 2011-02-18
4
724 Views
Last Modified: 2012-05-11
I am learning about ADO.NET Entity Data Models and have created a model within my VS2008
application.  I added a table to the MS SQL Server database and created a foreign key constraint using the customer_id keys.

Now,  when I attempt to run the attached code: the following error occurrs.

"
An error occurred while preparing the command definition. See the inner exception for details.  NFTrendbookModel.msl(88,6) : error 3007: Problem in Mapping Fragments starting at lines 20, 88: Non-Primary-Key column(s) [CUSTOMER_ID] are being mapped in both fragments to different conceptual side properties - data inconsistency is possible because the corresponding conceptual side properties can be independently modified.  "

Sub Main()

            Dim context As NFTrendbookDAL.NFTrendbookEntities = New NFTrendbookDAL.NFTrendbookEntities()


            Try
       
                Dim result = From c In context.tTrendEvents Select c
                
                'The next line explodes the code. 
                For Each cc In result
                    Console.WriteLine(cc.Comments)
                Next
                Console.Read()

            Catch ex As Exception
              'The Exception as noted above occurs

            End Try


        End Sub

Open in new window

0
Comment
Question by:Howard Bash
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 21

Expert Comment

by:Craig Wagner
ID: 34932930
The problem isn't in the code, it's in the model definition. It sounds like your foreign key in the model isn't pointing to the primary key of another entity. Making the column to which the foreign key point a primary key may solve the problem.
0
 
LVL 1

Author Comment

by:Howard Bash
ID: 34933152
I am attaching the ddl for both tables/constraints.  It looks ok to me.
USE [mydb]
GO

/****** Object:  Table [mydb].[tTrendEvents]    Script Date: 02/19/2011 10:00:39 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [mydb].[tTrendEvents](
	[EVENT_ID] [numeric](18, 0) NOT NULL,
	[CUSTOMER_ID] [numeric](18, 0) NOT NULL,
	[EventDateTime] [datetime] NULL,
	[UnitCost] [varchar](50) NULL,
	[ElapsedTime] [numeric](18, 0) NULL,
	[Comments] [varchar](2048) NULL,
 CONSTRAINT [PK_tTrendEvents] PRIMARY KEY CLUSTERED 
(
	[EVENT_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [mydb].[tTrendEvents]  WITH CHECK ADD  CONSTRAINT [fk_CustID] FOREIGN KEY([CUSTOMER_ID])
REFERENCES [mydb].[tTrendCustomers] ([CUSTOMER_ID])
GO

ALTER TABLE [mydb].[tTrendEvents] CHECK CONSTRAINT [fk_CustID]
GO


---
---

USE [mydb]
GO

/****** Object:  Table [mydb].[tTrendCustomers]    Script Date: 02/19/2011 10:04:08 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [mydb].[tTrendCustomers](
	[CUSTOMER_ID] [numeric](18, 0) NOT NULL,
	[Name] [varchar](100) NULL,
	[NickName] [varchar](100) NULL,
	[Email] [varchar](100) NULL,
	[FirstContactDateTime] [datetime] NOT NULL,
 CONSTRAINT [PK_tTrendCustomers] PRIMARY KEY CLUSTERED 
(
	[CUSTOMER_ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

Open in new window

0
 
LVL 1

Author Comment

by:Howard Bash
ID: 34934497
Here's an update.
I recreated the DAL project and the console application which uses it.  It worked fine with no change to any database constraints/keys.

My conclusion is that the DAL and/or the tester projects did not get refreshed correctly although I did select the refresh databases from the right click menu in the model.

So, the problem becomes how to refresh the applications as I certainly don't want to restart a project when I change the database.

0
 
LVL 21

Accepted Solution

by:
Craig Wagner earned 500 total points
ID: 34935255
When I referred to the model I wasn't referring to the database, I was referring to the Entity Model (i.e. the edmx) in the project.

Sometimes I've found that Entity Framework doesn't always infer keys correctly from the database. It can also be a problem if you change the keys on the table. In those cases, just modify the model. If a column is part of the primary key in the database and EF doesn't recognize it, right-click on the column and select Entity Key (I think that's it, I'm doing this from memory). There's no need to start over, nor is doing "Refresh from database" always necessary, you can just edit the model.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question