错误的关系无法更改,因为使用WCF和Automapper的外键不可为空

时间:2019-01-02 10:10:27

标签: entity-framework automapper

我正在一个项目,该项目在服务器端使用WCF对数据进行CRUD操作。使用Automapper将数据转换为模型。 WCF服务代码如下。

    [ServiceContract]    
    public interface IGuardService
    {
        [OperationContract]
        int UpdateGuest(Guest ev);
    }

    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
    public class GuardService : IGuardService
    {
        public IGuardUow Uow;


        public GuardService(IGuardUow uow)
        {
             this.Uow = uow;
             cfg.CreateMap<Guest, Guest>().ForMember(dest => dest.RowVersion, map => map.Ignore())
            .ForMember(dest => dest.VisitingPlace, opt => opt.UseDestinationValue())
            .ForMember(dest => dest.ChangeUser, opt => opt.UseDestinationValue())
            .ForMember(dest => dest.Section, opt => opt.UseDestinationValue())
            .ForMember(dest => dest.CreateUser, opt => opt.UseDestinationValue());
        }
        public int UpdateGuest(Guest ev)
        {
            try
            {
                Guest e = Uow.Guests.GetAllByCondition(u => u.GuestId == ev.GuestId).FirstOrDefault();
                Mapper.Map<Guest, Guest>(ev, e);
                Uow.Guests.Update(e);

                Uow.Commit();
                return 1;
            }
            catch (Exception ex)
            {
                return -1;
            }
        }
}

另一方面,Ninject的配置如下:

private static void RegisterServices(IKernel kernel)
        {

            kernel.Bind<RepositoryFactories>().To<RepositoryFactories>()
                .InSingletonScope();
            kernel.Bind<IRepositoryProvider>().To<RepositoryProvider>().InSingletonScope();
            kernel.Bind<IGuardUow>().To<GuardUow>().InSingletonScope();
            kernel.Bind<IGuardService>().To<GuardService>().InSingletonScope();

        } 

还有如下实现的通用更新方法:

public virtual void Update(T entity)
        {
            DbEntityEntry dbEntityEntry = DbContext.Entry(entity);

            if (dbEntityEntry.State == EntityState.Detached)
            {
                DbSet.Attach(entity);
            }
            dbEntityEntry.State = EntityState.Modified;


        }

为什么会出现此错误?

0 个答案:

没有答案