How to list all entity names / table names from your Entity Framework model in Silverlight 4 and RIA services

If you want to list all entity names / table names from your Entity Framework model (*.edmx) in Silverlight 4, use a RIA Invoke operation:

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.Metadata.Edm;
using System.ServiceModel.DomainServices.Server;

namespace Research.Web
{
    public partial class ResearchDomainService
    {
        /// <summary>
        /// Get the entity names from the entity framework model on which this DomainService is generated.
        /// </summary>
        /// <returns>
        /// A list of entity / table names
        /// </returns>
        [Invoke]
        public List<string> GetEntityTypeNames()
        {
            EntityContainer container = ObjectContext.MetadataWorkspace.GetEntityContainer(ObjectContext.DefaultContainerName, DataSpace.CSpace);
            List<string> result = (from meta in container.BaseEntitySets
                      where meta.BuiltInTypeKind == BuiltInTypeKind.EntitySet
                      select meta.ElementType.ToString()).ToList<string>();

            return result;
        }
    }
}

If the database contains the following tables:

 

image

 

The list will contain the strings:

ResearchModel.Customer

ResearchModel.Person

ResearchModel.Product

ResearchModel.ProductTag

ResearchModel.Tag

Solving: Add and delete buttons on the DataForm are disabled when using an List<Entity> as ItemSoure

 

When you bind the DataForm.ItemSource directly to an entity set returned from your RIA DomainService, the DataForm add and delete buttons are disabled. This is caused by the implementation in RIA Services of the entity sets. If you use an ObserverableCollection the buttons are enabled, found mine solution at: http://forums.silverlight.net/t/190405.aspx/1

Solving: DisplayName not showing in a DataForm in Silverlight 4 by using data annotations on a metadata class.

I was trying to change the displayname of a field in a dataform by placing the DisplayName attribute on the property in the metadata class. This will not work instead you must use the [Display(Name= "This is the correct display name")] attribute on the field in a metadata class:

[MetadataTypeAttribute(typeof(Product.ProductMetadata))]
    public partial class Product
    {
        internal sealed class ProductMetadata
        {

            // Metadata classes are not meant to be instantiated.
            private ProductMetadata()
            {
            }

            public int Id { get; set; }

            [Display(Name = "First Name", Description = "Employee's first name")] //Correct !
            //[DisplayName("First name 2")] // Wrong!
            public string Name { get; set; }

            public EntityCollection<ProductTag> ProductTag { get; set; }
        }
    }