none
Can I modify the DMBL.cs File? RRS feed

  • Question

  • Hello OmegaMan,
    What if i modified the ...Designer.cs source code?

    If i declare an instance field corresponding to the newly added table column (of course with the right type), add the following two events to it and then  adding a property in the code generated file for the newly added column with the right attribute attached to it, such as the one shown below.
    Where, SCode is the newly added column of type int.
    Note that i added the following events and declared a corresponding variable to the newly added column called "SCode":

     

     

    partial void OnSCodeChanging(System.Nullable<int> value); 
    partial void OnSCodeChanged(); 
    private System.Nullable<int> _SCode;
    [Column(Storage = "_SCode", DbType = "Int")] 
    public System.Nullable<int> SCode
    {
    get
    {
    return this._SCode;
    }
    set
    {
    if ((this._SCode != value))
    {
     
    this.OnSCodeChanging(value); 
    this.SendPropertyChanging(); 
    this._SCode = value; 
    this.SendPropertyChanged("SCode"); 
    this.OnSCodeChanged();
    }
    }
    }

    Is this way of doing it wrong or dangerous for any reason? Because, designer codes usually are meant to remain unchanged for they might be easlly messed up. Do you think this will have any problem?
    I am trying this method just because, <dbName>.dbml.layout is showing me an XML file NOT the ORM designer where i can easly delete and re-add the table.

    Thanks,
    Zolyita

     

    Tuesday, January 12, 2010 10:04 PM

Answers

  • I would advise against trying to do any direct changes to the generated dbml code behind file. It should be always be on par with what is in the database. If you take that route, what will happen is that you will make a mistake and when trying to save to the database, it will, simply fail. Use the designer do what it needs and update it properly.

    With that said, one can extend an entity away from the dbml background file though. I provide an example on my blog entitled Tribal Knowledge: Linq Entities To Do Your Bidding via the Partial Keyword in C# . HTH
    William Wegerson (www.OmegaCoder.Com)
    Tuesday, January 12, 2010 10:20 PM
    Moderator
  • I finally learn the following and thought it might be useful if i posted it here:
    It is possibile to change the dbml file (opening it in XML --not in OR/M designer), add the necessary changes you want to make, save it and finally go to visual studio command prompt and execute the following command:

    sqlmetal /code:<FilePath for xyz.designer.cs> <FilePath for xyz.dbml>
    where xyz is your database name. This command will re-generate the necessary class(es) and properties for the existing dbml file that you have changed (in xml editor). This way, you will have more control to the things you need to change than either to use the designer or manually go the designer.cs code and make the changes line by line. But, having said that, i really love the designer as well :).

    Thanks,
    Zolyita

    • Marked as answer by Bsharper Saturday, December 29, 2012 12:42 AM
    Thursday, January 21, 2010 7:46 PM

All replies

  • I would advise against trying to do any direct changes to the generated dbml code behind file. It should be always be on par with what is in the database. If you take that route, what will happen is that you will make a mistake and when trying to save to the database, it will, simply fail. Use the designer do what it needs and update it properly.

    With that said, one can extend an entity away from the dbml background file though. I provide an example on my blog entitled Tribal Knowledge: Linq Entities To Do Your Bidding via the Partial Keyword in C# . HTH
    William Wegerson (www.OmegaCoder.Com)
    Tuesday, January 12, 2010 10:20 PM
    Moderator
  • Okay, i understand that but my db.dbml.layout doesn't open me the OR designer where i can delete and re-add my altered table. Instead of opening the ORM designer where we can drag drop tables, it is opening an XML file. What is wrong and how can i solve this problem.

    Thanks,
    Zolyita
    • Marked as answer by Bsharper Wednesday, January 13, 2010 4:28 AM
    • Unmarked as answer by OmegaManModerator Wednesday, January 13, 2010 2:44 PM
    Tuesday, January 12, 2010 10:36 PM
  • Delete the dbml file and start over.
    William Wegerson (www.OmegaCoder.Com)
    Tuesday, January 12, 2010 11:38 PM
    Moderator
  • I finally learn the following and thought it might be useful if i posted it here:
    It is possibile to change the dbml file (opening it in XML --not in OR/M designer), add the necessary changes you want to make, save it and finally go to visual studio command prompt and execute the following command:

    sqlmetal /code:<FilePath for xyz.designer.cs> <FilePath for xyz.dbml>
    where xyz is your database name. This command will re-generate the necessary class(es) and properties for the existing dbml file that you have changed (in xml editor). This way, you will have more control to the things you need to change than either to use the designer or manually go the designer.cs code and make the changes line by line. But, having said that, i really love the designer as well :).

    Thanks,
    Zolyita

    • Marked as answer by Bsharper Saturday, December 29, 2012 12:42 AM
    Thursday, January 21, 2010 7:46 PM
  • The more one understand the options of what can be done, the better the solution which can be achieved. Glad you were able to navigate it and go forward.
    William Wegerson (www.OmegaCoder.Com)
    Thursday, January 21, 2010 8:12 PM
    Moderator