1

我现在遇到了麻烦,我无法在 DBDataContext 中更新表 X,所以这是我的片段,顺便说一下,当我只更新一个表时它可以工作!但是当我插入然后更新时,它会抛出异常:“无法将对象从 System.Int32 转换为 System.String 类型”

{
            using (DBDataContext db = new DBDataContext())
            {
                int codigo = Convert.ToInt32(lblNroInforme.Content);
                int sucessfull = 0;


                INFORMEMEDICO varInf = (from i in db.INFORMEMEDICOs
                                        where i.numeroinforme == codigo
                                        select i).SingleOrDefault();


                if (varInf == null)
                {
                    varInf = new INFORMEMEDICO();
                    varInf.codigoclase = Convert.ToInt32(lblCodigoClase.Content.ToString());
                    varInf.codigoestudio = lblCodigoEstudio.Content.ToString();
                    varInf.codigopaciente = Convert.ToInt32(lblCodigoPaciente.Content.ToString());
                    varInf.conclusion = GetText(rtbConclusion);
                    varInf.fechainforme = DateTime.Today;
                    varInf.firmauno = getJPGFromImageControl(firmaUno.Source as BitmapImage);
                    varInf.firmados = getJPGFromImageControl(firmaDos.Source as BitmapImage);
                    varInf.hallazgo = GetText(rtbHallazgo);
                    varInf.horainforme = Convert.ToDateTime(DateTime.Today.ToShortTimeString());
                    varInf.impreso = 0;
                    varInf.medicoinforma = cboTurnoMed.SelectedValue.ToString();
                    varInf.nombreinforme = txtNombreExamen.Text;
                    varInf.numeroinforme = Convert.ToInt32(lblNroInforme.Content.ToString());
                    varInf.tecnica = GetText(rtbTecnica);
                    varInf.turnosocio = lblTurnoMedico.Content.ToString();

                    try
                    {
                        db.INFORMEMEDICOs.InsertOnSubmit(varInf);
                        db.SubmitChanges();
                        sucessfull = 1;
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show("Error : " + ex.Message);
                    }
                    if (sucessfull==1)
                    {
                        EXAMENXATENCION varExA = (from ea in db.EXAMENXATENCIONs
                                                  where ea.codigo == codigo
                                                  select ea).SingleOrDefault();
                        varExA.estadoestudio = 'I';
                        db.SubmitChanges();
                    }

                }

            }

        }

当我执行更新时,我从调试器中获取了这个堆栈:

en System.Data.Linq.IdentityManager.StandardIdentityManager.MultiKeyManager`3.TryCreateKeyFromValues(Object[] values, MultiKey`2& k)
   en System.Data.Linq.IdentityManager.StandardIdentityManager.MultiKeyManager`3.TryCreateKeyFromValues(Object[] values, MultiKey`2& k)
   en System.Data.Linq.IdentityManager.StandardIdentityManager.MultiKeyManager`3.TryCreateKeyFromValues(Object[] values, MultiKey`2& k)
   en System.Data.Linq.IdentityManager.StandardIdentityManager.MultiKeyManager`3.TryCreateKeyFromValues(Object[] values, MultiKey`2& k)
   en System.Data.Linq.IdentityManager.StandardIdentityManager.IdentityCache`2.Find(Object[] keyValues)
   en System.Data.Linq.IdentityManager.StandardIdentityManager.Find(MetaType type, Object[] keyValues)
   en System.Data.Linq.CommonDataServices.GetCachedObject(MetaType type, Object[] keyValues)
   en System.Data.Linq.ChangeProcessor.GetOtherItem(MetaAssociation assoc, Object instance)
   en System.Data.Linq.ChangeProcessor.BuildEdgeMaps()
   en System.Data.Linq.ChangeProcessor.SubmitChanges(ConflictMode failureMode)
   en System.Data.Linq.DataContext.SubmitChanges(ConflictMode failureMode)
   en System.Data.Linq.DataContext.SubmitChanges()
   en Demo.View.InformeMedico.btnGuardar_Click(Object sender, RoutedEventArgs e) en D:\cs_InformeMedico\app\InformeMedico.xaml.cs:línea 430
4

3 回答 3

2

LINQ to Sql 不支持唯一键约束的外键(即备用键)。我遇到了同样的问题,发现阅读了一些博客。我想这不是一个错误。

非主键和 LINQ to SQL 问题(这是一个错误)?

于 2009-06-02T19:14:05.730 回答
1

After a lot of research, I found out that this bug only exists on WinXP-Machines with .net 3.5 SP1. On Windows 7 it works fine. Unfortunately, there are two different builds of the same framework.

There is a hotfix from microsoft, which resolves my problem on the xp machines. http://support.microsoft.com/hotfix/KBHotfix.aspx?kbln=ja&kbnum=963657

Regards, Florian

于 2010-08-24T10:17:15.993 回答
0

将您的 DMBL 文件作为 XML 文件打开。检查每一列的类型,看看应该是 int 的东西是否真的是字符串。

于 2009-02-16T21:30:50.017 回答