1

My problem is that I'm trying to add a child entity, but keep running into problems.

Here's my situation:

I have a bunch of Profiles. Each Profile has a bunch of CategoryPriorities attached to it. Each CategoryPriorities has one AttributePriority attached to it.

enter image description here

When I update my model in Visual Studio I get the following: enter image description here

I can without any trouble get the following to work:

CategoryPriority _categoryPriority = new CategoryPriority();
Profile _profile = DB.GetProfile(ProfileID);
_profile.CategoryPriorities.Add(_categoryPriority);
DB.savechanges()

But somehow the following will not work:

AttributePriority _attributePriority = new AttributePriority();    
CategoryPriority _categoryPriority = new CategoryPriority();
_categoryPriority.AttributePriorities.Add(_attributePriority);
// Error! There is no option of "Add" or any other operation for the matter.

Profile _profile = DB.GetProfile(ProfileID);
_profile.CategoryPriorities.Add(_categoryPriority);
DB.savechanges()

I'm guessing this is due to how the EF model is set up. Ideally I would like to have an one-to-one between CategoryPriority and AttributePriority tables.

I'll add an image of my model.

Any ideas? Any help much appreciated!

Edit: I've added images to my post.

Funny thing is that if I write:

Profile _p = new Profile();

There is no option of _p.Add there either.. obviously I'm missing something..

Edit 2: Ok, so I got it to work, almost... I know, not the most aesthetically pleasing code, I'm working on that..

Profile _profile = this.GetProfile(this.GetUserID(arrangeattributesviewmodel.ProfileID));

int iter = 0;
foreach (AttributeListForCategory _category in arrangeattributesviewmodel.AllAttributesForCheckBoxList.CategoryAttributeList)
{
iter++;
CategoryPriority _categoryPriority = new CategoryPriority();

_categoryPriority.ProfileID = arrangeattributesviewmodel.ProfileID;
_categoryPriority.CategoryID = _category.CategoryID;
_categoryPriority.CategoryName = _category.CategoryName;
_categoryPriority.CategoryID = _category.CategoryID;
_categoryPriority.CategoryPriorityNR = iter;


AttributePriority _attributePriority = new AttributePriority();
_attributePriority.AttributePriorityString = _category.CompilePriorityString();
_categoryPriority.AttributePriority = _attributePriority;
_profile.CategoryPriorities.Add(_categoryPriority);

db.SaveChanges(); // It fails here with the message below..
}

{"A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'CategoryPriorityID'."}

Any ideas?

Edit 3: Solved it!

I had to instanciate an AttributePriority in my CategoryPriority...

2
  • Is AttributePriorities collection in CategoryPriority entity? You should show your model and also CREATE scripts of your tables. Commented Mar 17, 2011 at 8:52
  • I think so, kind of new to EF. I will add images of my database model as soon I can (need another 2 reputation scores I think).. Commented Mar 17, 2011 at 9:13

2 Answers 2

1

Your relation between CategoryPriority and AttributePriority is 1:0..1 so there is not Add method. You simply call:

_categoryPriority.AttributePriority = attributePriority;

Btw. how is it possible that your entity define AttributePriority but your code uses AttributePriorities? It should not compile.

Sign up to request clarification or add additional context in comments.

1 Comment

That was a typo.. Thanks for your help! Have another problem though, updating my question...
1

Make sure you have foreign keys set up correctly and then regenerate your model.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.