none
Adding table to DBML does NOT update designer.cs code-behind file

    Question

  • title says it all.  I have a dbml mapping with about 40 tables in it that I have been using and updating successfully for the past couple of months.  Today, I drag'n'drop a new table into it and (using VS2008SP1 and TFS 2008 source control):
    1 - the dbml, cs, and layout files are checked out
    2 - these dbml and layout files are updated with the new table and associations
    3 - the designer.cs file is not checked out and is not updated - no classes generated.

    I can create a new dbml file and drag the new table plus 2 related tables and it generates the code just fine.

    what gives?


    Toby
    Friday, June 26, 2009 9:45 PM

Answers

  • Have you compared the entries in the .csproj file for the dbml file and for the generated code? Is there any difference between the entries for the .dbml/.designer.cs files where codegen works and the ones where it don't?

    There's one bug where the LastGenOutput setting sometimes get a "1" appended to the filename - not sure exactly what triggers it but changing it back will fix it if that is the case.

        <Compile Include="SomeDataContextFile.designer.cs">
          <AutoGen>True</AutoGen>
          <DesignTime>True</DesignTime>
          <DependentUpon>SomeDataContextFile.dbml</DependentUpon>
        </Compile>

        <None Include="SomeDataContextFile.dbml">
          <Generator>MSLinqToSQLGenerator</Generator>
          <LastGenOutput>SomeDataContextFile.designer.cs</LastGenOutput>
          <SubType>Designer</SubType>
        </None>


    Kristofer - Huagati Systems Co., Ltd. - Cool tools for Linq-to-SQL and Entity Framework: www.huagati.com/dbmltools (VS designer add-in), www.huagati.com/L2SProfiler (query profiler for L2S)
    • Marked as answer by TobyKraft Monday, June 29, 2009 1:46 PM
    Monday, June 29, 2009 2:37 AM
    Answerer

