使用EF更新WCF服务中的数据库表

时间:2014-04-17 13:52:09

标签: c# wcf entity-framework

我想在wcf web服务中更新我的数据库表。但我得到了2个错误。首先,我无法将字符串更改为日期时间。我发送了" 1/1/1991"但我得到了错误。其次,我无法更新我的表格。我该如何解决?

myentities db = new myentities();   
 public bool EditPerson(int uid,string _name,string bdate,string adr,string cty,string email,string tel,string pass)
        {
            DateTime myDate = DateTime.ParseExact(bdate, "yyyy-MM-dd",null); //Error

            var person = db.Person.FirstOrDefault(x => x.pID == uid);
            if (person != null)
            {
                Person pt = new Person();
                pt.pName = _name;
                pt.birthDate = myDate;
                pt.adress = adr;
                pt.city = cty;
                pt.email = email;
                pt.tel = int.Parse(tel);

                db.Entry(person).CurrentValues.SetValues(pt);
                db.SaveChanges();  //Error
                return true;
            }
            return false;
        }

错误:由于内部错误,客户端无法处理请求。

Server stack trace:
   location: System.ServiceModel.Channels.ServiceChannel.ThrowIfFaultUnderstood(Message reply, MessageFault fault, String action, MessageVersion version, FaultConverter faultConverter)
   location: System.ServiceModel.Channels.ServiceChannel.HandleReply(ProxyOperationRuntime operation, ProxyRpc& rpc)
   location: System.ServiceModel.Channels.ServiceChannel.Call(String action, Boolean oneway, ProxyOperationRuntime operation, Object[] ins, Object[] outs, TimeSpan timeout)
   location: System.ServiceModel.Channels.ServiceChannelProxy.InvokeService(IMethodCallMessage methodCall, ProxyOperationRuntime operation)
   location: System.ServiceModel.Channels.ServiceChannelProxy.Invoke(IMessage message)

Exception rethrown at [0]: 
   location: System.Runtime.Remoting.Proxies.RealProxy.HandleReturnMessage(IMessage reqMsg, IMessage retMsg)
   location: System.Runtime.Remoting.Proxies.RealProxy.PrivateInvoke(MessageData& msgData, Int32 type)
   location: IService.EditPerson(Int32 uid, String _name, String bdate, String adr, String cty, String email, String tel, String pass)
   location: ServiceClient.EditPerson(Int32 uid, String _name, String bdate, String adr, String cty, String email, String tel, String pass)

1 个答案:

答案 0 :(得分:2)

来自DateTime.ParseExact Method

  

将指定的日期和时间字符串表示形式转换为它   DateTime等效使用指定的格式和特定​​于文化   格式信息。 字符串表示的格式必须匹配   完全符合指定的格式。

在你的情况下,他们不是。如果您的字符串为1/1/1991,那么您需要使用d/M/yyyy(我假设第一个是日)格式。

您可能需要使用InvariantCulture,因为/ custom format specifier具有的特殊含义,将我替换为当前的文化日期分隔符。当您将null用作IFormatProvider时,此方法会使用您的CurrentCulture。如果您当前的文化广告热线/DateSeperator,则可以使用null

DateTime myDate = DateTime.ParseExact(bdate, "d/M/yyyy",
                                      CultureInfo.InvariantCulture);

我无法对您的更新错误发表任何意见,因为我对实体框架的了解太多。

相关问题