K.Z
K.Z

Reputation: 5075

insert data in many-to-many relationship in Entity Framework

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

Upvotes: 0

Views: 1315

Answers (1)

timothyclifford
timothyclifford

Reputation: 6959

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.

Upvotes: 2

Related Questions