c# - Entity-Framework Editing a many-to-many table -
i'm designing application table keep track of transportation.
i have 4 tables:
create table [dbo].[trips] ( [tripid] int not null primary key identity, ... ) create table [dbo].[drivers] ( [driverid] int not null primary key identity, ... ) create table [dbo].[vehicles] ( [vehicleid] int not null primary key identity, ... ) , 1 table called vehicletrip create table [dbo].[vehicletrip] ( [tripid] int not null , [vehicleid] int not null, [driverid] int not null, primary key ([tripid], [vehicleid], [driverid]), constraint [fk_tripids_trips] foreign key (tripid) references trips(tripid), constraint [fk_vechicleid_vehicles] foreign key (vehicleid) references vehicles(vehicleid), constraint [fk_driverid_driverid] foreign key (driverid) references drivers(driverid) )
using ef, i'm trying edit record in vehicletrip
public class tripvm { public int tripid { get; set; } public int vehicleid { get; set; } public int driverid { get; set; } //other fields public void insertupdate() { var vehicletrip = new studenttransportation.data.model.vehicletrip { //uses classes values create new object tripid = tripid, vehicleid = vehicleid, driverid = vehicleid }; context.vehicletrip.attach(vehicletrip); context.entry(vehicletrip).state = entitystate.modified; context.savechanges(); }
however, appears if ef doesn't save record.
one option retrieve original entity (the 1 trying update) create new entity new values , update using following code:
context.entry(originalentity).currentvalues.setvalues(newentitywithnewvalues);
and of course save changes.
Comments
Post a Comment