Insert Record in custom table with ID

Topics: Customizing Orchard, Writing modules
Jul 1, 2012 at 7:56 PM

Hi, I have created a table which I will manage using the basic MVC pattern.

The table has an Id record. The Id record is not set to Identity, because I want to insert the value of the userpart record in this field.

Now if I go to edit and I don't have a record I instantiate a model, with the Id set to the user id and the other fields to empty string. I get a nice clean empty form.

After that I go to EditPost action. I try to retrieve the record with the id from the passed model. if no record exists, I assume that there is no record yet. So I create a new record with the values from the passed on model.

If its a create I hit _repository.Create(record). And then I get an error:

could not insert: [MyModule.MyRecord][SQL: INSERT INTO MyModule_MyRecord (Field1, Field2) VALUES (?, ?); select SCOPE_IDENTITY()].

The reason I use this way, is that between the user and the record I am creating there must be a one-to-one relation. And I think the only way to achieve that is to have an Id on MyRecord that I can fill with the corresponding Id from the userpartrecord.

I am not looking into making this a contentpart and attach it, since I want this part to be separated from the contentitems in the CMS. 

Coordinator
Jul 2, 2012 at 1:48 AM

Why don't you add a UserId column and let the Id column do its job of being the Id (rather than a foreign key)?