Entity Framework Update Error in ASP.NET Mvc with related entity
- by Barry
I have run into a problem which have searched and tried everything i can to find a solution but to no avail. I am using the same repository and context throughout the process
I have a booking entity and a userExtension Entity
Below is my image
i then get my form collection back from my page and create a new booking
public ActionResult Create(FormCollection collection)
{
Booking toBooking = new Booking();
i then do some validation and property assignment and find an associated BidInstance toBooking.BidInstance = bid;
i have checked and the bid is not null. finally i get the user extension file from the Current IPRINCIPAL USER as below
UserExtension loggedInUser = m_BookingRepository.GetBookingCurrentUser(User);
toBooking.UserExtension = loggedInUser;
The Code to do the getUserExtension is :
public UserExtension GetBookingCurrentUser(IPrincipal currentUser)
{
var user = (from u in Context.aspnet_Users
.Include("UserExtension")
where u.UserName == currentUser.Identity.Name
select u).FirstOrDefault();
if (user != null)
{
var userextension = (from u in Context.UserExtension.Include("aspnet_Users") where u.aspnet_Users.UserId == user.UserId select u).FirstOrDefault();
return userextension;
}
else{
return null;
}
}
It returns the userextension fine and assigns it fine. i originally used the aspnet_users but encountered this problem so tried to change it to the extension entity.
as soon as i call the :
Context.AddToBooking(booking);
Context.SaveChanges();
i get the following exception and im completely baffled by how to fix it
Entities in 'FutureFlyersEntityModel.Booking' participate in the 'FK_Booking_UserExtension' relationship. 0 related 'UserExtension' were found. 1 'UserExtension' is expected.
then the final error that comes to the front end is:
Metadata information for the relationship 'FutureFlyersModel.FK_Booking_BidInstance' could not be retrieved. Make sure that the EdmRelationshipAttribute for the relationship has been defined in the assembly.
Parameter name: relationshipName..
But both the related entities are set in the booking entity passed thruogh
PLEASE HELP Im at wits end with this