1

I'm creating an MVC3 asp.net application using Entity Framework 4 and C#.

I've tried to read up on EF and model binding, lazy loading, etc. But I've hit an obstacle.

I have a User Model. The Store and UserType models can have an ICollection of Users. When I add a User with the Create Action, How do I specify multiple parents?

I think that I only know how to add if there is one parent.

My Models:

 public class UserType
{
    public virtual int ID { get; set; }
    public virtual string UserTypeName { get; set; }
    public virtual ICollection<User> Users { get; set; }
}

public class Store
{
    public virtual int ID { get; set; }
    public virtual string StoreName { get; set; }
    public virtual Address StoreAddress { get; set; }
    public virtual ICollection<Workroom> Workrooms { get; set;}
    public virtual ICollection<User> Users { get; set; }
}

    public class User
{
    public virtual int ID { get; set; }
    public virtual string Username { get; set; }
    public virtual string Email { get; set; }
    public virtual Store Store { get; set; }
    public virtual UserType UserType { get; set; }
}

Here is my db context:

    public DbSet<Workroom> Workrooms { get; set; }
    public DbSet<Ticket> Tickets { get; set; }
    public DbSet<Customer> Customers { get; set; }
    public DbSet<Store> Stores { get; set; }
    public DbSet<User> Users { get; set; }
    public DbSet<UserType> UserTypes { get; set; }

    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Store>()
            .HasMany(store => store.Workrooms)
            .WithRequired(workroom => workroom.Store);

        modelBuilder.Entity<Store>()
            .HasMany(store => store.Users)
            .WithRequired(user => user.Store);

        modelBuilder.Entity<UserType>()
            .HasMany(usertype => usertype.Users)
            .WithRequired(user => user.UserType);

        base.OnModelCreating(modelBuilder);
    }

Here's my create action:

    public ActionResult Create()
    {
        return View(new User());
    } 

    //
    // POST: /Users/Create

    [HttpPost]
    public ActionResult Create(User newUser)
    {
        try
        {
            int storeID = newUser.Store.ID;
            var store = _db.Stores.Single(r => r.ID == storeID);
            store.Users.Add(newUser);
            _db.SaveChanges();

            return RedirectToAction("Index");
        }
        catch (Exception ex)
        {
            ModelState.AddModelError("", ex.InnerException.Message);

            return View();
        }
    }

Do I just add another "Add" call for UserType? for example: int userTypeID = newUser.UserType.ID var usertype = _db.UserTypes.Single(s => s.ID == userTypeID)

How would the Entity Framework know that Users has another Parent??? ...do I care?

My hunch is that I should be doing this a different way, more specific and more accurate.

Mattbob
  • 45
  • 2
  • 6
  • Don't include keywords in your subject title, that's what tags are for. – Erik Funkenbusch Apr 23 '12 at 16:32
  • This is called [polymorphic associations](http://stackoverflow.com/questions/8895806/how-to-implement-polymorphic-associations-in-an-existing-database). Maybe this will give you a hint which approach suits you best. – Gert Arnold Apr 27 '12 at 20:58

1 Answers1

0

In this case, you probably want to add the user to the Users table, rather than the Stores. Then you assign the StoreID and UserTypeID to the user before you commit.

It looks like you're already setting the StoreID in your UI, are you doing the same for UserType? If so, then just add the user to the users table and you should be good.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • So it sounds like as long as I populate StoreID and UserTypeID in the UI form on the User Model, then my foreign keys will be properly set and there's no need to specify the parent (whether it be Store or UserType) in the create action? – Mattbob Apr 23 '12 at 17:00
  • @Mattbob - That's typically how it works, yes. However, be careful that you aren't overwriting your Store types. I'm not sure where you're setting those. – Erik Funkenbusch Apr 23 '12 at 17:06
  • I'm assuming this means I need to designate StoreID and UserTypeID as {Foreign Key] on the User model. Cool. Obstacle successfully navigated. – Mattbob Apr 23 '12 at 17:09