none
Removing generated classes RRS feed

  • Question

  • Hi, I have generated classes from an existing database using entity framework core and am looking to eliminate some of the classes and some class members that I don't need(such as navigation properties). I was just wondering how I would go about doing this? What do I change so that I can do this?

    Thank you

    Friday, August 11, 2017 9:37 PM

All replies

  • if there is a model that is viewable in a designer, then you would uncheck the DB table or tables, update the model and verify the model. 
    Friday, August 11, 2017 11:56 PM
  • Hi Sam_Mallabone,

    >>I was just wondering how I would go about doing this? What do I change so that I can do this?

    If you want to scaffold special table, you could use -t parameter, like this:

    Scaffold-DbContext "Server=(localdb)\mssqllocaldb;Database=Blogging;Trusted_Connection=True;" Microsoft.EntityFrameworkCore.SqlServer -t ('table1','table2','table3')

    For more information, please refer to:

    https://docs.microsoft.com/en-us/ef/core/miscellaneous/cli/powershell#scaffold-dbcontext

    Best regards,

    Cole


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, August 14, 2017 10:28 AM
    Moderator
  • Hi,

    I was able to remove entities in the model but am now running into issues trying to remove member variables in some of the classes. I have some navigation methods within some of my classes and am looking to remove them, is there an easy way to do this? 

    Thanks for the help,

    Sam

    Monday, August 14, 2017 3:26 PM
  •  I have some navigation methods within some of my classes and am looking to remove them, is there an easy way to do this? 

    What are you talking about? You have some navigation methods in EF auto-generated classes? This is something you did?

    Monday, August 14, 2017 5:52 PM
  • Basically, when Entity Framework Core generated classes from an existing database, it included some navigation member variables that I want to remove as they are unnecessary. From examples, here is one of the classes that has one: 
    public class SerialNumbers
        {
            public SerialNumbers()
            {
                Features = new HashSet<Features>();
            }
            /// <summary>
            /// Serial Number ID
            /// </summary>
    
            public Guid Id { get; set; }
    
            /// <summary>
            /// The Contracts Id
            /// </summary>
            public Guid ContractId { get; set; }
    
            /// <summary>
            /// The User's ID
            /// </summary>
            public Guid UserId { get; set; }
    
            public virtual ICollection<Features> Features { get; set; }
    
            
            public virtual Contracts Contract { get; set; }
    
           
            public virtual Users User { get; set; }
        }
    User and Contract are not necessary as I never use them so I am trying to remove them. However, whenever I try to remove them, I get SQL exceptions when I call link methods and I believe this is due to Entity Framework being unable to resolve the tables schema. I would just like to remove these navigation variables without any problems.
    Monday, August 14, 2017 6:06 PM
  • I have not used EF core. So I can't tell you what to do, but I doubt that they are navigation properties.
    Monday, August 14, 2017 6:50 PM
  • Hi Sam_Mallabone,

    Do you scaffold special table via Scaffold-DbContext, which does not generate navigate properties if you does not scaffold related table.

    if it still does not work, could you please provide a bit more information about related detailed error message, related code.

    Best regards,

    Cole


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, August 15, 2017 3:22 AM
    Moderator