none
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,
     CONSTRAINT [PK__pagerMsgQue__571DF1D5] PRIMARY KEY CLUSTERED 
    (
    	[id] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) 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()
            {
                InitializeComponent();
                //Initialize Globals:
    	Id=1;
    	Ts= DateTime.Now.ToString("yyyyMMdd HH:mm:ss", System.Globalization.CultureInfo.InvariantCulture); 
    	srcUser=”jane”;
    	srcIp=”123”
    // 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 :

    http://msdn.microsoft.com/en-us/data/jj206878.aspx

    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 :

    http://www.dotnetcurry.com/ShowArticle.aspx?ID=228

    http://www.youtube.com/watch?v=r8YCFufS2Qk

    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 :

    http://geekswithblogs.net/dotNETvinz/archive/2011/04/04/entity-framework----inserting-data-to-database.aspx

    http://social.msdn.microsoft.com/Forums/en-US/adodotnetentityframework/thread/cde2bb42-8978-4c18-bd52-e56814096881/

    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, http://geekswithblogs.net/berthin/archive/2011/05/29/ado_net_entityframework_from_silverlight.aspx, 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: http://www.code-magazine.com/article.aspx?quickid=1011111&page=2, (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$"; 

    with

    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:

    http://www.code-magazine.com/article.aspx?quickid=1011111&page=1

    http://www.codeproject.com/Articles/363040/An-Introduction-to-Entity-Framework-for-Absolute-B

    http://msdn.microsoft.com/en-us/data/gg699321.aspx

    http://www.dotnetcurry.com/ShowArticle.aspx?ID=619

    http://stackoverflow.com/questions/3579230/entity-framework-inserting-records-in-child-table-when-parent-record-already-e

    http://geekswithblogs.net/berthin/archive/2011/05/29/ado_net_entityframework_from_silverlight.aspx

    http://chanmingman.wordpress.com/2010/09/04/silverlight-wcf-ria-domain-service-class-crud-part1-create-domain-service-class/

    http://silverlightloaddata.codeplex.com/

    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: http://social.msdn.microsoft.com/Forums/en-US/wcf/threads

    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