How do I get rid of Error 3002?

.net-3.5 entity-framework linq-to-entities sql-server-2008

Question

Suppose I use SQL Server 2008 with the following table definitions:

CREATE TABLE Person
(PersonId INT IDENTITY NOT NULL PRIMARY KEY,
 Name VARCHAR(50) NOT NULL,
 ManyMoreIrrelevantColumns VARCHAR(MAX) NOT NULL)

CREATE TABLE Model
(ModelId INT IDENTITY NOT NULL PRIMARY KEY,
 ModelName VARCHAR(50) NOT NULL,
 Description VARCHAR(200) NULL)

CREATE TABLE ModelScore
(ModelId INT NOT NULL REFERENCES Model (ModelId),
 Score INT NOT NULL,
 Definition VARCHAR(100) NULL,
 PRIMARY KEY (ModelId, Score))

CREATE TABLE PersonModelScore
(PersonId INT NOT NULL REFERENCES Person (PersonId),
 ModelId INT NOT NULL,
 Score INT NOT NULL,
 PRIMARY KEY (PersonId, ModelId),
 FOREIGN KEY (ModelId, Score) REFERENCES ModelScore (ModelId, Score))

The notion is that each Person may have a score for any number of stated Models, but each Person may only have one ModelScore per Model. I believe that this SQL should automatically enforce these limitations. The Definition contains the specific "meaning" that the ModelScore has. Nothing revolutionary there.

I now attempt to translate this using the designer in Entity Framework. I now have three objects: a Person object, a Model object, and a ModelScore object after updating the model from the database and making certain changes. Being a join table, PersonModelScore is not an object; rather, it is included as an association with a different name (let's say ModelScorePersonAssociation). The association's mapping information is as follows:

- Association
  - Maps to PersonModelScore
    - ModelScore
        ModelId : Int32       <=>  ModelId : int
        Score : Int32         <=>  Score : int
    - Person
        PersonId : Int32      <=>  PersonId : int

The ModelId and PersonId variables contain primary key symbols on the right side, but the Score value does not.

After compilation, I obtain:

Error 3002: Problem in Mapping Fragment starting at line 5190: Potential runtime violation of table PersonModelScore's keys (PersonModelScore.ModelId, PersonModelScore.PersonId): Columns (PersonModelScore.PersonId, PersonModelScore.ModelId) are mapped to EntitySet ModelScorePersonAssociation's properties (ModelScorePersonAssociation.Person.PersonId, ModelScorePersonAssociation.ModelScore.ModelId) on the conceptual side but they do not form the EntitySet's key properties (ModelScorePersonAssociation.ModelScore.ModelId, ModelScorePersonAssociation.ModelScore.Score, ModelScorePersonAssociation.Person.PersonId).

What went wrong, whether I made the mistake in the designer or not, and how can I change it?

Thank you!

1
41
5/14/2010 4:41:41 PM

Popular Answer

I apologize for responding so late to your inquiry, but I ran into the identical problem and found that the entity framework designer had designated my "ScoreId" column as a primary key column (in relation to your PersonModelScore table). My ScoreId option was adjusted to false, and everything functioned well after that.

65
3/5/2011 4:45:40 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow