Linq to SQL-在代码中关闭UpdateCheck [英] Linq to SQL - Turn off UpdateCheck in code

查看:114
本文介绍了Linq to SQL-在代码中关闭UpdateCheck的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为所有成员(除了其主键)关闭UpdateCheck功能.现在,我以下面的示例为指导,但是我的表的MetaDataMembers仍然设置为Always.

I am wanting to turn off the UpdateCheck functionality for all members (except their primary keys). Now I was following the example below as guidance, however my MetaDataMembers of the table are still set to Always.

http://www.the- lazy-coder.com/2013/04/set-updatecheck-to-never.html

上面的代码片段只是让您更改属性,但是似乎永远都不会变,因为我可以在运行时调试代码,并且看到所有属性都已设置,因此我假设属性是更改不会更改基础对象.

The above code snippet just gets you to change the attribute, however it seems to never get picked up, as I can debug the code when it is running and I see all the properties being set, so I am presuming that the attributes changing does not change the underlying object.

现在,如果我要更改方法并直接从RowType直接获取MetaDataMembers,我会注意到它们具有UpdateCheck属性,但是只有一个吸气剂.那么,是否有一种方法可以在设置此属性后(如果需要,通过反射)将其覆盖?即使查看了反编译的源代码,它也是一个抽象类,我找不到任何可用于参考的实现.

Now if I were to change approach and just get the MetaDataMembers directly from the RowType I notice they have the UpdateCheck property, however only a getter. So is there a way to (via reflection if needed) overwrite this property once it is set? Even after looking at decompiled source it is an abstract class and I cannot find any implementations to use for reference.

我正在使用SQLMetal生成Context文件,因此没有设计者可以修改,尽管有些人会说我应该运行一些文本编辑宏来解析和更改属性,但是当我听起来这些都太长了应该只需能够进入内存中的对象,并使其忽略先前已告知的内容即可.

I am using SQLMetal to generate the Context files, so there is no designer tinkering available, and although some people will say that I should run some text editing macros to parse and change the attributes, it all sounds too long winded when I should just be able to go into the object in memory and tell it to ignore whatever it has been told previously.

所以!有没有一种方法可以覆盖实体中的属性?在创建对象之后和即将进行更新之前,我都尝试在两个构造函数中的那个链接中运行原始代码,但是这些更改似乎都不会停留或至少传播到重要的地方,几乎没有有关如何以编程方式进行任何操作的任何材料.

SO! Is there a way to override the property in the entities? I have tried running the original code in that link in both constructor, after the objects created and just before I am about to do an update, however none of the changes seem to stick or at least propagate to where it matters, and there is hardly any material on how to do any of this progmatically.

推荐答案

在互联网上搜索后,我发现没有很好的方法,尽管我最初提到的链接是无效的,因为它在属性在某种程度上是正确的,但是在上面的例子中,它们正在处理不在内存中的属性,而只是装饰,无论如何,下面的代码似乎有效,但效果不佳:

After searching around the internet I found no nice way to do it, and although there is the link I mentioned originally it doesn't work as it works on the attributes which are partly right but in the case above they are working on the attributes which are not in memory and are just the decorations, anyway the code below seems to work but is not nice:

public static void SetUpdateCheckStatus(this IDataContext dataContext, UpdateCheck updateCheckStatus)
        {
            var tables = dataContext.Mapping.GetTables();
            foreach (var table in tables)
            {
                var dataMembers = table.RowType.DataMembers;
                foreach (var dataMember in dataMembers)
                {
                    if (!dataMember.IsPrimaryKey)
                    {
                        var dataMemberType = dataMember.GetType();
                        if (dataMemberType.Name == "AttributedMetaDataMember")
                        {
                            var underlyingAttributeField = dataMember.GetType().GetField("attrColumn", BindingFlags.Instance | BindingFlags.NonPublic);
                            if (underlyingAttributeField != null)
                            {
                                var underlyingAttribute = underlyingAttributeField.GetValue(dataMember) as ColumnAttribute;
                                if (underlyingAttribute != null)
                                { underlyingAttribute.UpdateCheck = updateCheckStatus; }
                            }
                        }
                        else
                        {
                            var underlyingField = dataMember.Type.GetField("updateCheck", BindingFlags.Instance | BindingFlags.NonPublic);
                            if (underlyingField != null)
                            { underlyingField.SetValue(dataMember, updateCheckStatus); }
                        }
                    }
                }
            }
        }

IDataContext只是我们出于模拟目的围绕DataContext的包装,因此可以随时将其更改为DataContext.它的写法极具防御性,因为这种方式会拉回许多没有所有所需数据的成员,因此必须将其过滤掉,而只能处理那些需要的数据.

The IDataContext is just a wrapper we put around a DataContext for mocking purposes, so feel free to change that to just DataContext. It is written extremely defensively as this way pulls back lots of members which do not have all the desired data so it has to filter them out and only work on the ones which do.

这篇关于Linq to SQL-在代码中关闭UpdateCheck的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