Thursday, 5 September 2013

ManyToMany insertion issue

ManyToMany insertion issue

The goal
Create a new market and add its manager successfully.
The problem
Entity Validation Failed - errors follow:
MyApp.Models.Data.Users failed validation
Email : The Email field is required.
PHash : The PHash field is required.
PSalt : The PSalt field is required.
The scenario
I'm creating a market on my application and I can set to it a manager. In
other words, I'm attaching to a market a user that already exists. I'm
trying to do this by the following code:
[HttpPost]
public ActionResult Add(Market market)
{
[...]
Market marketBasics = new Market
{
Name = market.Name,
Slug = market.Slug,
Manager = market.ManagerId
};
[...]
User User = new User
{
Id = market.ManagerId
};
db.Markets.Add(marketBasics); // Here I insert all the information
// into "markets" table.
marketBasics.User.Add(User); // Here I (attempts to) insert into
// "users_in_markets"
[...]
}
As you can see, I'm not setting the user's Email, PHash or PSalt — I'm not
registering a new user, but I'm associating an existing (user) to a (new)
market.
So I ask: How can I resolve this?
Technical details
I'm using Entity Framework 5 + C# + MySQL.

No comments:

Post a Comment