English 中文(简体)
如何利用实体框架和地图制作更新/电子数据库
原标题:How to Update/Edit Database Field with Using Entity Framework and Mapper

This is a specific question. We are coding a wcf service with using C#, EF4.1 and Mapper. We don t want to use stored procedures... Anyway; Problem is, we want to edit address field on db. But we can t save edited address to db.

    public int EditAddress(int userId, Address address)
    {
        using(var mobileServiceContext = new MobileServiceContext())
        {
            Address oldAddress = mobileServiceContext.Addresses.FirstOrDefault(p => p.UserId == userId && p.AddressId == address.AddressId);
            Address lastAddress = Mapper.Map(address, oldAddress);

            //save new-last address with ? HOW ?
            //mobileServiceContext.Addresses.Attach(updatedAddress); //doesn t work
            mobileServiceContext.SaveChanges();
        }

        return address.AddressId;
    }

这里是我们的编辑职能。

    public int EditAddress(int userId, Address address)
    {
        using(var mobileServiceContext = new MobileServiceContext())
        {
            Address oldAddress = mobileServiceContext.Addresses.FirstOrDefault(p => p.UserId == userId && p.AddressId == address.AddressId);
            Address lastAddress = Mapper.Map(address, oldAddress);

            mobileServiceContext.Addresses.Attach(lastAddress); //error on this line
            mobileServiceContext.ObjectStateManager.ChangeObjectState(lastAddress, EntityState.Modified);
            mobileServiceContext.SaveChanges();
        }

        return address.AddressId;
    }

说明:“地址”类别已经处理。 同上。

我们在这里所做的设计非常复杂,也难以读到读者。

最佳回答

在这种情形下,你不能使用自动上诉人,而是可以做以下工作:

//this assumes that the AddressId in address is exists in the database: just attach it to the addresses set
mobileServiceContext.Addresses.Attach(address);
//this tells ef that the object is in a modified state
mobileServiceContext.ObjectStateManager.ChangeObjectState(address, entityState.Modified);
//savechanges will now update the database
mobileServiceContext.SaveChanges();

我看不出需要自动评估器或任何制图效用,因为我们没有将数据模型绘制成一种观察模型(或按照这些模型做任何事情)。

问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签