none
EF code-first: how to save two entity's data in one table? RRS feed

  • Question

  • I have two entities called ControllerBlock and ControllerAction.

    See the GetControllerBlocks() function then you can understand how I am populating two classes' data like master details. How can I use EF to store these two entities' data into one table?

    Table structure like

    ControllerName    ActionName
    --------------    ------------
    Home                Index
    Home                DasBoard
    Home                Chart
    HR                  Leave
    HR                  Loan
    Payroll             view
    Payroll             Edit
    Payroll             Process

    My sample code:

    public class ControllerBlock
    {
        public string ControllerName { get; set; }
        public List<ControllerAction> ControllerActions { get; set; }
    }
    
    public class ControllerAction
    {
        public string ActionName { get; set; }
    }
    
    public List<ControllerBlock> GetControllerBlocks()
    {
        // Set ActionActive herem, if necessary
        List<ControllerAction> homeActions = new List<ControllerAction>() {
            new ControllerAction { ActionName = "Index" },
            new ControllerAction {  ActionName = "DashBoard" },
            new ControllerAction { ActionName = "Chart" }
        };
    
        List<ControllerAction> hrActions = new List<ControllerAction>() {
            new ControllerAction { ActionName = "Leave" },
            new ControllerAction { ActionName = "Loan" },
            new ControllerAction { ActionName = "NoticeBoard" }
        };
    
        List<ControllerAction> payRollActions = new List<ControllerAction>() {
            new ControllerAction { ActionName = "View" },
            new ControllerAction {  ActionName = "Edit" },
            new ControllerAction { ActionName = "Process" }
        };
    
        List<ControllerBlock> actionBlocks = new List<ControllerBlock>()
        {
            new ControllerBlock(){ControllerName = "Home", ControllerActions = homeActions},
            new ControllerBlock(){ControllerName = "HR",  ControllerActions =  hrActions},
            new ControllerBlock(){ControllerName = "PayRoll",  ControllerActions =  payRollActions}
        };
    
        return actionBlocks;
    }

    I am working with EF code first so suggest best way to insert data in one table when I have two entities. If possible add some code hint for inserting the data into one table from two classes. Thanks

    Monday, July 3, 2017 3:46 PM

Answers

All replies

  • Hi Sudip_inn,

    Based on your description, you use a one-to-many relationship on your entity framework, and entity framework does not support two entities into one table with navigate property, which will be generate foreign key.

    if you want to use one table, please use one entity, like this:

    public class controller_Action 
    {
       public int Id{get;set;}
       public string ControllerName { get; set; }
       public string  ActionName { get; set; }
    }
    

    Best regards,

    Cole Wu


    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, July 4, 2017 9:06 AM
    Moderator
  • NO, two entity class data can be saved in one table. it is possible.

    check google what i said.

    Wednesday, November 1, 2017 8:56 AM
  • NO, two entity class data can be saved in one table. it is possible.

    check google what i said.


    Yeah it's possible if you put all the columns in one table, and save one table record. Other than that in using EF, what you are talking about is not possible.
    Wednesday, November 1, 2017 1:29 PM
  • i am trying to say two different class data can be saved to single table. say i have two class customer and address. so i can save customer and address data in same db table.
    Thursday, November 2, 2017 12:39 PM
  • i am trying to say two different class data can be saved to single table. say i have two class customer and address. so i can save customer and address data in same db table.

    https://weblogs.asp.net/manavi/inheritance-mapping-strategies-with-entity-framework-code-first-ctp5-part-1-table-per-hierarchy-tph

    If you are using DB first you can do the same thing too with TPH.

    • Marked as answer by Sudip_inn Friday, November 3, 2017 12:46 PM
    Thursday, November 2, 2017 12:57 PM