[英]How to create a table relation in Orchard?
I want the MachineRecord to contain a GroupRecord Id, like the following: 我希望MachineRecord包含一个GroupRecord ID,如下所示:
MachineRecord MachineRecord
Id ID
Name 名称
Number 数
GroupRecord GroupRecord
GroupRecord GroupRecord
Id ID
Name 名称
What I have tried: 我试过的
I have tried to make the property in the model a 我试图使模型中的属性为
public virtual GroupRecord GroupRecord { get; set; }
and the database column name GroupRecord_Id
, but somehow that won't work, tried some other ways like giving the property the name GroupRecordId
and the table column GroupRecord
and so on, but all with no results. 和数据库列名称
GroupRecord_Id
,但以某种方式不起作用,尝试了其他一些方法,例如为属性指定名称GroupRecordId
和表列GroupRecord
等等,但都没有结果。
So the question is, how to get this to work so the MachineRecord table contains a GroupRecord Id? 所以问题是,如何使它工作,以便MachineRecord表包含GroupRecord ID?
Machine.cs Machine.cs
namespace PowerAll.Voorraad.Models
{
public class MachineRecord
{
public virtual int Id { get; set; }
public virtual int MachineNumber { get; set; }
public virtual string Title { get; set; }
public virtual string Description1 { get; set; }
public virtual string Description2 { get; set; }
public virtual string Description3 { get; set; }
public virtual string Description4 { get; set; }
public virtual string Description5 { get; set; }
public virtual string Description6 { get; set; }
public virtual string SerialNumber { get; set; }
public virtual GroupRecord GroupRecord { get; set; }
public virtual char PriceType { get; set; }
public virtual decimal Price { get; set; }
public virtual int Year { get; set; }
}
}
Group.cs Group.cs
namespace PowerAll.Voorraad.Models
{
public class GroupRecord
{
public virtual int Id { get; set; }
public virtual string GroupName { get; set; }
}
}
Migrations.cs Migrations.cs
namespace PowerAll.Voorraad
{
public class Migrations : DataMigrationImpl
{
public int Create()
{
SchemaBuilder.CreateTable("GroupRecord", table => table
.Column<int>("Id", column => column.PrimaryKey().Identity())
.Column<string>("GroupName", column => column.WithLength(100))
);
SchemaBuilder.CreateTable("MachineRecord", table => table
.Column<int>("Id", column => column.PrimaryKey().Identity())
.Column<int>("MachineNumber", column => column.NotNull())
.Column<string>("Title", column => column.NotNull().WithLength(40))
.Column<string>("Description1", column => column.WithLength(70))
.Column<string>("Description2", column => column.WithLength(70))
.Column<string>("Description3", column => column.WithLength(70))
.Column<string>("Description4", column => column.WithLength(70))
.Column<string>("Description5", column => column.WithLength(70))
.Column<string>("Description6", column => column.WithLength(70))
.Column<string>("SerialNumber", column => column.WithLength(20))
.Column<int>("GroupRecord_id", column => column.NotNull())
.Column<char>("PriceType", column => column.NotNull().WithLength(1))
.Column<decimal>("Price", column => column.NotNull())
.Column<int>("Year", column => column.WithLength(4))
);
// Return the version that this feature will be after this method completes
return 1;
}
}
}
MachineController.cs MachineController.cs
namespace PowerAll.Voorraad.Controllers
{
[Themed]
public class MachineController : Controller
{
private readonly IRepository<MachineRecord> machineRecords;
public MachineController(IRepository<MachineRecord> MachineRecords) {
machineRecords = MachineRecords;
}
public ActionResult List()
{
var items = machineRecords.Table;
return View(items);
}
}
}
EDIT 2: 编辑2:
I came this far thanks to endorphin, but when I try to execute AddDummyData()
I get this error: could not insert: [PowerAll.Voorraad.Models.MachineRecord][SQL: INSERT INTO PowerAll_Voorraad_MachineRecord (MachineNumber, Title, Description1, Description2, Description3, Description4, Description5, Description6, SerialNumber, PriceType, Price, Year) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?); select SCOPE_IDENTITY()]
由于
AddDummyData()
,我走了这么远,但是当我尝试执行AddDummyData()
此错误: could not insert: [PowerAll.Voorraad.Models.MachineRecord][SQL: INSERT INTO PowerAll_Voorraad_MachineRecord (MachineNumber, Title, Description1, Description2, Description3, Description4, Description5, Description6, SerialNumber, PriceType, Price, Year) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?); select SCOPE_IDENTITY()]
could not insert: [PowerAll.Voorraad.Models.MachineRecord][SQL: INSERT INTO PowerAll_Voorraad_MachineRecord (MachineNumber, Title, Description1, Description2, Description3, Description4, Description5, Description6, SerialNumber, PriceType, Price, Year) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?); select SCOPE_IDENTITY()]
While when I step trough the code all values are being posted right. could not insert: [PowerAll.Voorraad.Models.MachineRecord][SQL: INSERT INTO PowerAll_Voorraad_MachineRecord (MachineNumber, Title, Description1, Description2, Description3, Description4, Description5, Description6, SerialNumber, PriceType, Price, Year) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?); select SCOPE_IDENTITY()]
当我通过代码时,所有值都会正确发布。
This is my AddDummyData(): 这是我的AddDummyData():
public void AddDummyData()
{
var GroupRecord = new GroupRecord { GroupName = "Oldtimers" };
groupRecords.Create(GroupRecord);
var MachineRecord = new MachineRecord
{
MachineNumber = 100001,
Title = "Landini L25",
Description1 = "Desc 1",
Description2 = "Desc 2",
Description3 = "Desc 3",
Description4 = "Desc 4",
Description5 = "Desc 5",
Description6 = "Desc 6",
SerialNumber = "100000",
GroupRecord = GroupRecord,
PriceType = 'I',
Price = 7999.99m,
Year = 1954
};
machineRecords.Create(MachineRecord);
}
EDIT 3: 编辑3:
Now it's working, this is my working code (for those who are struggling on this too) 现在它正在工作,这是我的工作代码(对于那些也在这方面苦苦挣扎的人)
Migrations.cs Migrations.cs
public class Migrations : DataMigrationImpl
{
public int Create()
{
SchemaBuilder.CreateTable("MachineRecord", table => table
.Column<int>("Id", column => column.PrimaryKey().Identity())
.Column<int>("GroupRecord_Id")
.Column<int>("MachineNumber", column => column.NotNull())
.Column<string>("Title", column => column.NotNull().WithLength(40))
.Column<string>("Description", column => column.WithLength(70))
.Column<char>("PriceType", column => column.NotNull().WithLength(1))
.Column<decimal>("Price", column => column.NotNull())
.Column<int>("Year", column => column.WithLength(4))
);
SchemaBuilder.CreateTable("GroupRecord", table => table
.Column<int>("Id", column => column.PrimaryKey().Identity())
.Column<string>("Name")
);
return 1;
}
}
MachineRecord.cs MachineRecord.cs
public class MachineRecord
{
public virtual int Id { get; set; }
public virtual int MachineNumber { get; set; }
public virtual string Title { get; set; }
public virtual string Description { get; set; }
public virtual GroupRecord GroupRecord { get; set; }
public virtual char PriceType { get; set; }
public virtual decimal Price { get; set; }
public virtual int Year { get; set; }
}
GroupRecord.cs GroupRecord.cs
public class GroupRecord
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
The key to your problem is split across how you structure the models and how you define their relationship in the Migrations.cs. 问题的关键在于如何构造模型以及如何在Migrations.cs中定义模型之间的关系。
Here is a solution that I have used in the past. 这是我过去使用的解决方案。
Firstly, the class which contains the link to the second class. 首先,包含指向第二个类的链接的类。
public class CaptureRecord : ContentPartRecord {
public virtual string CaptureName { get; set; }
public virtual OptionsRecord OptionsRecord { get; set; }
}
Note: inheriting from ContentPartRecord
gives you an ID. 注意:从
ContentPartRecord
继承会给您一个ID。
Then the second class (in this case I do not need to inherit from ContentPartRecord
so I add the ID myself) 然后是第二个类(在这种情况下,我不需要从
ContentPartRecord
继承,因此我自己添加ID)
public class OptionsRecord {
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}
In the Migrations.cs add the OptionRecord ID in the CaptureRecord table definition 在Migrations.cs中,在CaptureRecord表定义中添加OptionRecord ID。
public class Migrations : DataMigrationImpl
{
SchemaBuilder.CreateTable("CaptureRecord",
table => table
.ContentPartRecord()
.Column<string>("CaptureName")
.Column<int>("OptionsRecord_id");
SchemaBuilder.CreateTable("OptionsRecord",
table => table
.Column<int>("Id", column => column.PrimaryKey().Identity())
.Column<string>("Name"));
return 1;
}
Hopefully this will give you a push in the right direction in terms of how to structure the relationship between your two tables. 希望这将帮助您朝正确的方向推动如何构建两个表之间的关系。
Edit: To add content to this sort of structure you will need to manually manage this yourself using the IRepository
instances. 编辑:要将内容添加到这种结构中,您将需要使用
IRepository
实例自己手动进行管理。
So in your controller AddDummyData
gives an example of this. 因此,在您的控制器中,
AddDummyData
给出了一个示例。
public class MachineController : Controller {
private readonly IRepository<MachineRecord> _machineRecords;
private readonly IRepository<GroupRecord> _groupRecords;
public MachineController(IRepository<MachineRecord> machineRecords, IRepository<GroupRecord> groupRecords) {
_machineRecords = machineRecords;
_groupRecords = groupRecords;
}
public void AddDummyData(){
// create a grouprecord in the database
_groupRecords.Create(new GroupRecord { Id = 1, GroupName = "One" });
// get the groupRecord just created (annoying that Create doesnt return the instance,
// but I dont know a way around this
var groupRecord = _groupRecords
.Fetch(x => x.Id == 1)
.Single();
// create a machine return with the grouprecord assigned to the GroupRecord property
_machineRecords.Create(new MachineRecord {
Id = 1,
..
GroupRecord = groupRecord
});
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.