c# - Problem adding objects in Entity Framework, I'm guessing it's a problem with my database model -


My problem is that I am trying to add a child, but I am running into problems.

Here's my position:

I have a bunch of profiles. Each profile includes category preferences. Each attribute preference is attached to an attribute priority.

Enter image details here

When I'm in Visual Studio When I update my model, I get the following: Enter image details here

I can get the following to work without any problems:

  priority> category Priority = New CategoryPriority (); Profile_profile = db GetProfile (ProfileId); _profile.CategoryPriorities.Add (_categoryPriority); DB. Savechanges ()   

But somehow the following will not work:

  attribute priority _attributePriority = new attributePriority (); Category Preference _categoryPriority = New Category Preference (); _categoryPriority.AttributePriorities.Add (_attributePriority); // Error! There is no option of "add" or any other operation for the case profile _profile = db. GetProfile (ProfileId); _profile.CategoryPriorities.Add (_categoryPriority); DB. Savechanges ()   

I am guessing how the EF model is established. Ideally I want to do one-on-one between category priority and attribute priority tables.

I will add an image of my model.

Any thoughts?

EDIT: I have added images to my post.

The funny thing is that if I write:

  profile _p = new profile ();   

_p.Add is not an option. Obviously I'm missing something ..

Edit 2: Okay so I got to work, almost ... I know, not the most aesthetically soothing code, I am working on it ..

  Profile _profile = this. GetProfile (This. GetUserID (arrangeattributesviewmodel.ProfileID)); Int iter = 0; In the foreach (attributableforregrage_content) arrangeattributesviewmodel.AllAttributesForCheckBoxList.CategoryAttributeList) {iter ++; Category Preference _categoryPriority = New Category Preference (); _category Prioryity.ProfileID = arrangeattributesviewModel.ProfileID; _category Priority.CategoryID = _category.CategoryID; _category Priority.CategoryName = _category.CategoryName; _category Priority.CategoryID = _category.CategoryID; _category Priorityity.CategoryPriorityNR = iter; Characteristic Prairie _attributePriority = New Feature Preference (); _attributePriority.AttributePriorityString = _category.CompilePriorityString (); _category Priorityity.AttributePriority = _attributePriority; _profile.CategoryPriorities.Add (_categoryPriority); Db.SaveChanges (); // This fails with the message below.}   

{"A dependent property has been mapped for a store-generated column in a Revexial Consultant column: ' Career Priority ID '. "}

Any ideas?

EDIT 3: Fix it!

I had to instciciate an attribute priority in my category priority ...

category priority and AttributePriority is 1: 0..1 so that's not the case. You simply call the Add method. / P>

  _categoryPriority.AttributePriority = attributePriority;   

How is BTW defining your organization attribute priority , but uses your code AttributePriorities ? It should not be compiled.

Comments