Simple Insert of Variables into Table using Entity Framework from within Silverlight RRS feed

  • Question

  • I have a simple table, which I want to add data using the Entity Framework from variables within in Silverlight using code in MainPage.Xaml.cs :

    CREATE TABLE [dbo].[pagerMsgQue](
    	[id] [varchar](50) NOT NULL,
    	[ts] [datetime] NULL,
    	[srcUser] [varchar](255) NULL,
    	[srcIp] [varchar](150) NULL,
    	[srcAgent] [varchar](255) NULL,
    	[destType] [varchar](150) NULL,
    	[destTypeVal] [varchar](150) NULL,
    	[destMsg] [varchar](500) NULL,
    	[id] ASC
    ) ON [PRIMARY]

    I created a simple deptModel.edmx, recompiled, created simple deptDomainService, recompiled. All is well.

    Not clear on how to insert the variables into database via Entity Framework here is my code:

    namespace InsertSample
        public partial class MainPage : UserControl
    public string id;
    public DateTime ts;
    public string srcUser;
    public string srcIp;
            public MainPage()
                //Initialize Globals:
    	Ts= DateTime.Now.ToString("yyyyMMdd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); 
    // insert ID, TS, srcuser, srcip into database using C# via entityframework 

    Friday, January 4, 2013 5:21 PM

All replies

  • Hi,

    1. Create a DataModel(in web project or WCF project) and drag that table onto that DataModel. For more detail please visit this link :

    2. Create a WCF service in WEB project of your Silverlight application. For more information on how to create WCF service and how to consume in Silverlight application please visit this link :

    3 . In that service create a method that will use your EF datamodel and use that model to insert data.

    Create object of EF DataModel context and set values to the fields to tables in that Context Object. After setting values finally call SaveChange method to finally save changes to the Databse. For more information on how to insert data to database using Entity Framework please visit this links :

    Let me know at which point do you have any confusion.

    One good question is equivalent to ten best answers.

    Saturday, January 5, 2013 8:42 AM
  • Hello and Thank you.

    I have done some searching and reading, and the steps, while they differ slightly, are consistent with what you stated above:

    1. Create  A Silverlight Application, using defaults, ASP.Net Web Application Project & WCF not checked
    2. To the Web:
    3. ADD EDMX Model Entity Framework
    4. Add WCF Service Application, Two Source files are supposed to be automatically created:  e.g. IService and Service. (Note: Only one was create
    5. Add a Reference to the Project
    6. Add manual code to the IService Operation Contract and Service (reference Context).
    7. To SL:
    8. Add a Service Reference to WCF
    9. Build User Interface (XAML)
    10. Add Code to Code Behind

    Perhaps I am getting confused with adding a DomainService in the past, but I thought much of the code, especially with Entity Framework was automated, so it did not require much additional code to be written manually. Specifically in the EDMX Function area for "Select Insert Function", Update and Delete Function, etc.

    Using a simple one table example, The Only object that appears is my one table without navigation properties, and the Mapping Details with the SQL Server columns and type and the Entity Framework Value/Property.

    In one article,, it states that IService and Service are automatically created during the Silverlight Enabled WCF Service addition, called IService.cs and . When I create sYes, this is correct. It then states to modify the IService1.cs and Service1.svc.cs. I don't see these files anywhere, even after selecting show all files. However, WCFService.svc and WCFService.svc.cs are created, so I believe that to be a typo. But the text states specifically to alter the IService1.cs file, and then to separately edit the Service1.svc.cs file with different additions. I thought this meant perhaps to edit the WCFService.svc file (in stead of IService1.cs) but there is no way to edit WCFService.svc, it opens WCFService.svc.cs instead.

    In other reading I see there are also referents to "Context" but I don't know how to specifically reference the "Context" attribute. I think this actually pertains to using the DomainService component, and I believe the Context is defined in the XAML.

    It's very simple to create the Entity Framework, the WCFService, the Service reference, and while the Service Reference does communicate through discovery the WCFService, it seems like there is a gap here with piecing them together in regards to referencing the data components - at least on the automated code generation side.

    I'm looking to just have a simple method add a row to a SQL Table.

    In reviewing:, (i converted the VB to C#) it states to add an ADO.NET Self-Tracking Generator to Web. This seems to build some code and the entities, after replacing:

    string inputFile = @"$edmxInputFile$"; 


    string inputFile = @"deptModel.edmx"; 

    although I get duplicate error messages such as the following:

    Error 2 Type 'WebClientEx.Web.dept_midEntities' already defines a member called 'dept_midEntities' with the same parameter types C:\Documents and Settings\My Documents\Visual Studio 2010\Projects\HttpWebRequest_SL\WebClientEx.Web\deptModel.Context.cs 32 16 WebClientEx.Web

    So again, there is a gap missing.

    I reviewed the following:

    Thank you in advance.

    • Edited by jmcpsd Tuesday, January 8, 2013 10:03 PM
    Tuesday, January 8, 2013 7:36 PM
  • Hi Jmcpsd,

    I think this problem is related to WCF, so I recommend you try this forum for better support:

    Sorry for any inconvenience.

    Have a nice day.

    Alexander Sun [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Tuesday, January 29, 2013 9:28 AM