【问题标题】:Azure Mobile App with ASP.Net backend posting data to a SQL Server table with a foreign key带有 ASP.Net 后端的 Azure 移动应用程序使用外键将数据发布到 SQL Server 表
【发布时间】: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


    【解决方案1】:

    如果它们的声明 ID 尚未使用,但如果声明已存在(它给我在声明表上违反主键合同错误),上述将发布到 Petrols 表和声明表。

    AFAIK,如果您在 Petrol 对象中指定 claim 属性并向 Petrol 表发送请求,那么它将添加新的索赔记录并添加新的 Petrol 记录。如果claim.id 的值已经存在于Claim 表中,那么您将检索到如下错误:

    如果您想用现有的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; }
    }
    

    【讨论】:

    • 我曾尝试使用外键进行装饰,但我是在字符串 Client_id 而不是 Client Client 对象上完成的。完美的谢谢。
    猜你喜欢
    • 2021-11-08
    • 1970-01-01
    • 1970-01-01
    • 2018-02-21
    • 1970-01-01
    • 2020-07-22
    • 1970-01-01
    • 2018-12-06
    • 1970-01-01
    相关资源
    最近更新 更多