Friday 19 October 2012

Entity Data Model With Stored Procedure in Silverlight


1.Create Silverlight application with the name of   DemoEntityFrameWork,
2.Add edmx file to DemoEntityFrameWork.web and name it as DemoEntityFrameWork.edmx
3.Establish connection from sqlserver required database.
4.check required stored procedures with related tables.
5.Click on finish button. Now .edmx file generates in solution explorer under DemoEntityFrameWork.web
6.Next step is model browser. For getting model browser click on view-->Other Windows-->Entity Data Model browser.
Model Browser
1.Extract stored procedures folder ,it has stored procedures what ever we added to the .edmx file.
2.Stored procedures folder place  under the <datbasename>.store file in  ModelBrowser.
3.Right Click on required sored procedure and  take Add Function Import option.
4.Dialog box will be displayed.
5.select stored procedure name--->Click Complex Radio Button--->Create new complex type--->Ok
6.Add domain service to DemoEntityFrameWork.web and name it as DemoEntityFrameWorkDomainService.cs
7.Write the fallowing code in DemoEntityFrameWorkDomainService.cs  for connection
public IList<GetProducts_Result> GetProductDetails()
        {
            try
            {
                this.ObjectContext.Connection.Open();
                return this.ObjectContext.GetProducts().ToList();
            }
            catch (Exception exp)
            {
                // Logger.WriteLog(exp.Message);
                throw;
            }
            finally
            {
                if (this.ObjectContext.Connection.State != ConnectionState.Closed)
                    this.ObjectContext.Connection.Close();
            }
        }

8.create metadata folder under the DemoEntityFrameWork.Web and create one class with the name GetSubsidiary_Result.cs
9.Write the fallowing code in the class
[MetadataTypeAttribute(typeof(GetProducts_Result.GetProducts_ResultMetadata))]
    public partial class GetProducts_Result
    {
        internal sealed class GetProducts_ResultMetadata
        {
            [Key]
            public int ProductId { get; set; }
        }

    }
10.Write userdefine method in mainpage.xaml.cs for binding values to the comboBox
public partial class MainPage : UserControl
    {
        DemoEntityFrameWorkDomainContext _context;
        public MainPage()
        {
            InitializeComponent();
            _context=new DemoEntityFrameWorkDomainContext();
         
        }
        #region loadsubsidiary
        void LoadSubsidaries()
        {
            _context.Load(_context.GetProductDetailsQuery()
                , delegate(LoadOperation<GetProducts_Result> gSR)
                {
                    foreach (GetProducts_Result sub in _context.GetProducts_Results)
                    {
                        dgData.DataContext = sub;

                        ComboBoxItem itm = new ComboBoxItem();
                        itm.Content = sub.ModelNumber;
                        comboBox1.Items.Add(itm);
                    }
                }
            , null);
        }

        #endregion

        private void UserControl_Loaded(object sender, RoutedEventArgs e)
        {
            LoadSubsidaries();
        }      
    }
xmlns:toolkit="http://schemas.microsoft.com/winfx/2006/xaml/presentation/toolkit" Loaded="UserControl_Loaded"

No comments: