none
Sqlmetal for LinqToSql removes the IsDiscriminator=true attribute on a column RRS feed

  • Question

  • Hello,

      We are using sqlce for our app. And we are using LinqToSql. We are setting the "IsDiscriminator=true" for a column of a particular table after we generate the linq.cs from sqlmetal.exe for C#.

    Everytime we modify the database schema and have to re-generate linq.cs, we will have to manually go and add all the attributes back.

    We edit the schema from Visual Studio 2008.

      What do I do so that I dont have to set this everytime our db schema changes. I do not want to write a post create script which updates based on regex etc
    Monday, May 30, 2011 3:26 AM

Answers

  • Ohh, this thiread is still open?, hmm I hope the OP is still reading this. Ok I will explain the three methods to obtain this result from most desirable to less desirable. I tested this and every method explained solves this problem.

    1. Go to the dbml file, find the column you want and add isdiscriminator=true to the attribute list, that way whenever the cs file is regenerated, that attribute will be added (I recommend this way)
    2. Do the mapping yourself, yes you have to do it all but you get absolute control as to how the table is represented (A lot of work and negates part of the model of VS but sometimes this is needed) Use this only if a lot of control is needed.
    3. A compromise between method 1 and 2, go to the dbml file find the column you want and delete the whole line, that way that column won't be mapped to a property and using the fact that the class is partial you can define that property in a separate file.  You get absolute control for the needed property and autogenerated code but this break the design view as this column won't be shown there but it will exist.  I don't like this method as it can lead to confusion but sometimes this is the only practical way. Use with caution and document it well so that other developers reading your code know what you have done

    I hope that with this explanation this thread can be closed as this solves your problem, thanks for the patience, sorry I couldn't post sooner however if you need anything else let me know.

    Regards

    Monday, June 20, 2011 12:21 PM

All replies

  • Hello Abhilash,

    Thanks for your post.

    As far as I know, the attribute "IsDiscriminator" is false by default. Please see this: http://msdn.microsoft.com/en-us/library/system.data.linq.mapping.columnattribute.isdiscriminator.aspx So if you want to remove this attribute, just don't set it.

    I hope this can help you.

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, May 31, 2011 1:54 AM
    Moderator
  • Hello Jackie,

     

                  I am aware that it is false by default. I want to set it to true. The issue we are facing is, whenever we modify the schema, we re-run sqlmetal..  which generates the linq.cs without the isdiscriminator=true for some of the tables. This we manually add once linq.cs is generated.

    Tuesday, May 31, 2011 3:06 AM
  • Hi Abhilash,

    Thanks for your feedbacks.

    As far as I know, there is an attribute of column named IsInheritanceDefault:

    IsInheritanceDefault attribute
     
    True if this type is use to represent the database row when the discriminator code is not specified in the mapping. Exactly one Type in the inheritance hierarchy must have IsInheritanceDefault set to true, unless no hierarchy is present.

    I think you can try to set the two attributes to true at the same time. And then run again. Any feedbacks please feel free to tell me.

    I hope this can help you.

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Thursday, June 2, 2011 9:25 AM
    Moderator
  • Hello Jackie,

     

        I dont think the issue has been understood properly / not explained properly by me.

     

        I want a column of a particular table to be marked as isdiscriminator=true. I do not need the IsInheritanceDefault  attribute.

     

        Whenever we modify the schema and we re-generate linq.cs, the isdiscriminator=true attribute has to be put back manually by me. Is there any way, where I dont have to do this every time, and it gets added automatically

    Tuesday, June 14, 2011 12:07 PM
  • Sure, when you define the class put the attribute to true, use the fact that the class is partial and set the attribute in a different file so that when you regenerate the rest of the cs files this does not change.

    Regards

    Tuesday, June 14, 2011 2:08 PM
  • Thanks Serguey for your explaination.

    @Abhilash, if you have any time, could you please have a try? Any feedbacks please feel free to tell me.

     

    Good day,

     


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, June 15, 2011 3:26 AM
    Moderator
  • @Serguey123

              Since i have to set the isdiscriminator=true for a column of a table. Since each column in the table, becomes a property, and the attributes are defined for that. Lets say the generated file is Linq.cs. Now my partial class is MyClass.cs. How do I set an attribute to the property in MyClass.cs. I searched a bit, found that property attributes cannot be re/defined in the partial class

    Wednesday, June 15, 2011 4:01 AM
  • The problem is that there is no such thing as a partial property at least for now.  However I believe I did this before using custom mapping to define the column in a separate file, that way it wouldn't be autogenerated.  I think I have the sample somewhere, I'll dig it or replicate and post the code if you want but I'll have to be tomorrow as I'm really busy and for some reason I'm not getting alerts so it might take me a while to answer your posts.

    @Jackie you are welcome

    Regards

    Wednesday, June 15, 2011 4:16 PM
  • @Serguey Not an issue. Will check back tomorrow. Thanks for checking. Even if you can post some links, i can read up
    Wednesday, June 15, 2011 5:17 PM
  • Ok, thanks for your patience, now let me explain how to achieve this

    The idea is to use custom mapping to override the default mapping, that way you can have the cake and eat it too, if you define the column attribute in the generation files or ommit the column mapping and do the mapping yourself in a different file

    You should read this http://msdn.microsoft.com/en-us/library/bb399400.aspx in order to learn how the code is generated

    http://msdn.microsoft.com/en-us/library/bb546174.aspx This as well

    I'll try to put an example later on...

    Regards

    Thursday, June 16, 2011 1:40 PM
  • Ohh, this thiread is still open?, hmm I hope the OP is still reading this. Ok I will explain the three methods to obtain this result from most desirable to less desirable. I tested this and every method explained solves this problem.

    1. Go to the dbml file, find the column you want and add isdiscriminator=true to the attribute list, that way whenever the cs file is regenerated, that attribute will be added (I recommend this way)
    2. Do the mapping yourself, yes you have to do it all but you get absolute control as to how the table is represented (A lot of work and negates part of the model of VS but sometimes this is needed) Use this only if a lot of control is needed.
    3. A compromise between method 1 and 2, go to the dbml file find the column you want and delete the whole line, that way that column won't be mapped to a property and using the fact that the class is partial you can define that property in a separate file.  You get absolute control for the needed property and autogenerated code but this break the design view as this column won't be shown there but it will exist.  I don't like this method as it can lead to confusion but sometimes this is the only practical way. Use with caution and document it well so that other developers reading your code know what you have done

    I hope that with this explanation this thread can be closed as this solves your problem, thanks for the patience, sorry I couldn't post sooner however if you need anything else let me know.

    Regards

    Monday, June 20, 2011 12:21 PM
  • @Serguey :

    Thanks for all the suggestions. We are writing a script which will be run as soon as sqlmetal is run, to go set the isdiscriminator value

    Tuesday, June 21, 2011 7:48 AM