Solved

ADO.NET Entity Data Model constraint problem

Posted on 2011-02-18
4
710 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:hbash
  • 2
  • 2
4 Comments
 
LVL 21

Expert Comment

by:Craig Wagner
Comment Utility
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:hbash
Comment Utility
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:hbash
Comment Utility
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
Comment Utility
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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Suggested Solutions

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
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…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

762 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

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now