I'm working on a MVC odata service using EF and .net 4.5.2.
The main entity of this service (Product) is mapped to a table of Product which only has Ids and Names. I then store the attributes in a separate table with the following columns: ProductId, AttributeCd, AttributeValue as in a network model.
I added a Property to my Product Entity that is a Collection of Attributes, mapped by the ProductId.
When I call the service I can chose to $expand those attributes to show me whatever attributes each product has associated.
Thus far, everything is good, but when I receive the return data in json format I see the expanded attributes first and the ProductId, ProductNm last, although these were declared the other way around in my entities.
Is there a way I can tell EF (either the modelbuilder, entityset) that I want those attributes shown at the bottom of each ProductId in the Json? Since its not doing it by default.
Code for this:
Product Entity:
public class Product
{
public Product()
{
this.Entities = new HashSet<Attribute>();
}
public Product(int id)
{
ProductId = id;
}
public int ProductId { get; set; }
public string ProductName { get; set; }
public virtual ICollection<Attribute> Attributes { get; set; }
}
}
Attribute Entity:
public class Attribute
{
public Attribute()
{
}
public Attribute(int pid, string attributecd, string attributevalue)
{
ProducId = pid;
AttributeCode = attributecd;
AttributeValue = attributevalue;
}
public int ProductId { get; set; }
public virtual Product product { get; set; }
public string AttributeCode { get; set; }
public string AttributeValue { get; set; }
}
}
Product Mapping:
public class ProductMapping : EntityTypeConfiguration<Product>
{
public ProductMapping()
{
this.ToTable("dbo.Products");
this.HasKey(p => p.ProductId);
this.Property(p => p.ProductId)
.IsRequired()
.HasColumnName("ProductId")
.HasColumnType("int");
this.Property(p => p.ProductName)
.IsRequired()
.HasColumnName("ProductNm")
.HasColumnType("varchar")
.HasMaxLegnth(150);
this.HasMany(a => a.Attributes)
.WithRequired(p => p.product)
.HasForeignKey(t => t.ProductId);
}
}
The Attribute mapping only has the ToTable, HasKey and column name/types mapping.
My controller just has a simple query to the entitySet where productId = productId input and returns an IQueryable of the Entity Product.
Any help will be greatly appreciated!