Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

ADO.NET Entity Data Model constraint problem

Posted on 2011-02-18
4
Medium Priority
?
775 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
  • 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 2000 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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone 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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…

916 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