Entity Framework Assistance

Pho3nix

The Legend
Joined
Jul 31, 2009
Messages
32,829
Reaction score
3,033
Location
On the toilet
Hi all,

Need some assistance. Code is 90% done and really don't want to re-do everything in Linq to Sql :p
Keep getting the error below :
Code:
System.Data.UpdateException was caught
Message: Entities in 'SecondaryServices' participate in the 'SecondaryService_PrimaryService' relationship. 0 related 'PrimaryService' were found. 1 'PrimaryService' is expected.

Code in the repository looks like this :

Code:
                    service.PrimaryService = dataContext.PrimaryServices.FirstOrDefault(s => s.Id == service.PrimaryService.Id);
                    service.SecondaryService = dataContext.SecondaryServices.FirstOrDefault(s => s.Id == service.SecondaryService.Id);
                    service.PracticeDiscipline = dataContext.PracticeDisciplines.FirstOrDefault(s => s.Id == service.PracticeDiscipline.Id);
                    dataContext.AddToServiceRequests(serviceRequest);
                    dataContext.SaveChanges();

DB is set up with a one to many relationship (Primary Service -> Secondary Service). All fields are not nullable so I'm very confused. Any help would be appreciated.
 
I think we would need more info than that. Which line is it breaking on exactly? Stack trace? Litte more info on the data model too.

From some googling, it seems as if your model is incorrect...
 
It's breaking on the last line.
Code:
dataContext.SaveChanges();

Off-site at the moment so will post when I get back.
 
Are you sure you are looking for the correct Ids by any chance?
First you set service.PrimaryService to a PrimaryService with the same Id as itself. And then you do the same for the other two. So I take take it that you set the Ids somewhere else and then get the rest of the information from the database?
 
Doesnt make sense. Before saving you want to load the objects primaryservice and secondaryservice by using their primary key values but since you haven't loaded the object yet how will you know the primary key values.
 
Ok. This is the TestMethod

Code:
[TestMethod]
        public void AddServiceRequest()
        {
            ServiceRequest details = new ServiceRequest();
            details.Consultant = new UserDetail {Id = 1};
            details.PrimaryService = new PrimaryService {Id = 1};
            details.SecondaryService = new SecondaryService {Id = 2};
            details.PracticeDiscipline = new PracticeDiscipline {Id = 1};
            details.ResolutionComments = "New Test";
            serviceRequestRepository.SaveServiceRequest(details);
        }

In teh repository as posted earlier, this is a snippet :

Code:
void  IServiceRequestRepository.SaveServiceRequest(ServiceRequest service)
        {
            try
            {
                CentreEntities dataContext = new Entities(this.ConnectionString);
                if (service.Id == 0)
                {
                    service.PrimaryService = dataContext.PrimaryServices.FirstOrDefault(s => s.Id == service.PrimaryService.Id);
                    service.SecondaryService = dataContext.SecondaryServices.FirstOrDefault(s => s.Id == service.SecondaryService.Id);
                    service.PracticeDiscipline = dataContext.PracticeDisciplines.FirstOrDefault(s => s.Id == service.PracticeDiscipline.Id);
                    service.LineManager = dataContext.UserDetails.FirstOrDefault(s => s.Id == service.LineManager.Id);
                    dataContext.AddToServiceRequests(service);
                    dataContext.SaveChanges();
                }

Using .net 3.5, so EF v3.5 :o.

I've already changed everything to Linq to Sql but I'd like to figure out why this doesn't work :\.
Will try post some pics of the Model itself a little later today :)
 
Last edited:
Pho3nix, to me it still looks like you are using service.PrimaryService.Id and so forth when you probably want to use serviceRequest.PrimaryService.Id and so forth.
 
Pho3nix, to me it still looks like you are using service.PrimaryService.Id and so forth when you probably want to use serviceRequest.PrimaryService.Id and so forth.

:o Forgot to rename everything. Going to post everything properly when I get back to work,
 
:o Forgot to rename everything. Going to post everything properly when I get back to work,

Hehe alright.

What happens when you debug? What is the value of service.PrimaryService when you get to this line:
service.SecondaryService = dataContext.SecondaryServices.FirstOrDefault(s => s.Id == service.SecondaryService.Id);

I take it there already is a PrimaryService row in the database?
 
Hehe alright.

What happens when you debug? What is the value of service.PrimaryService when you get to this line:
service.SecondaryService = dataContext.SecondaryServices.FirstOrDefault(s => s.Id == service.SecondaryService.Id);

I take it there already is a PrimaryService row in the database?

Value is = 2 and yes there are rows in the DB for both Primary and Secondary Service.

Below is the Stack Trace :)

Code:
   at System.Data.Mapping.Update.Internal.UpdateTranslator.RelationshipConstraintValidator.ValidateConstraints()
   at System.Data.Mapping.Update.Internal.UpdateTranslator.ProduceCommands()
   at System.Data.Mapping.Update.Internal.UpdateTranslator.Update(IEntityStateManager stateManager, IEntityAdapter adapter)
   at System.Data.EntityClient.EntityAdapter.Update(IEntityStateManager entityCache)
   at System.Data.Objects.ObjectContext.SaveChanges(SaveOptions options)
   at System.Data.Objects.ObjectContext.SaveChanges()
   at Services.Repositories.EF.ServiceRequestRepository.Services.Repositories.Contract.IServiceRequestRepository.SaveServiceRequest(ServiceRequest serviceRequest) in C:\Projects\Services.Repositories.EF\ServiceRequestRepository.cs:line 114
   at ServicesTesting.EF.ServiceRepositoryTest.AddServiceRequest() in C:\Projects\ServicesTesting.EF\ServiceRepositoryTest.cs:line 43


PS> If anyone see's anything that would be confidential that I posted by mistake please PM me.
 
Top
Sign up to the MyBroadband newsletter
X