I have a Project table and a Organization table.
Project has a many to one relationship with Organization.
Project has a scalar property OrganizationName that references the primary key Name in Organization
I have a create view which is strongly typed to the Project model.
I collect input for the Project.Organization.Name property like so:
<div class="editor-label">
@Html.Label("Organization Name")
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Organization.Name)
@Html.ValidationMessageFor(model => model.Organization.Name)
</div>
The controller handles the post like so :
[HttpPost]
public ActionResult Create(Project project)
{
if (ModelState.IsValid)
{
db.Projects.AddObject(project);
db.SaveChanges();
return RedirectToAction("Index");
}
return View(project);
}
How can I check to see if the new project.Organization.Name is already in the database? If it is, I want the new project to reference that tuple in the database and not attempt to add it again.
UNIQUEconstraint to the table. In a multi-user environment, that’s the only way to be sure.Like this: