• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 423
  • Last Modified:

Entity Framework issue

Hello,
i have two tables one table for employees and the other is for PCs
PcID is a forign Key in the employee table, when i try to add an employee to the system using entity framework

EmployeeEntity x = new EmployeeEntity();
Employee employee = new employee();
i always get an error around
employee.PcID

how can i solve this issue ??

0
saudiprogrammer
Asked:
saudiprogrammer
  • 10
  • 6
1 Solution
 
CodeCruiserCommented:
What does the error say? Is the PCID a required field in the database?
0
 
saudiprogrammerAuthor Commented:
The member with identity 'EventCount' does not exist in the member collection.
0
 
saudiprogrammerAuthor Commented:
pcid is required
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
CodeCruiserCommented:
I think EventCount is a property in the class but does not correspond to a column in the database. I have been getting similar problems with LINQ.
0
 
saudiprogrammerAuthor Commented:
what should i do with it, my hair is starting to fall because of it :(
0
 
CodeCruiserCommented:
Is there any property in the class? If so, try by commenting that out to isolate the problem.
0
 
saudiprogrammerAuthor Commented:
nope there is no property in the class
0
 
CodeCruiserCommented:
What a quick reply!!! Can you show us the code?
0
 
saudiprogrammerAuthor Commented:
here is the code & the entity diagram


                WebFleetEntities x = new WebFleetEntities();
                Vehicles vehicle;
                VehicleIDs = new int[NewVehicles.Length];
                var query = 0;
                int DriverIDNum, TrailerIdNum;
                short VGroupNum, MDTTypeNum;
                for (int i = 0; i < NewVehicles.Length; i++)
                {
                    vehicle = new Vehicles();
                    vehicle.BrandName = NewVehicles[i].BrandName;
                    vehicle.Color = NewVehicles[i].Color;
                    vehicle.DefaultDestination = (short)NewVehicles[i].DefaultDestination;
                    vehicle.DefaultEndTime = NewVehicles[i].DefaultEndTime;
                    vehicle.DefaultEndType = (short)NewVehicles[i].DefaultEndType;
                    vehicle.Idle = (short)NewVehicles[i].Idle;
                    vehicle.JobCardNo = NewVehicles[i].JobcardNumber;
                    vehicle.LiceNo = NewVehicles[i].LicenseNumber;
                    vehicle.LicIssueDate = NewVehicles[i].LicenseIssueDate;
                    vehicle.LicValidUntil = NewVehicles[i].LicValidUntil;
                    vehicle.MapSymbol = NewVehicles[i].MapSymbole;
                    vehicle.MobileNo = NewVehicles[i].MobileNumber;
                    vehicle.ModelNo = NewVehicles[i].ModelNumber;
                    vehicle.Password = NewVehicles[i].Password;
                    vehicle.PlateNo = NewVehicles[i].PlateNumber;
                    if (NewVehicles[i].AttachmentFile == null)
                        vehicle.AttachmentFile = "";
                    else vehicle.AttachmentFile = NewVehicles[i].AttachmentFile;
                    vehicle.VCode = NewVehicles[i].VCode;
                    vehicle.SerialNumber = NewVehicles[i].SerialNumber;
                    DriverIDNum = NewVehicles[i].DefaultDriver;
                    TrailerIdNum = NewVehicles[i].TrailerID;
                    VGroupNum = (short)NewVehicles[i].VGroup;
                    MDTTypeNum = (short)NewVehicles[i].MDTType;
                    Drivers dr = x.Drivers.Where(m => m.DriverID == DriverIDNum).FirstOrDefault();
                    VehGroups vg = x.VehGroups.Where(s => s.GroupID == VGroupNum).FirstOrDefault();
                    Trailers tr = x.Trailers.Where(m => m.TrailerID == TrailerIdNum).FirstOrDefault();
                    MDTtypes mt = x.MDTtypes.Where(m => m.TypeNo == MDTTypeNum).FirstOrDefault();
                    
                    if (dr != null)
                        dr.Vehicles.Add(vehicle);
                    else
                        return -6;
                    if (mt != null)
                        mt.Vehicles.Add(vehicle);
                    else
                        return -7;
                    if (vg != null)
                        vg.Vehicles.Add(vehicle);
                    else
                        return -8;
                    if (tr != null)
                    tr.Vehicles.Add(vehicle);
                    try
                    {
                        x.AddToVehicles(vehicle);
                        x.SaveChanges(true);
                    }
                    catch (Exception ex)
                    {
                        return -5;
                    }
                    query = (from d in x.Vehicles select d.VehicleID).Max();
                    VehicleIDs[i] = query;
                }
                if (VehicleIDs.Length > 0)
                    return 0;
                else
                    return -1;
            }
            else
            {
                VehicleIDs = null;
                return -2;
            }

Open in new window

Attachment.JPG
0
 
CodeCruiserCommented:
I dont see the Employee entity although the problem you get is with the employee entity!
0
 
saudiprogrammerAuthor Commented:
i gave employee entity as an example, the correct code is what you have there
0
 
saudiprogrammerAuthor Commented:
still awaiting reply
0
 
CodeCruiserCommented:
When i receive this error, its usually when properties do not match up to columns in DB. You said you used Employee as an example. Now in the code and diagram you supplied, i dont see any pcid nor eventcount. What line of this code you get error at?
0
 
saudiprogrammerAuthor Commented:
i solved it i added AcceptAllChanges();
and it worked fine.
0
 
saudiprogrammerAuthor Commented:
still its not saving
im getting this error when i do x.SaveChanges();
0
 
saudiprogrammerAuthor Commented:
i solved the issue by using stored procedures and mapping them to the entity model.

Regards,
0

Featured Post

Configuration Guide and Best Practices

Read the guide to learn how to orchestrate Data ONTAP, create application-consistent backups and enable fast recovery from NetApp storage snapshots. Version 9.5 also contains performance and scalability enhancements to meet the needs of the largest enterprise environments.

  • 10
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now