none
Best Practice For Doing This? RRS feed

  • Question

  • I am seeing a lot of posts related to this issue, and a couple of techniques mentioned in order to work around it, but I haven't seen any concrete examples.

     

    A classic problem:  if I use the built-in ORM to generate my classes from the database, I wind up with a bunch of object names that aren't very useful.  If I change them in the designer file, of course those changes are lost any time I have to regenerate it.

    As an example, the main object in my database is a Request.  I also have a Users table.  Seven different users are tied to any given Request which is of course handled via foreign keys to the Users table.  The problem of course is when the Request object code is generated, my associated objects become:  User, User1, User2, User3, etc.  In other words pretty useless when it comes to actually using those objects in code since I have no way of knowing which User is the one I actually need.

    Since my budget on this is ZERO, I am unable to use any external tools I would have to purchase like CodeSmith, etc. 

    From what I can tell so far, I see two main ways to do this:

    1 - Create my own class code, and decorate it with attributes which tie it to the database.

    2 - Use an external XML mapping file to map class code to database fields.

    Option 1 seems more attractive to me, since there is no love lost between me and XML.   ;)

     

    So my question is:  is there some way to initially generate that code so I don't have to start completely from scratch?  I see references to SqlMetal here and there, but again....no concrete examples of exactly what it does.  I don't mind maintaining it by hand but I'd much rather not have to start with completely empty class files.  I have seen some amazingly simplified examples, but comparing that to all the code the Designer generates makes me wonder what I'd be missing if I followed those overly simplistic examples.  Assuming I can generate the initial code by hand, how do future changes to the database get integrated into it exactly?  Something simple like adding a column to a table. 

    Thanks in advance.

    Friday, August 20, 2010 3:44 PM

Answers

  • Well, not knowing xml will hurt you a lot because a dbml file is a xml file and if you want to define custom logic to validate your db a xsd file is needed.

    However read this threads, there are links and information that is related to this and you might find helpful

    http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/2b872866-079e-42cc-a3c2-a2157626dfc1

    http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/f2bcf30d-6220-47b9-b676-54b67192ac4c

    Regards

    Friday, August 20, 2010 4:10 PM
  • Hi.

    Does the request object always link to seven users or might it start of with just one and gradually other users get attached.  The reason why I am asking is it might be at an advantage to use a linking table (association table) instead.

    Coming back to your question:  What you can do to start of with, is to use the built-in ORM to generate the classes and then go into the designer.cs file of the .dbml file.  This will allow you to see the classes that are generated for you.  Once you obtain their code, simply copy each class to a new class file in your solution and all done. Then you can make the necessary changes without having to use the built-in ORM each subsequent time.

    Just take note that the .dbml file will contain a class that inherits from DataContext.  You don't copy the class, since you can use, new DataContext(...).GetTable(..); , which will create a temporary DataContext object whenever you need it.

    Regards.

    Friday, August 20, 2010 7:38 PM

All replies

  • Well, not knowing xml will hurt you a lot because a dbml file is a xml file and if you want to define custom logic to validate your db a xsd file is needed.

    However read this threads, there are links and information that is related to this and you might find helpful

    http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/2b872866-079e-42cc-a3c2-a2157626dfc1

    http://social.msdn.microsoft.com/Forums/en-US/linqtosql/thread/f2bcf30d-6220-47b9-b676-54b67192ac4c

    Regards

    Friday, August 20, 2010 4:10 PM
  • Hi.

    Does the request object always link to seven users or might it start of with just one and gradually other users get attached.  The reason why I am asking is it might be at an advantage to use a linking table (association table) instead.

    Coming back to your question:  What you can do to start of with, is to use the built-in ORM to generate the classes and then go into the designer.cs file of the .dbml file.  This will allow you to see the classes that are generated for you.  Once you obtain their code, simply copy each class to a new class file in your solution and all done. Then you can make the necessary changes without having to use the built-in ORM each subsequent time.

    Just take note that the .dbml file will contain a class that inherits from DataContext.  You don't copy the class, since you can use, new DataContext(...).GetTable(..); , which will create a temporary DataContext object whenever you need it.

    Regards.

    Friday, August 20, 2010 7:38 PM
  • Hi vHab, 

    I'm writing to follow up the post. Does the above suggestion work ?

    Please feel free to let me know if you need any help.

    Best regards,
    Alex Liang

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, August 26, 2010 3:33 AM
    Moderator
  • Hi vHab,

    I am marking the replies as answer. If you have questions, please feel free to let me know.

    Best regards,
    Alex Liang

    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Monday, August 30, 2010 1:16 PM
    Moderator