简体   繁体   中英

Entity-Framework Editing a many-to-many table

I'm designing an application that will be table to keep track of transportation.

I have four 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,
    ...
)
And one 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 to edit a 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 the classes values to create the new object
            TripID = TripID,
            VehicleID = VehicleID,
            DriverID = VehicleID
        };

    context.VehicleTrip.Attach(vehicletrip);
    context.Entry(vehicletrip).State = EntityState.Modified;
    context.SaveChanges();
}

However, it appears as if EF doesn't save the record.

You can change the state of an entity that is already being tracked by setting the State property on its entry. For example:

var existingBlog = new Blog { BlogId = 1, Name = "ADO.NET Blog" }; 

using (var context = new BloggingContext()) 
{ 
    context.Blogs.Attach(existingBlog); 
    context.Entry(existingBlog).State = EntityState.Unchanged; 

    // Do some more work...  

    context.SaveChanges(); 
}

Note that calling Add or Attach for an entity that is already tracked can also be used to change the entity state. For example, calling Attach for an entity that is currently in the Added state will change its state to Unchanged.

took it from here: http://msdn.microsoft.com/en-us/data/jj592676.aspx really good explanation of what is going on

One option will be to retrieve the original entity (the one that you are trying to update) then create a new entity with the new values and update using the following code:

context.Entry(originalEntity).CurrentValues.SetValues(newEntityWithNewValues);

and of course save your changes.

You don't appear to have any actual modifications to the record you attached. I suspect that in order for anything to be written, you either have to have an entity in "Added" state (which would cause an insert), or the "Modified" record needs to have some actual modifications, and the original values have to refer to an existing record that would be modified. Generally when dealing with link tables, however, you never modify values. You just add or delete link records.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM