简体   繁体   English

更新一对多表实体框架

[英]Update one to many table Entity Framework

I have a create User ASP.NET MVC 3 page - on the page there is a multiselect box of cars which is prepoulated from a Cars table in my DB. 我有一个“创建User ASP.NET MVC 3”页面-该页面上有一个多选汽车框,该框是从我的数据库中的Cars表中预先填充的。 On save User I return a list of ints of CarIds that the user is associated with. 在保存用户时,我返回与用户关联的CarId的整数列表。 I am using WCF services and Entity Framework 5.0 to get my data back to SQL Server and save to SQL server. 我正在使用WCF服务和Entity Framework 5.0将数据返回到SQL Server并保存到SQL Server。

I then have a Table in my DB CarUser - it just contain a user Id and a client id - two FK's to the UserId in User Table and the CarId in the Cars table. 然后,我在数据库CarUser中有一个表-它只包含一个用户ID和一个客户端ID-用户表中的UserId和汽车表中的CarId分别为两个FK。

When I create a User it is updating the CarUser table is getting updated correctly (so for eg - it might look like this) 当我创建一个用户时,它正在更新CarUser表正在正确地更新(因此,例如-它可能看起来像这样)

CarId UserId 2 4 3 4 4 4 5 4 CarId用户ID 2 4 3 4 4 4 5 4

So it is looking correct in that my CarUser table is showing User 4 associated with the 4 CarIds that were seleted from the Multi Select box. 因此它看起来正确,因为我的CarUser表显示了与从“多重选择”框中选择的4个CarId相关联的用户4。 However the problem I am having is that it is re-creating the Cars in the cars table - so it is creating Car Id 9,10,11,12 for example but the details of them are exactly the same as 2,3,4,5 但是我遇到的问题是它正在汽车表中重新创建汽车-例如,它正在创建汽车ID 9,10,11,12,但它们的详细信息与2,3,4完全相同,5

The code I have wrote for this is below: 我为此编写的代码如下:

    public User User_Create(User user, List<int> carIds)
    {
        DAL.CarUserWCFFServiceImpl carUser = new DAL.CarUserWCFFServiceImpl();

        // cal the DAL layer to do the create
        User newUser = carUser.User_Create(user);

        //call the DAL layer to create the cars linked to a user
        carUser.CarUserMapping_Create(newUser.UserID, carIds);

so my first DAL method (which is a layer below the one this code is listed from) call creates the User - then once I have the user so I have the ID, etc I call another method at the DAL layer passing in the UserID of the newly created User and then the list of carIds associated with the User. 因此,我的第一个DAL方法(位于列出该代码的方法的下一层)调用创建了User-然后,一旦有了用户,便有了ID,依此类推,我在DAL层调用了另一个方法,传入了UserID作为新创建的用户,然后是与该用户关联的carId列表。

The code from the 2nd method is below: 下面是第二种方法的代码:

    public void CarUserMapping_Create(int userId, List<int> carIds)
    {
        using (CUEntities entities = new CUEntities())
        {
            User user = User_GetById(userId);
            entities.Users.Attach(userId);

            foreach (int carId in carIds)
            {
                Car car = Car_GetById(carId);

                user.Cars.Add(car);
                entities.ObjectStateManager.ChangeObjectState(user, System.Data.EntityState.Modified);
                entities.SaveChanges();
            }
        }
    }

Can anyone see something I am doing wrong? 谁能看到我做错了什么? My other option I am thinking is take this away from Entity Framework and just call a stored procedure in CarUserMapping_Create 我在想的另一种选择是从Entity Framework移除它,然后在CarUserMapping_Create中调用存储过程

Maybe you need to add do the following: 也许您需要添加以下内容:

public void CarUserMapping_Create(int userId, List<int> carIds)
{
    using (CUEntities entities = new CUEntities())
    {
        User user = User_GetById(userId);
        entities.Users.Attach(userId);

        foreach (int carId in carIds)
        {
            Car car = Car_GetById(carId);
            car.UserID = user.UserID;
            entities.Entry(car).State = EntityState.Modified;
            entities.SaveChanges();
        }
    }
}

You shuold not set the state like that. 您不应该这样设置状态。 thats like raping the framework. 那就像强奸框架。

Instedad send the model instance ( entities ) down as a paramater to the methods that reads the reads the car and user. Instedad将模型实例( entities )作为参数传递给读取读取的汽车和用户的方法。 (and ofc. use the entities as the ObjectContext in the methods) like this: (并且在方法中使用entities作为ObjectContext),如下所示:

using (CUEntities entities = new CUEntities())
{
    User user = User_GetById(entities , userId);


    foreach (int carId in carIds)
    {
        Car car = Car_GetById(entities, carId);
        car.UserID = user.UserID;
        entities.SaveChanges();
    }
}

that way, the entity framework will keep tract of the entity state 这样,实体框架将保持实体状态范围

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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