简体   繁体   English

带有ASP.Net后端的Azure移动应用程序使用外键将数据发布到SQL Server表

[英]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.

相关问题 将数据插入包含asp.net中的外键的表的SQL查询 - SQL query to insert data into a table containing foreign key in asp.net 如何使用asp.net获取SQL Server外键中的数据? - How to get data in a SQL Server foreign key using asp.net? 如何将 ASP.NET Web 应用程序部署到 Azure 与现有的 Z9778840A0104CB30C52ZB7 服务器数据库 (76410CB30C52ZB7) 服务器数据库 - How to deploy an ASP.NET Web App to Azure with an existing SQL Server database (Azure Data Studio) 具有ASP.NET身份的Azure移动应用 - Azure Mobile App with ASP.NET Identity ASP.Net Core 2.1上的Azure移动后端连接问题 - Azure Mobile Backend connection issue on ASP.Net Core 2.1 外键问题阻止我将现有表绑定到ASP.NET Identity 2.0(Azure服务器) - Foreign Key Issue preventing me from tying an existing table into ASP.NET Identity 2.0 (Azure Servers) ASP.NET Core 3.1 将数据发布到相关表(一对多)以 sqllite 错误 19 '外键约束失败告终 - ASP.NET Core 3.1 posting data to related tables(one to many) ending up with sqllite error 19 'Foreign key constraint failed 如何在ASP.NET C#中的SQL Server中添加外键 - How to add foreign key in SQL Server in ASP.NET C# ASP.NET MVC显示SQL Server表数据 - ASP.NET MVC Display SQL Server table data 如何在Azure移动应用程序服务(.Net后端)中访问用户数据 - How to access user data in Azure Mobile App services (.Net backend)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM