Solved

ADO.NET Entity Data Model constraint problem

Posted on 2011-02-18
4
746 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

Containers & Docker to Create a Powerful Team

Containers are an incredibly powerful technology that can provide you and/or your engineering team with huge productivity gains. Using containers, you can deploy, back up, replicate, and move apps and their dependencies quickly and easily.

Question has a verified solution.

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

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

632 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