[英]Azure Mobile App with ASP.Net backend posting data to a SQL Server table with a foreign key
I have an ASP.NET backend for an Azure mobile app. 我有一个用于Azure移动应用程序的ASP.NET后端。 It has two tables one with a foreign key in the dataobjects folder. 它有两个表,其中一个在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; }
The Petrols
tables has created a column called Claim_id
and I am able to add data to the Petrols
table manually (using the id key for one of the existing claim rows). Petrols
表创建了一个称为Claim_id
的列,我能够手动将数据添加到Petrols
表中(使用ID密钥来表示现有的索赔行之一)。
But when I tried using JSON post calls (using postman), I am only able to post to the Claim
table. 但是,当我尝试使用JSON发布调用(使用邮递员)时,我只能发布到Claim
表。
I cannot post to the Petrol
table using a currently existing Claim id. 我无法使用当前现有的Claim ID将其发布到Petrol
表中。 Below is the syntax I am using. 以下是我使用的语法。
{
"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"
}
}
}
Above will post to both the Petrols table and the Claims table if they Claim Id is not already in use, but if the Claim already exists (it gives me a violation of primary key contract error on the Claims table ). 如果它们的Claim Id尚未使用,但是如果Claim已经存在,则上述内容将同时发布到Petrols表和Claims表中(这使我违反了Claims表上的主键合同错误)。 What I want to do is post the Petrols table that is related to a Claim table by the foreign key Claim_Id field in the Petrols table. 我想做的是通过Petrols表中的外键Claim_Id字段发布与Claim表相关的Petrols表。 I come from an Asp.net webforms/ SQL background where adding new data with a foreign key is easy. 我来自Asp.net Webforms / SQL背景,使用外键添加新数据很容易。 This is my first real go of using Entity framework and I cannot see where I am going wrong. 这是我使用实体框架的第一个真正的尝试,而且我看不到哪里出了问题。 Have I created my objects correctly and related them correctly, or am I using Postman incorrectly? 我是否正确创建了对象并正确关联了它们,或者我使用Postman的方法有误?
Above will post to both the Petrols table and the Claims table if they Claim Id is not already in use, but if the Claim already exists (it gives me a violation of primary key contract error on the Claims table ). 如果它们的Claim Id尚未使用,但是如果Claim已经存在,则上述内容将同时发布到Petrols表和Claims表中(这使我违反了Claims表上的主键合同错误)。
AFAIK, if you specific the claim
property within the Petrol
object and send request against Petrol table, then it would add the new claim record and add the new Petrol record. 据我所知,如果你的具体claim
物业内的Petrol
对象,并发送对汽油表请求,那么它会添加新的要求记录并添加新的汽油纪录。 If the value of claim.id
has existed in Claim table, then you would retrieve the error as follows: 如果claim.id
的值已存在于Claim表中,则将按以下方式检索错误:
If you want to add a new record to Petrol table with a existing Claim_id
, you could modify the Petrol
model and define the foreign key property as follows: 如果要使用现有Claim_id
将新记录添加到Petrol表中,则可以修改Petrol
模型并定义外键属性,如下所示:
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 string Claim_Id { get; set; }
[ForeignKey("Claim_Id")]
public virtual Claim Claim { get; set; }
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.