我希望 MachineRecord 包含一个 GroupRecord Id,如下所示:
机器记录
Id
姓名
号码
GroupRecord
集体记录
标识
名称
我试过的:
我试图在模型 a
public virtual GroupRecord GroupRecord { get; set; }
和数据库列 name中创建属性GroupRecord_Id
,但不知何故,这不起作用,尝试了其他一些方法,例如为属性提供 nameGroupRecordId
和 table 列GroupRecord
等等,但都没有结果。
所以问题是,如何让它工作,以便 MachineRecord 表包含 GroupRecord Id?
机器.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; }
}
}
组.cs
namespace PowerAll.Voorraad.Models
{
public class GroupRecord
{
public virtual int Id { get; set; }
public virtual string GroupName { get; set; }
}
}
迁移.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;
}
}
}
机器控制器.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);
}
}
}
编辑2:
多亏了内啡肽,我走到了这一步,但是当我尝试执行时,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()]
当我通过代码时,所有值都被正确发布。
这是我的 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);
}
编辑 3:
现在它正在工作,这是我的工作代码(对于那些也在为此苦苦挣扎的人)
迁移.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;
}
}
机器记录.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
public class GroupRecord
{
public virtual int Id { get; set; }
public virtual string Name { get; set; }
}