简体   繁体   中英

Azure Mobile App: How to reference id column from self-referencing foreign key?

I have an entity defined as follows:

public class GoalItem : EntityData
{
    [DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    public int Id { get; set; }

    public string Title { get; set; }

    public string Description { get; set; }

    public DateTime DueDate { get; set; }

    public bool Complete { get; set; }

    [ForeignKey("ParentGoal")]
    public int ParentGoalId { get; set; }

    [InverseProperty("SubGoals")]
    public GoalItem ParentGoal { get; set; }

    public List<GoalItem> SubGoals { get; set; }
}

And I am able to create my database in Azure using this entity, however, when I try to access my entity I get the following error:

{"message":"The query specified in the URI is not valid. Property 'Id' is of an unrecognized EdmPropertyKind."}

I did some reading and found that Azure Mobile Apps require a table with lowercase id, however, if I change my Id to be lowercase the self referencing doesn't work because the ParentGoalId foreign key looks for the Id property of the GoalItem and the types don't match so my database migration fails. Does anyone know how to get ParentGoalId to reference the id propert instead of Id?

Much appreciated!

I did some reading and found that Azure Mobile Apps require a table with lowercase id, however, if I change my Id to be lowercase the self referencing doesn't work because the ParentGoalId foreign key looks for the Id property of the GoalItem and the types don't match so my database migration fails. Does anyone know how to get ParentGoalId to reference the id propert instead of Id?

According to your codes and description, I found you set your ForeignKey ParentGoalId's type is int and hide the EntityData's id.

As far as I know, the EntityData's id type is string . So the mobile library will query the database by the string type. But you replace the property with int , so you get "The query specified in the URI is not valid. Property 'Id' is of an unrecognized EdmPropertyKind"error.

The entity data class:

在此处输入图片说明

I suggest you could try to use the entity data class primary key id as the ForeignKey not hide the id by replacing its type string with int.

The code as below:

public class GoalItem : EntityData
{
    //[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
    //public int Id { get; set; }

    public string Title { get; set; }

    public string Description { get; set; }

    public DateTime DueDate { get; set; }

    public bool Complete { get; set; }

    [ForeignKey("ParentGoal")]
    public string ParentGoalId { get; set; }

    [InverseProperty("SubGoals")]
    public GoalItem ParentGoal { get; set; }

    public List<GoalItem> SubGoals { get; set; }
}

I also create a demo to test it, the result as below:

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM