Linq to SQL - disable UpdateCheck in code

I want to disable functionality UpdateCheck

for all members (except primary keys). I have now followed the example below as a guide, however my MetaDataMembers tables are still set to Always

.

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

The above code snippet allows you to change the attribute, however it never seems to get picked up as I can debug the code when I run it and I can see all the properties are set, so I am assuming the attributes changing does not change the underlying object.

Now if I had to change the approach and just get the MetaDataMembers directly from the RowType, I noticed that they have a property UpdateCheck

, but only a getter. So, is there a way (via reflection if necessary) to overwrite this property after setting it? Even after looking at the decompiled source, it is an abstract class and I cannot find any implementations to use.

I am using SQLMetal to generate context files, so there are no design wizards available, and while some people will say that I should run some text editing macros to parse and change attributes, it all sounds too long when I should just be able to enter an object in memory and tell it to ignore everything that was said earlier.

SO! Is there a way to override a property on an entity? I tried to run the source code in this link in both constructors after the objects were created and before I go about making the update, however, none of these changes seem to concern or at least extend to where it matters. and hardly any material on how to do it progmatically.

+2


source to share


1 answer


After searching on the internet, I did not find a suitable way to do this, and although there is a link I mentioned initially, it does not work as it works with attributes which are kind of correct, but in the case above, they work on attributes that are not in memory and are just decorations, in any case the code below works, but not very 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 is just a wrapper that we put around the DataContext for mocking purposes, so feel free to change this just for the DataContext. This is written extremely defensively as it pulls in many members that don't have all the data it wants, so it should filter it out and only work on those that do.

+2


source







All Articles