Question

I kind of new to Orchard. What I'm trying to achieve here is relationship between my Entity. So basically ApiConfigurationRecord should have a list of ApiParameterRecord. But when I execute the action, nothing is being save. Plus, if it save, hopefully it will lazy-load its child automatically. By the way, I'm trying to non-content way (no ContentPart or ContentPartRecord).

Ok these are my codes:

public class ApiConfigurationRecord
{
    public ApiConfigurationRecord()
    {
        if (Parameters == null)
            Parameters = new List<ApiParameterRecord>();
    }

    public virtual int Id { get; set; }

    public virtual string Name { get; set; }

    public virtual string Description { get; set; }

    public virtual IList<ApiParameterRecord> Parameters { get; set; }
}

public class ApiParameterRecord
{
    public virtual int Id { get; set; }

    public virtual string Name { get; set; }
}

public class Migrations : DataMigrationImpl
{
    public int Create()
    {
        SchemaBuilder.CreateTable("ApiConfigurationRecord", table => table
            .Column<int>("Id", column => column.PrimaryKey().Identity())
            .Column<string>("Name", column=> column.NotNull())
            .Column<string>("Description", column => column.WithLength(500))
            );

        SchemaBuilder.CreateTable("ApiParameterRecord", table => table
            .Column<int>("Id", column => column.PrimaryKey().Identity())
            .Column<int>("ApiConfigurationRecord_Id")
            .Column<string>("Name", column => column.NotNull())
            );

        return 1;
    }
}

My controller action:

    public ActionResult TestInsert()
    {

        var record = new ApiConfigurationRecord() { Name = "Test 1", Description = "Some descripton" };
        record.Parameters.Add(new ApiParameterRecord() { Name = "Param1" });
        this.ApiConfigurationRepository.Create(record);

        return Content("Inserted");
    }
Was it helpful?

Solution

You need to actually link the two items.

public class ApiParameterRecord
{
    public virtual int Id { get; set; }

    public virtual string Name { get; set; }

    public virtual ApiConfigurationRecord ApiConfigurationRecord { get; set; }
}

As for creating, I wouldn't assume it would automatically create the child items, but I'm not sure. Try this:

var record = new ApiConfigurationRecord() { Name = "Test 1", Description = "Some descripton" };
this.ApiConfigurationRepository.Create(record);
var child1 = new ApiParameterRecord() { Name = "Param1", ApiConfigurationRecord = record };
ApiParameterRepository.Create(child1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top