Is it possible to make a related Entity possible to display with Odata Expand (Webapi)?

Asked

Viewed 36 times

1

Hello, I have a question regarding the following situation: I have an Entity that I defined in my model as Tasfafreq

public partial class TarefaFreq
{
    [Key]
    public int ID { get; set; }
    public string Name { get; set; }
}

I created some data in a static list:

public class TarefaFreqData
{
    public static IEnumerable<TarefaFreq> GetLista()
    {
        var list = new List<TarefaFreq>();
        list.Add(new TarefaFreq() { ID = 1, Name = "Teste 1" });
        list.Add(new TarefaFreq() { ID = 2, Name = "Teste 2" });            
        return list;
    }        
}

And I also have the Task model (This one is in the database):

    public partial class Tarefa
    {
        public int ID { get; set; }
        public int FreqID { get; set; } 
        public virtual TarefaFreq Freq { get; set; }
    }

In the settings of my webAPI with Entity-Framework if I try to set my property: builder.EntityType<Tarefa>().Property(f => f.Freq); I get the following error: The type TarefaFreq must be a non-nullable value type in order to use it ...

My goal is that when I report to the URL: http://localhost:58170/odata/Tarefas(12)/?$expand=Freq I can get the data in the answer.

Recalling that the Taskforce Entity will not be in my database, it has only been created to serve data to a foreign key that is in my database that is owned by Tarefa -> FreqID, so the static list I created. In this case, as I am developing in the client I need to use the $expand of ASP.NET Web API 2 Odata.

  • Ever tried to turn into struct? I’m not familiar with these requirements of the tool, but this should solve it. I’m just not saying it’s the right solution. Is it right there not only to use the ID of the object? It even seems. It even seems to be something redundant in the code.

  • @Maniero Good, as it is so far, is working when I need to create the relationship between the foreign key and the data on my list. The $expand to the entity that TarefaFreq is that when I list the details this information cannot come mapped in the field.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.