What is the best way to have default values in an Orchard database?

Topics: Writing modules
Mar 28, 2012 at 4:36 PM

Basically I'm creating an event module that has 3 tables: Users (name, account created), User Events (userid, eventtype, start/end dates, and description), and EventTypes (ex: holiday, vacation, sick, etc). I'd like to have the EventTypes table be created with default values already in the table, but allow the users to add when needed. So how do I create the table and then give it default values? What's the best way to go about that?

Mar 28, 2012 at 6:22 PM

You can use

.Column<Type>("Name", column => column.WithDefault(defaultValue))
in migrations or even giving default values for your records' properties in their constructors could work.

Mar 28, 2012 at 6:48 PM
Edited Mar 28, 2012 at 6:49 PM

Well I wanted to create the table (that part I can do). Then give put in the default values of: Business Trip, Vacation, Sick, Holiday. So the table should have 4 rows to start with.

Mar 28, 2012 at 7:20 PM

Ah, default rows? Then just use an IContentManager injected into your class. New() and Create() methods are what you're looking for. I think you can use this even in your migration class, after you create the table for your part (I'm not sure when the table is actually created, so you may need to do this later).

Mar 29, 2012 at 1:28 PM

Could I get a code example? Not really sure how to set that up. I'm a newbie at this.

Mar 29, 2012 at 6:53 PM

There are a lot of modules doing that, like the Blogs, but something a way simpler is e.g. this module's only controller's only post action.