【发布时间】:2018-01-04 11:16:06
【问题描述】:
我有一个 Azure 移动应用程序的 ASP.NET 后端。它有两个表,其中一个在 dataobjects 文件夹中有一个外键。
public class Claim : EntityData
{
public string CarReg { get; set; }
public int StartMileage { get; set; }
public int? EndMileage { get; set; }
public DateTime StartDate { get; set; }
public DateTime? EndDate { get; set; }
public DateTime? SubmittedDate { get; set; }
public string UserId { get; set; }
public virtual ICollection<Petrol> Petrols { get; set; }
}
public class Petrol : EntityData
{
public int Mileage { get; set; }
public DateTime PurchaseDate { get; set; }
public float Quantity { get; set; }
public Decimal Cost { get; set; }
public string Station { get; set; }
public virtual Claim Claim { get; set; }
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Add(
new AttributeToColumnAnnotationConvention
<TableColumnAttribute, string>("ServiceTableColumn", (property, attributes) =>
attributes.Single().ColumnType.ToString()));
}
public System.Data.Entity.DbSet<Claim> Claims { get; set; }
public System.Data.Entity.DbSet<Petrol> Petrols { get; set; }
Petrols 表创建了一个名为 Claim_id 的列,我可以手动将数据添加到 Petrols 表中(使用现有声明行之一的 id 键)。
但是当我尝试使用 JSON 发布调用(使用邮递员)时,我只能发布到 Claim 表。
我无法使用当前存在的声明 ID 发布到 Petrol 表。
下面是我使用的语法。
{
"station": "testpetrol",
"cost": 100,
"quantity": 99,
"date": "2017-07-26T00:00:00Z",
"mileage": 100,
"claim": {
"deleted": false,
"updatedAt": "2017-07-26T11:18:54.062Z",
"createdAt": "2017-07-26T11:18:54.062Z",
"version": "AAAAAAAAD8U=",
"id": "6FFFC2B6-C410-4D5A-987B-DA0662F75F03",
"user_id": "sid:xxxxxxxxxxxxxxxxxxxxx""submitted_date": null,
"end_date": null,
start_date": "2017-07-25T00: 00: 00Z",
"end_mileage": null,
"start_mileage": 15001,
"car_reg": "TestClaim"
}
}
}
如果它们的声明 ID 尚未使用,但如果声明已存在(它给我在声明表上违反主键合同错误),上述将发布到 Petrols 表和声明表。 我想要做的是通过 Petrols 表中的外键 Claim_Id 字段发布与 Claim 表相关的 Petrols 表。我来自 Asp.net webforms/SQL 背景,使用外键添加新数据很容易。这是我第一次真正使用 Entity 框架,我看不出哪里出错了。我是否正确地创建了我的对象并将它们正确关联,还是我错误地使用了 Postman?
【问题讨论】:
标签: c# asp.net entity-framework azure postman