locked
EF Code First: How to save two entity data in one table RRS feed

  • Question

  • User828542847 posted

    i have two entities called 

    ControllerBlock and ControllerAction . see GetControllerBlocks()

     function then can understand how i am populating two class data like master details. now how can i use EF to store two entities data in 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 insert data in one table from two class. thanks

    Sunday, July 2, 2017 10:20 PM

Answers

  • User-707554951 posted

    Hi  tapan2017,

    As  far as I know, you could add Add [Table(“Students”)] attribute to both class as follows:

    using System.ComponentModel.DataAnnotations.Schema 
    
    [Table("TableName")]
    
    public class ControllerBlock
    {
        public string ControllerName { get; set; }
        public List<ControllerAction> ControllerActions { get; set; }
    }
    [Table("TableName")]
    
    public class ControllerAction
    {
        public string ActionName { get; set; }
    }

    https://dukhabandhu.wordpress.com/2014/11/12/table-and-entity-splitting-in-entity-framework-code-first/

    Best regard

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 3, 2017 10:12 AM

All replies

  • User-707554951 posted

    Hi  tapan2017,

    As  far as I know, you could add Add [Table(“Students”)] attribute to both class as follows:

    using System.ComponentModel.DataAnnotations.Schema 
    
    [Table("TableName")]
    
    public class ControllerBlock
    {
        public string ControllerName { get; set; }
        public List<ControllerAction> ControllerActions { get; set; }
    }
    [Table("TableName")]
    
    public class ControllerAction
    {
        public string ActionName { get; set; }
    }

    https://dukhabandhu.wordpress.com/2014/11/12/table-and-entity-splitting-in-entity-framework-code-first/

    Best regard

    Cathy

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, July 3, 2017 10:12 AM
  • User-691209617 posted

    I would recommend having a single entity mapped to the table, but create two 'view' entities that contain only those properties that the form requires.

    These view entities could contain methods to map data entered back to the underlying entity.

    Tuesday, July 18, 2017 7:32 PM