0

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!

Robert McKee
  • 21,305
  • 1
  • 43
  • 57
  • EF is not at fault here, it's whatever you are using to serialize to JSON that is determining the order of the properties (which is usually alphabetical). http://stackoverflow.com/questions/3330989/order-of-serialized-fields-using-json-net But why do you care about the order? It has no impact on serialization/deserialization. – Paul Abbott May 06 '15 at 21:20
  • What is more, EF's mapping, either fluent or attributes, and JSON serialization are two separate worlds and they should be. But since I suspect you use Web API 2, which uses Json.Net, you could try to use the [JsonProperty attribute](http://stackoverflow.com/a/14035431/861716) if for some reason the order is important (but see Paul's comment). – Gert Arnold May 06 '15 at 22:47
  • Thanks. It doesnt matter for serialization/deserialization as it should work either way. Its just a matter of presentation/tidiness rather than processing. – Mariano Barbuscio May 07 '15 at 12:47
  • I did try to use the jsonproperty attribute, but it still wont work. If i cant make it display the way i want to, its fine anyways. I just wanted it to look tidy and was curious if there was a way to achieve that. Thanks – Mariano Barbuscio May 07 '15 at 12:48

0 Answers0