简体   繁体   中英

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

I am new to Entity Framework.

I have created tables using Code First approach and it worked ie 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?

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:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM