views:

133

answers:

1

I'm building an asp.net mvc application for users to enter an essay contest. I have an Essay table in sql server 2005. I created my domain models by dragging over the tables in the server explorer and saving the layout as, what I've named DAL.dbml.

I'm now trying to implement input field validation in the business layer using methods mentioned in Chapter 11 of Pro ASP.NET MVC Framework by Steven Sanderson. But, the author didn't create his models like I am, so I'm at a loss as to where to put my model-specific code.

I considered putting it into the auto-generated DAL.designer.cs file, but that seems problematic.

I also tried creating a new class, EssayRequired.cs, where I made EssayRequired extend Essay. This almost worked.

It successfully showed all of the validation errors.

But when it comes time to actually save the record:

EssayTable.Context.Refresh(RefreshMode.KeepCurrentValues, essay);
EssayTable.Context.SubmitChanges();

I'm getting this error:

The type 'DomainModel.Entities.EssayRequired' is not mapped as a Table.

Any ideas as to what I should do now? Is there a better place to put domain-specific code that won't get wiped out if the db table changes and I have to delete and re-drag the table over?

Or is there a way to tell the app that the EssayRequired object should land in the Essay table.?

Thanks, Scott

A: 

You can create a partial Essay class in the same namespace as the dbml file.

public partial class Essay
{
    partial void OnCreated()
    {
       // handle additional stuff on creation, for instance.
    }
}
Terje
It works great! Thanks!
Scott