none
Cannot Customize EF Model's Partial Classes in VS2012 RRS feed

  • Question

  • I am using VS 2012 Express and have a website using the StarterSite.sdf SQL CE 4.0 database for testing purposes.  Best I can tell the Entity Framework version is 4.0

    I went into the App_code folder and added a new item, ADO.Net Entity Data Model.  The wizard came up and I selected "generate from database"  I selected the StarterSite db and completed the wizard, to include checking OK on the two template execution prompts that came up.

    The generated model had the following properties:
       EntityContainerName: StarterSiteEntities
       NameSpace: StarterSiteModel

    I then saved the model and tested it against some code and all worked fine.

    I then add a new file to the App_code folder, "myModelCustomizer.cs".

    In that file I used the same approach I have used in VS 2010 on other projects, declaring public partial classes on the generated c lasses and adding custom methods and properties.  Here is a (nonsensical) example, attempting to "extend" the UserProfile generated entity with an additional property, userName:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using StarterSiteModel;
    namespace StarterSiteModel
    {
        public partial class UserProfile
        {
            string userName
            {
                get
                {
                    return "x";
                }
            }
        }
    }
    


    I was immediately suspicious because intellisense did not seem to be aware of the namespace nor of the fact that UserProfile was a partial class in the generated model.  However, it clearly IS in the generated Model.cs file under the Model.tt folder:

    public partial class UserProfile
    {
        public UserProfile()
        {
            this.webpages_Roles = new HashSet<webpages_Roles>();
        }
    
        public string Email { get; set; }
        public int UserId { get; set; }
    	. . . 
    }
    

    My suspicions were confirmed when I tried to use the new property in a webpage and the compiler complained that my newly added, customized UserProfile.userName was invalid:

    Error	4 'UserProfile' does not contain a definition for 'userName' 
    and no extension method 'userName' accepting a first argument of
    type 'UserProfile' could be found


    What am I missing or doing wrong here?  In previous projects in VS 2010 Express, I have been able to customize the partial classes generated in my Entity model.  However, in VS 2012 I cannot customize the generated partial classes.  It seems like the namespace is not being recognized.



    RRR

    Friday, May 3, 2013 3:32 PM

Answers

  • I can't explain why, but removing the

    namespace StarterSiteModel

    {

    }

    from around the customizing code fixed it.

    Somehow, EF 5 makes that wrapper unnecessary (and, in fact, fatal to one's customization efforts).  Once I "unwrapped" the code, everything worked as expected.

    Problem solved.  Knowledge gap not reduced.


    RRR

    Monday, May 6, 2013 1:05 PM

All replies

  • Hi ROBSR,

    Unfortunately I cannot reproduce the issue.

    Are the two partial classes designed in the same namespace?

    Where are you using this property? I can see that userName property is private and cannot be accessed outside of the class.

    If the problem cannot be solved, would you like to upload a demo project to reproduce this issue to SkyDrive? We can have a better understanding with this problem.

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by ROBSR Monday, May 6, 2013 1:02 PM
    • Unmarked as answer by ROBSR Monday, May 6, 2013 1:02 PM
    Monday, May 6, 2013 9:04 AM
    Moderator
  • I can't explain why, but removing the

    namespace StarterSiteModel

    {

    }

    from around the customizing code fixed it.

    Somehow, EF 5 makes that wrapper unnecessary (and, in fact, fatal to one's customization efforts).  Once I "unwrapped" the code, everything worked as expected.

    Problem solved.  Knowledge gap not reduced.


    RRR

    Monday, May 6, 2013 1:05 PM
  • Hi ROBSR,

    Base on my knowledge, this class will reside in global namespace if removing the code snippet. I'm not sure why there is such behavior.

    Thanks for sharing your solution anyway. Have a nice day!

    Best regards,


    Chester Hong
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.


    Tuesday, May 7, 2013 6:58 AM
    Moderator