How to reference and then insert data into associative entity that is created automatically in db?

c# ef-code-first ef-migrations entity-framework entity-framework-6

Question

I am new to Entity Framework.

I have created tables using Code First approach and it worked i.e. created Users, Phones, UsersPhones tables.

Now I can add the data to Phones table and Users table since they are mentioned in my entity models but how would I insert data into UsersPhones associative entity for which I have not data entity or model becuase it was created automatically.

Code:

Phones:

 public class Phones
    {
        public int ID { get; set; }
        public string Model { get; set; }
        public string Manufacturer { get; set; }
        public List<Users> Users { get; set; }
    }

Users:

 public class Users
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public List<Phones> Phones { get; set; }

    }

UsersPhonesDBContext

 public class UsersPhonesDBContext: DbContext
    {
        public DbSet<Users> Users { get; set; }
        public DbSet<Phones> Phones { get; set; }
    }

Controller and action to add phones:

        [HttpPost]
        public ActionResult Create(FormCollection fc)
        {
            Phones phone= new Phones();
            phone.Model= fc["Model"];
            phone.Manufacturer= fc["Manufacturer"];

            UsersPhonesDBContext.Phones.Add(phone);

            int r = UsersPhonesDBContext.SaveChanges();

            if (r > 0)
                return RedirectToAction("Index", "Test");
            else
                return Redirect("~Views/Shared/Error.cshtml");
        }

and Users in a similar fashion.

But what about UsersPhones table?

1
0
1/14/2019 11:33:49 AM

Accepted Answer

You are adding phones without users or users without phones, then there are not data in the relational table UsersPhones.

If you want to have data in this table, then you have to relate a user with a phone: for example:

[HttpPost]
public ActionResult CreateUserWithPhone(FormCollection fc)
{
    ...
    using (var context = new UsersPhonesDBContext())
    {
        var user= new Users { Name = "testName" };
        context.Users.Add(user);

        var phone = new Phones{ Model= "testModel" };
        context.Phones.Add(phone);

        user.Phones = new List<Phones>();
        user.Phones.Add(phone);

        context.SaveChanges();
    }
    ...
}

This code will create a row in Users table, a row in Phones table and a row in UsersPhones table.

--EDIT--

I created a console project to test it, with your classes (Phones, Users and UsersPhonesDBContext) and this is the Program.cs:

class Program
{
    static void Main(string[] args)
    {
        using (var context = new UsersPhonesDBContext())
        {
            var user = new Users { Name = "testName" };
            context.Users.Add(user);

            var phone = new Phones { Model = "testModel" };
            context.Phones.Add(phone);

            user.Phones = new List<Phones>();
            user.Phones.Add(phone);

            context.SaveChanges();
        }
    }
}

After execute the program, it created a row in Users table, Phones table and UsersPhones table:

enter image description here

1
1/15/2019 8:08:51 AM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow