0

I have many-to-may relationship between two tables that breaks by introducing another table in between, containing primary keys of both.

enter image description here

and here is my model classes

Navigation_Functions

public class Navigation_Functions
{
    public Navigation_Functions()
    {

    }
    [Key]
    public int Function_ID { get; set; }

    [StringLength(250)]
    [Required(ErrorMessage = "Required Title")]
    [Display(Name = "Function Title")]
    public string FunctionName { get; set; }

    [Required(ErrorMessage = "Required Hierarchy Level")]
    [Display(Name = "Hierarchy Level")]
    public int Hierarchy_Level { get; set; }

    public ICollection<Navigation_FunctionHierarchy> Navigation_FunctionHierarchy { get; set; }
    public ICollection<Navigation_FunctionInAction> Navigation_FunctionInAction { get; set; }
    public ICollection<Navigation_FunctionInController> Navigation_FunctionInController { get; set; }
}

Navigation_FunctionController Model

 public class Navigation_FunctionController
{
    public Navigation_FunctionController()
    {

    }

    [Key]
    public int ControllerID { get; set; }

    [StringLength(250)]
    [Required]
    public string ControllerName { get; set; }

    public ICollection<Navigation_FunctionAction> Navigation_FunctionAction { get; set; }
    public ICollection<Navigation_FunctionInController> Navigation_FunctionInController { get; set; }

}

Navigation_FunctionInController Model

  public Navigation_FunctionInController() 
    { 

    }

    [Key]
    public int FunctionInController_ID { get; set; }
    public int Function_ID { get; set; }
    public int ControllerID { get; set; }

    public Navigation_FunctionController Navigation_FunctionController { get; set; }
    public Navigation_Functions Navigation_Functions { get; set; }

}

I have generic repository for CRUD operation

public void InsertEntity(TEntity obj)
    {
        _DbSet.Add(obj);
    }

Now I have class responsible to insert data by calling generic repository and pass object with data, I am struggling to insert data as how I insert primary of both tables into Navigation_FunctionInController table, where Functions and Controller record are creating at same time. ??????????????????????????????????????

I am reading blogs Insert/Update Many to Many Entity Framework . How do I do it? but I am struggling to understand how to insert data with my structure.

?????????

    public void CreateFunctionNavigation(FunctionsNavigation_ViewModel _obj)
    {

        using(var _uow = new FunctionsNavigation_UnitOfWork())
        {
            try
            {
                 ??????????????????????

            }
            catch
            {

            }
        }
    }

My ViewModel

public class FunctionsNavigation_ViewModel 
{
    public Navigation_Functions _Navigation_Functions { get; set; }

    public Navigation_FunctionController _Navigation_FunctionController { get; set; }
}

I have updated my model as been suggested but still failing to save data in database, following is screen shot from debug code.

enter image description here

Community
  • 1
  • 1
K.Z
  • 5,201
  • 25
  • 104
  • 240

1 Answers1

2

From what I can tell, you have a many-to-many relationship between Navigation_Functions and Navigation_FunctionController.

The Navigation_Functions model should have a property to the foreign entity rather than the joining table, as suggested in the link you supplied, you don't map the joining table as Entity Framework takes care of this for you:

This

public ICollection<Navigation_FunctionInController> Navigation_FunctionInController { get; set; }

Should be

public ICollection<Navigation_FunctionController> Navigation_FunctionController { get; set; }

Then when you come to save your view model, it would be similar to:

public void CreateFunctionNavigation(FunctionsNavigation_ViewModel _obj)
{
    using(var _uow = new FunctionsNavigation_UnitOfWork())
    {
        try
        {
            // UPDATE START
            _obj._Navigation_FunctionController.Navigation_Functions = new List<Navigation_Functions>();
            _obj._Navigation_FunctionController.Navigation_Functions.Add(_obj._Navigation_Functions);
            // UPDATE END

            var navigationFunctions = _obj._Navigation_Functions;
            navigationFunctions.Navigation_FunctionController = new List<Navigation_FunctionController>();
            navigationFunctions.Navigation_FunctionController.Add(_obj._Navigation_FunctionController);

            _uow.Entities.Add(navigationFunctions);
            _uow.Save();
        }
        catch
        {
            // Log exception
        }
    }
}

Keep in mind I haven't tested this code so you'll probably have to update the syntax.

timothyclifford
  • 6,799
  • 7
  • 57
  • 85
  • thanks you very much ... I am going to try now and hope its going to work – K.Z Jan 09 '15 at 18:38
  • is navigation_controller should also been assigneda value to navigation_function ... as I am seeing this null in debug????? – K.Z Jan 11 '15 at 07:53
  • As it's a many to many relationship, you should only need to save the relationship on one side however I've added an update (UPDATE START - UPDATE END) around how you would do this on both sides if you wanted. Are you using database first or code first? – timothyclifford Jan 12 '15 at 10:51
  • i have debug code .... and put screen shot in my question as above... I am struggling to save data in database. is my viewModel is correct??? – K.Z Jan 12 '15 at 11:53
  • Your view model won't have any effect on your database models, you're using this to transport data from the UI. The fact that Navigation_FunctionController has a null Navigation_Functions object shouldn't matter as you've already added the reference on the other side of relationship. What does you DB schema look like now? Are you getting any errors when you try and save? – timothyclifford Jan 12 '15 at 17:42