All replies

  • I already found one and reported it on Microsoft connect and it sounds similar:

    DBML display does not show new foreign key constraints

    Here is my text, resolution, one has to build with the improper items, delete them, re-add them and build again:

    I am updating my database and changing the FK constraints on the fly. VS2008 does not seem to see those changes immediately nor allow me to easily update the dbml file. Even a refresh to the Server Explorer in VS2008 does not get constraints. I refresh and drag and drop the tables in question onto the dbml file in VS2008, but it has no affect even though SQL Server Management studio shows the FK constraints on the tables!

    Its as if the tables are cached in VS2008.

    The problem will go away if I do these steps
    1) Build the project with the tables not showing the foreign key constraints.
    2) Remove tables in question.
    3) Drag/Drop the tables back to the dbml file.


    It will be fixed in VS2010. Can you wait? :-p

    William Wegerson (www.OmegaCoder.Com)
    Friday, June 26, 2009 10:28 PM
    Moderator
  • Given that he is dragging out a whole new table I'm not sure that is the issue.

    Toby, one thing you can try is to go to the properties for the new DBML file and copy the "CustomTool" properties contents to the same field in the one that doesn't work.  It is possible to wipe this out by mistake which will stop the code generation updating.

    [)amien
    Saturday, June 27, 2009 6:09 AM
    Moderator
  • thanks, doesn't appear to be the problem though since the CustomTool property is set to MSLinqToSQLGenerator on the broken dbml.

    Toby
    Saturday, June 27, 2009 7:03 PM
  • Thanks but I don't think this applies.  Dropping table onto design surface does not generate the code.

    Toby
    Saturday, June 27, 2009 7:27 PM
  • Do you get any error message (e.g. "the custom tool mslinqtosqlgenerator failed...")?

    Take a look at this connect item and see if that's what you ran into?
    https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=361577

    I also have a weak memory of some similar issue where starting devenv.exe once with the /ResetSkipPkgs command line switch repaired VS. But that would make sense only if it fails consistently - if codegen works for one file but not for another one then it is more likely something project-specific...


    Kristofer - Huagati Systems Co., Ltd. - Cool tools for Linq-to-SQL and Entity Framework: www.huagati.com/dbmltools (VS designer add-in), www.huagati.com/L2SProfiler (query profiler for L2S)
    Sunday, June 28, 2009 8:57 AM
    Answerer
  • Try creating a new solution/project and see if the same happens to that.


    William Wegerson (www.OmegaCoder.Com)
    Sunday, June 28, 2009 11:25 AM
    Moderator
  • Thanks, well I can create a new dbml in the same project, accessing the same database, and drop the new table into that designer and the code is generated.  dropping the same table into the existing dbml - nothing is generated.  So it's specific to the dbml file apparently.

    Toby
    Monday, June 29, 2009 1:15 AM
  • Have you compared the entries in the .csproj file for the dbml file and for the generated code? Is there any difference between the entries for the .dbml/.designer.cs files where codegen works and the ones where it don't?

    There's one bug where the LastGenOutput setting sometimes get a "1" appended to the filename - not sure exactly what triggers it but changing it back will fix it if that is the case.

        <Compile Include="SomeDataContextFile.designer.cs">
          <AutoGen>True</AutoGen>
          <DesignTime>True</DesignTime>
          <DependentUpon>SomeDataContextFile.dbml</DependentUpon>
        </Compile>

        <None Include="SomeDataContextFile.dbml">
          <Generator>MSLinqToSQLGenerator</Generator>
          <LastGenOutput>SomeDataContextFile.designer.cs</LastGenOutput>
          <SubType>Designer</SubType>
        </None>


    Kristofer - Huagati Systems Co., Ltd. - Cool tools for Linq-to-SQL and Entity Framework: www.huagati.com/dbmltools (VS designer add-in), www.huagati.com/L2SProfiler (query profiler for L2S)
    • Marked as answer by TobyKraft Monday, June 29, 2009 1:46 PM
    Monday, June 29, 2009 2:37 AM
    Answerer
  • Eureka! - that was the clue I needed.  The <LastGenOutput> node was missing from the <itemgroup> entry for the dbml file.  (somehow it got dropped on a prior edit/checkin)  I added the LastGenOutput, dragged the new table onto the designer, and this time it worked! 
    Thanks, Kristofer.
    Toby
    Monday, June 29, 2009 1:55 PM
  • What if there is no .csproj file to modify? I am using Linq to Sql on an Asp.Net project. There is no .csproj file to modify. I have the same problem. What should I do then?
    Monday, July 13, 2009 9:02 AM
  • What if there is no .csproj file to modify? I am using Linq to Sql on an Asp.Net project. There is no .csproj file to modify. I have the same problem. What should I do then?

    Did you try the workaround described in MS Connect #361577 - https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=361577&wa=wsignin1.0 ?

    Also, check if you have a .cs file using the same name as the datacontext, but that is not the auto-generated .designer.cs file. If so, try giving that file a different name.
    Kristofer - Huagati Systems Co., Ltd. - Cool tools for Linq-to-SQL and Entity Framework: www.huagati.com/dbmltools (VS designer add-in), www.huagati.com/L2SProfiler (query profiler for L2S)
    Monday, July 13, 2009 11:53 AM
    Answerer
  • What if there is no .csproj file to modify? I am using Linq to Sql on an Asp.Net project. There is no .csproj file to modify. I have the same problem. What should I do then?
    There is a .csproj in asp.net during development mode. It is not published to the website.

    William Wegerson (www.OmegaCoder.Com)
    Monday, July 13, 2009 12:13 PM
    Moderator
  • Hi Kristofer and OmegaMan,
    Kristofer, the link that you sent me returns an error message and I dont have any other class using the same name as datacontext. But I do have a aspx page as the same name with datacontext; like "Application.dbml" and "Application.aspx.cs"(in a saperate folder). Hope, this is not the problem...

    OmegaMan, I am using Team System and the codes are stored on a saperate server. I opened the project from a source control. I look at the path that the code files are stored in my computer, but there is no .csproj file. I think the problem is that the project was not created as a "web project", but it was created as a "website" application. When I create a project via "New->Project->Web" I can see that VS2008 creates a .csproj file in working folder. But not via "New->Web Site".

    Thanks for your help
    I dont know what to do though...
    Monday, July 13, 2009 12:53 PM
  • keskinTr,
    the filenames shouldn't be an issue since the class names would be different.  I assume you do not get any compile errors.
    So, you have a Web Site, added a linq to sql (l2s) class to the App_Code directory, added tables to the l2s class, use the l2s classes in the app, everything works fine.  Then at some point you have added another table to the l2s class, and the code for the table does not get added to the l2s class.  Is that correct?

    If you right-click the dbml file in solution explorer and Run Custom Tool, do you get any errors?

    When you add a table, you have to Build the web site for it to appear in Intellisense (The build action is when the custom tool runs). 

    Have you tried deleting/recreating the l2s class?

    Tell us more about what you are actually doing/seeing.

    Toby
    Toby
    Monday, July 13, 2009 3:31 PM
  • Well the celebration was premature...

    The Linq-to-Sql support in VS2008 is very fragile.  I had this dbml class working fine with the added table, but further updates pull you back into the black hole of corruption done by the custom tool.  By that I mean, just something simple like adding a property to an existing table, save the dbml, then the designer.cs file is DELETED .    (Yes, right there in the Pending Changes window of TFS and missing from Solution Explorer)
    Digging into the csproj file and comparing a dbml that seems to work vs the broken one I see some differences:
    a - working one did not have a partial class for the datacontext, so the designer.cs was dependent upon the dbml file - vs the broken one that did have a partial class and the designer.cs was dependent on the context.cs partial class file which was in turn dependent on the dbml file.
    b - the AutoGen and DesignTime nodes for the designer.cs file compile entry vanished out of the csproj file.

    will dig some more to see if I can conjure up a fixed csproj file that works and doesn't break at the slightest touch....


    Toby
    Monday, July 13, 2009 8:54 PM
  • Ah Ha! - If you put the using statements outside the namespace in the datacontext partial class, the Custom Tool will delete the designer file when you run it!
    Clues -
    partial class by itself doesn't cause a problem,
    designer.cs has usings inside the Namespace.

    Some other tidbits from my spelunking...
    In my project, I have a folder for each database.  When you add the Linq-to-Sql class, it sets up the namespace as <project name space>.<folder name>.
    But when you click on View Code on the designer, it doesn't recognize that the dbml is in a folder and creates the partial data context class in the <project name space>.    Looks like a bug to me!






    Toby
    Monday, July 13, 2009 9:56 PM
  • There is a .csproj in asp.net during development mode. It is not published to the website.

    William Wegerson (www.OmegaCoder.Com)

    Only in asp.net web-app projects. asp.net website projects just reads all files from the directory where the project is located.
    Kristofer - Huagati Systems Co., Ltd. - Cool tools for Linq-to-SQL and Entity Framework: www.huagati.com/dbmltools (VS designer add-in), www.huagati.com/L2SProfiler (query profiler for L2S)
    Tuesday, July 14, 2009 4:43 AM
    Answerer
  • Hi Kristofer and OmegaMan,
    Kristofer, the link that you sent me returns an error message and I dont have any other class using the same name as datacontext. But I do have a aspx page as the same name with datacontext; like "Application.dbml" and "Application.aspx.cs"(in a saperate folder). Hope, this is not the problem...

    OmegaMan, I am using Team System and the codes are stored on a saperate server. I opened the project from a source control. I look at the path that the code files are stored in my computer, but there is no .csproj file. I think the problem is that the project was not created as a "web project", but it was created as a "website" application. When I create a project via "New->Project->Web" I can see that VS2008 creates a .csproj file in working folder. But not via "New->Web Site".

    Thanks for your help
    I dont know what to do though...

    If connect link above shows an error, make sure you have registered on the connect site ( http://connect.microsoft.com/ ) and subscribe to the visual studio and .net framework connection. After that the link should work. Anyway, the workaround described behind that link is to move any using statements inside the namespace declaration if you have a partial datacontext class with your own code in...  Odd thing but that's the way it works... :)

    As for the filenames - if they're in separate folders it shouldn't matter. But I once got some funny issues when I had files in the same folder called [datacontext].dbml, [datacontext].designer.cs, [datacontext].cs etc. Renaming the latter to sometihng else fixed it.
    Kristofer - Huagati Systems Co., Ltd. - Cool tools for Linq-to-SQL and Entity Framework: www.huagati.com/dbmltools (VS designer add-in), www.huagati.com/L2SProfiler (query profiler for L2S)
    Tuesday, July 14, 2009 4:47 AM
    Answerer
  • If you're having trouble with the link, try without the https:

    http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=361577
    Toby
    Tuesday, July 14, 2009 1:24 PM
  • Make sure that your new table that you're adding to the designer DBML has a primary key.  If the table has no PK, the designer will not create the association and therefore you end up with the entity but no association (via any foreign key) to other entities or any CRUD methods for your new entity.

    I tried to drag a new table from the server explorer to an existing DBML designer, and while it added the table an created the association in the designer layout, the code behind was not created.  When I right click the DBML and "Run Custom Tool" I get the actual warning that a PK was not found for my entity and so it won't create the association.

    HTH

    --Thiago
    Thursday, August 13, 2009 7:26 PM