Question

Allright, this should be fairly easy.

I would like to persist some records for my module in Orchard (1.7.2) without those records being also a ContentPartRecord.

In other words, I would like to be able to persist in DB the following objects:

public class LogItemRecord
{
    public virtual string Message { get; set; }
}

..which is already mapped on to the db. But notice that this class is not derived from ContentPartRecord, as it is most certainly not one.

However, when I call IRepository instance's .Create method, all I get is a lousy nHibernate exception:

No persister for: MyModule.Models.LogItemRecord

...which disappears if I do declare the LogItem record as having been inherited from ContentPartRecord, but trying to persist that, apart from being hacky-tacky, runs into an exception of its own, where nHibernate again justly complains that the Id value for the record is zero, though in not so many words.

So... how do I play nicely with Orchard and use its API to persist objects of my own that are not ContentParts / ContentItems?

Était-ce utile?

La solution

I'm running 1.7.3 (also tested in 1.7.2) and have successfully been able to persist the following class to the DB:

public class ContactRecord
{
    public virtual int Id { get; set; }
    public virtual string Name { get; set; }
    public virtual string JobTitle { get; set; }
    public virtual string Email { get; set; }
    public virtual string Phone { get; set; }
}

Here are the relevant lines from Migrations.cs

SchemaBuilder.CreateTable(
    typeof(ContactRecord).Name,
    table => table
        .Column<int>("Id", col => col.Identity().PrimaryKey())
        .Column<string>("Name")
        .Column<string>("JobTitle")
        .Column<string>("Email")
        .Column<string>("Phone")
);

I'm going to assume that the code you've shown for LogItemRecord is the complete class definition when making the following statement...

I think that any Record class you store in the DB needs an Id property, and that property should be marked as Identity and PrimaryKey in the table definition (as I've done above).

When you create a *Record class which inherits from ContentPartRecord and setup the table like

SchemaBuilder.CreateTable(
    "YourRecord",
    table => table
       .ContentPartRecord()
       // more column definitions
);

then you get the Id property/PK "for free" by inheritance and calling .ContentPartRecord() in the Migration.

See the PersonRecord in the Orchard Training Demo Module for another example of storing a standard class as a record in the DB.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top