c# - Azure Mobile App with ASP.Net backend posting data to a SQL Server table with a foreign key -
i have asp.net backend azure mobile app. has 2 tables 1 foreign key in dataobjects folder.
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 column called claim_id
, able add data petrols
table manually (using id key 1 of existing claim rows).
but when tried using json post calls (using postman), able post claim
table.
i cannot post petrol
table using existing claim id. below syntax 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 post both petrols table , claims table if claim id not in use, if claim exists (it gives me violation of primary key contract error on claims table ). want post petrols table related claim table foreign key claim_id field in petrols table. come asp.net webforms/ sql background adding new data foreign key easy. first real go of using entity framework , cannot see going wrong. have created objects correctly , related them correctly, or using postman incorrectly?
above post both petrols table , claims table if claim id not in use, if claim exists (it gives me violation of primary key contract error on claims table ).
afaik, if specific claim
property within petrol
object , send request against petrol table, add new claim record , add new petrol record. if value of claim.id
has existed in claim table, retrieve error follows:
if want add new record petrol table existing claim_id
, modify petrol
model , define foreign key property follows:
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; } }
Comments
Post a Comment