SubmitChanges() 只更新 1 个字段?

发布于 2024-08-08 16:16:05 字数 1344 浏览 2 评论 0原文

我确实是一个 LINQ 新手。我遇到一个未知问题:

public static int save(TEntity obj)
    {
        var table = dbo.GetTable<TEntity>();
        var mapping = dbo.Mapping.GetTable(typeof(TEntity));
        var pkfield = mapping.RowType.DataMembers.Where(d => d.IsPrimaryKey).Take(1).SingleOrDefault();
        if (Convert.ToInt32(obj.GetType().GetProperty(pkfield.Name).GetValue(obj, null)) == 0)
            table.InsertOnSubmit(obj);
        try
        {
            dbo.SubmitChanges();
        }
        catch (ChangeConflictException e)
        {
            dbo.SubmitChanges();
        }
        if (dbo.ChangeConflicts.Count == 0)
        {
            ClearCache(dbo);
            return Convert.ToInt32(obj.GetType().GetProperty(pkfield.Name).GetValue(obj, null));
        }
        else
        {
            dbo.ChangeConflicts.ResolveAll(System.Data.Linq.RefreshMode.KeepCurrentValues);
            return 0;
        }
    }

使用此方法时,只有 1 个字段已更新!这是我的日志:

UPDATE [dbo].[tbl_album]
SET [dt_m_date] = @p1
WHERE [i_album_id] = @p0
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- @p1: Input BigInt (Size = 0; Prec = 0; Scale = 0) [1256485605]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.4918

即使我更改了几乎所有字段,我的表也已经有了主键。但还是有问题。

请帮忙!

I'm really a LINQ newbie. I got an unknown problem:

public static int save(TEntity obj)
    {
        var table = dbo.GetTable<TEntity>();
        var mapping = dbo.Mapping.GetTable(typeof(TEntity));
        var pkfield = mapping.RowType.DataMembers.Where(d => d.IsPrimaryKey).Take(1).SingleOrDefault();
        if (Convert.ToInt32(obj.GetType().GetProperty(pkfield.Name).GetValue(obj, null)) == 0)
            table.InsertOnSubmit(obj);
        try
        {
            dbo.SubmitChanges();
        }
        catch (ChangeConflictException e)
        {
            dbo.SubmitChanges();
        }
        if (dbo.ChangeConflicts.Count == 0)
        {
            ClearCache(dbo);
            return Convert.ToInt32(obj.GetType().GetProperty(pkfield.Name).GetValue(obj, null));
        }
        else
        {
            dbo.ChangeConflicts.ResolveAll(System.Data.Linq.RefreshMode.KeepCurrentValues);
            return 0;
        }
    }

When using this method, there is only 1 field has been updated!! Here is my log:

UPDATE [dbo].[tbl_album]
SET [dt_m_date] = @p1
WHERE [i_album_id] = @p0
-- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [1]
-- @p1: Input BigInt (Size = 0; Prec = 0; Scale = 0) [1256485605]
-- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.4918

Even I changed almost fields, my table has primary key already. But still problem.

Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

柒七 2024-08-15 16:16:05

您是否单步执行 save 方法来查看代码的作用?它是否找到了正确的主键列,并且是否确实检测到现有主键的存在?

您的 tbl_album 中有哪些字段 - 您是否确保它们没有全部标记为只读(不太可能,但仍然 - 请检查以确保!)。

我现在不认为你的代码有什么根本性的错误,对我来说这似乎有点复杂,但我相信它应该有效。

马克

更新:
检查以确保您的表列并非都是只读的! :)

替代文本

Did you step through your save method to see what the code was doing? Did it find the right primary key column, and did it indeed detect the presence of the existing primary key?

What fields to you have in your tbl_album - did you make sure they're not all marked as read-only (unlikely, but still - check to be sure!).

I don't see antyhing fundamentally wrong with your code right now, it seems a bit complicated for my taste, but it should work, I believe.

Marc

UPDATE:
Check to make sure your table columns aren't all readonly! :)

alt text

歌入人心 2024-08-15 16:16:05

经过大量测试后,我发现一件事:所有外键都没有更新,正常字段都可以。那么告诉我是否有任何合理的方式导致了这种情况?

我用谷歌搜索了很多,但什么也没找到......

After making a lot of tests, I found one thing: all foreign keys are not updated, normal fields are okay. So tell me is there any reasonable way caused this crap?

I googled a lot but found nothing...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文