Solved

ADO.NET Entity Data Model constraint problem

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Convert Select to DropDownListFor MVC 5 2 30
How to extract certain lines from textfile and save them to another file? 6 27
Where is this file? 3 25
VS 2017 18 44
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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…

860 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