locked
Entity Framework Code First + RIA Service +Lightswitch RRS feed

  • Question

  • I have attempted to build a DBDomain service based on a code first database context. I am able to get the LS app to see the service and the etities but the application does not build citing an error about converting the dbdomain service to a domainservice. (I dont have the code in front of me right now) so I can't post the exact error.

    Basically the ida is that I want to have LS work off the same code that I want to use in other apps (MVC etc)

    Just wanted to see if anyone else had tried this.

    Looking forward to responses

     

    Tuesday, August 9, 2011 3:03 AM

Answers

  • Finally made this work.

    The trick is to use nu-get to install RiaServices.Toolkit((just released on 8-4 ish) in both the Data Layer DLL and the Server project of the lightswitch app.

    Note -- you will have to override the constructor of your db context to read the connection string from the webconfig file something like the following:

    public ControllersAssistantContext(){
      string temp = System.Configuration.ConfigurationManager.ConnectionStrings["bd060398-acc6-4375-a886-0ff241fd1928"].ConnectionString;
      Database.Connection.ConnectionString = temp;
      Database.SetInitializer<ControllersAssistantContext>(null);
    }

    Note -- LS is correctly detecting the consraints on entities without adding attributes to the domain object class 

    Note -- I have not tested this with relationships yet but I dont forsee any issues.

    Hope someone else finds this useful.

     
    • Marked as answer by rdavison Wednesday, August 17, 2011 12:02 AM
    Tuesday, August 16, 2011 11:58 PM

All replies

  • I wonder if you can use some kind of variation of the technique that Eric uses in How Do I: Display a Chart Built On Aggregated Data, to "share" the model that LS creates, to do something similar with your model.

    Almost like what he did in reverse.


    Yann

    (plus ça change, plus c'est la même chose!)

    Tuesday, August 9, 2011 3:56 AM
    Moderator
  • LightSwitch is designed to use WCF RIA Services as an extensibility point.

    Any data that you are trying to get "into" LightSwitch, you want to use a WCF RIA Service.

    See:

    WCF RIA Service: Combining Two Tables

    Creating a Simple LightSwitch RIA Service (using POCO)


    Make Them Ask: That's a LightSwitch App?

    http://LightSwitchHelpWebsite.com

    Tuesday, August 9, 2011 4:09 AM
  • I was gong to say something very similar at first, but I thought that's what he was already doing, lol.

    I gather a DBDomain service *isn't* a RIA service then?


    Yann

    (plus ça change, plus c'est la même chose!)

    Tuesday, August 9, 2011 4:12 AM
    Moderator
  • I was gong to say something very similar at first, but I thought that's what he was already doing, lol.

    I gather a DBDomain service *isn't* a RIA service then?


    Yann

    (plus ça change, plus c'est la même chose!)


    It must be close because he said "I am able to get the LS app to see the service and the entities but the application does not build" so that WOULD make you think 'oh so you DO have a WCF RIA Service?'.

    However, I know that only a "WCF RIA SERVICE" (the quotes for emphasis meaning "exactly what is described here: http://207.46.16.248/en-us/library/gg589479.aspx" and not something that is "close") will work (grin).


    Make Them Ask: That's a LightSwitch App?

    http://LightSwitchHelpWebsite.com


    Tuesday, August 9, 2011 4:25 AM
  • I wasn't saying you were wrong, I was trying to understand. I;ve never used RIA services before LS.

    Yann

    (plus ça change, plus c'est la même chose!)

    Tuesday, August 9, 2011 4:53 AM
    Moderator
  • I wasn't saying you were wrong, I was trying to understand. I;ve never used RIA services before LS.

    Yann

    (plus ça change, plus c'est la même chose!)


    Oh I know, I was also saying you were not wrong to assume he had a WCF RIA Service based on what he said he accomplished.

    It may turn out I am wrong, let's see what he says :)


    Make Them Ask: That's a LightSwitch App?

    http://LightSwitchHelpWebsite.com

    Tuesday, August 9, 2011 5:00 AM
  • Alas, it looks like I deleted my experiment.

    I will have to recreate it (ahh the life of a full time programmer trying to do his own stuff on the weekends)

    More to follow.

    Basically I think this should work, if it does not in V1 then it definately should by V2. Code first is so attractive as a place to start .. it just had better Ria integragtion a LS crud ap could go right along with the MVC web app and use the same data service.

    :P Should all be able to be generated as a place to start inside visual studio IMHO.

    Wednesday, August 10, 2011 2:06 AM
  • Experiment recreated:

    Error 1 The type 'SLCLCAData.Services.ControllersAssistantDomainService' cannot be used as type parameter 'TServiceContext' in the generic type or method 'Microsoft.LightSwitch.ServerGenerated.Implementation.RiaDataServiceImplementation<TContext,TServiceContext>'. There is no implicit reference conversion from 'SLCLCAData.Services.ControllersAssistantDomainService' to 'System.ServiceModel.DomainServices.Server.DomainService'. D:\SourceCode\New folder\HSProjects\Old\WcfServiceLibrary1\CompanyEditorWCF\ServerGenerated\GeneratedArtifacts\DataServiceImplementation.cs 99 18 ServerGenerated

    Error 2 The type 'System.ServiceModel.DomainServices.EntityFramework.DbDomainService`1<T0>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.ServiceModel.DomainServices.EntityFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. D:\SourceCode\New folder\HSProjects\Old\WcfServiceLibrary1\CompanyEditorWCF\ServerGenerated\GeneratedArtifacts\DataServiceImplementation.cs 99 18 ServerGenerated

    please see attached project (well i thought i could attach a project but alas i dont seem to be able to find that screen again will try again later)

    simple company table schema

    USE

    [ControllersAssistant]

    GO

    /****** Object: Table [dbo].[Company] Script Date: 08/10/2011 21:49:53 ******/

    SET

    ANSI_NULLS

     

    QUOTED_IDENTIFIER

     

    ANSI_PADDING

     

    TABLE [dbo].[Company]

     

    [Id] [int]

    IDENTITY(1,1) NOT NULL,
     

    [Name] [varchar]

    (128) NOT NULL,

    [ShortDescription] [varchar]

    (50) NOT NULL,

    [ConnectionString] [varchar]

    (1024) NULL,

    [DatabaseName] [varchar]

    (128) NULL,

    [DisplaySortSequence] [int]

    NOT NULL,

    [TS] [timestamp]

    NOT NULL,

     

    CONSTRAINT [PK_Company] 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]

    GO

    SET

    ANSI_PADDING

     

    TABLE [dbo].[Company] ADD CONSTRAINT [DF_Company_DisplaySortSequence] DEFAULT ((0)) FOR [DisplaySortSequence]

    GO

     

     

    ON

     

     

    GO

    ALTER

     

    (

     

    ON

     

     

    GO

    CREATE

     

    ON

     

     

    GO

    SET

     

    ON

     

     

    GO

    SET


    Thursday, August 11, 2011 2:51 AM
  • Just so you know I used the exact same set of steps to tell LS about the dbdomain service as you did to tell it about your regular WCF Service.. All the dialogs came up that were in your instructions.

    just recieved these two errors on build

    Error 1 The type 'SLCLCAData.Services.ControllersAssistantDomainService' cannot be used as type parameter 'TServiceContext' in the generic type or method 'Microsoft.LightSwitch.ServerGenerated.Implementation.RiaDataServiceImplementation<TContext,TServiceContext>'. There is no implicit reference conversion from 'SLCLCAData.Services.ControllersAssistantDomainService' to 'System.ServiceModel.DomainServices.Server.DomainService'. D:\SourceCode\New folder\HSProjects\Old\WcfServiceLibrary1\CompanyEditorWCF\ServerGenerated\GeneratedArtifacts\DataServiceImplementation.cs 99 18 ServerGenerated

    Error 2 The type 'System.ServiceModel.DomainServices.EntityFramework.DbDomainService`1<T0>' is defined in an assembly that is not referenced. You must add a reference to assembly 'Microsoft.ServiceModel.DomainServices.EntityFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35'. D:\SourceCode\New folder\HSProjects\Old\WcfServiceLibrary1\CompanyEditorWCF\ServerGenerated\GeneratedArtifacts\DataServiceImplementation.cs 99 18 ServerGenerated

    the fact that the VS tooling worked leads me to believe this should work and that the LS team hasn't tested this scenerio or something else needs to be done

    Ideas?

    Friday, August 12, 2011 12:06 AM
  • Curious if you saw this walkthrough...

    http://varunpuranik.wordpress.com/2011/06/29/wcf-ria-services-support-for-ef-4-1-and-ef-code-first/

     

    Friday, August 12, 2011 2:56 PM
  • Yes, I used the RiaServices.EntityFramework.Scaffolding tool to generate the dbdomain service but the ideas are the same.

    Friday, August 12, 2011 3:09 PM
  • Yes I have.

    I used the RiaServices.EntityFramework.Scaffolding tool to generate the dbdomain service from my datbase context. ended up the same in the end.

     

    Friday, August 12, 2011 3:11 PM
  • What about the RiaServicesContrib version here: http://www.riaservicesblog.net/Blog/post/WCF-RIA-Services-EF-41-Scaffolding-Support-WCF-RIA-Services-Contrib-and-NuGet.aspx

    According to Colin, he uses a different approach than LaRud did.  Don't know if that will matter however.

    Friday, August 12, 2011 3:33 PM
  • I will give that a try. My gut feeling is though this is an issue within lightswtich where they were not *aware* or able to fit in support for dbdomain services before RTM

    THanks

    RD

    Friday, August 12, 2011 4:03 PM
  • I have a simple project that demonstrates this issue if anyone knows how to upload files to this forum
    Saturday, August 13, 2011 5:39 AM
  • I am having a problem using timestamp fields in my database using EF and RIA services with LS.

    I have created a thread at http://social.msdn.microsoft.com/Forums/en-US/lightswitch/thread/f7accda5-9a62-46f9-80e9-f4334f20ed39

    Do you perhaps have any advice for me?

    Thanks

    Darrel

    Saturday, August 13, 2011 6:59 PM
  • Finally made this work.

    The trick is to use nu-get to install RiaServices.Toolkit((just released on 8-4 ish) in both the Data Layer DLL and the Server project of the lightswitch app.

    Note -- you will have to override the constructor of your db context to read the connection string from the webconfig file something like the following:

    public ControllersAssistantContext(){
      string temp = System.Configuration.ConfigurationManager.ConnectionStrings["bd060398-acc6-4375-a886-0ff241fd1928"].ConnectionString;
      Database.Connection.ConnectionString = temp;
      Database.SetInitializer<ControllersAssistantContext>(null);
    }

    Note -- LS is correctly detecting the consraints on entities without adding attributes to the domain object class 

    Note -- I have not tested this with relationships yet but I dont forsee any issues.

    Hope someone else finds this useful.

     
    • Marked as answer by rdavison Wednesday, August 17, 2011 12:02 AM
    Tuesday, August 16, 2011 11:58 PM
  • Hi, great progress here :-), thanks a lot!
    Petr Antoš
    Saturday, August 27, 2011 7:49 AM
  • Hello,

    I'm trying the same - use WCF RIA Service with code first as LS data source, but without success. Could you share if your solution still works which assemblies (nuget packages) you references to be able to add WCF RIA Service with code first as data source in LS Server project (unfortunately I cannot find the RiaServices.Toolkit nuget package you mentioned).

    Thank you very much in advance.

    Saturday, March 7, 2015 2:44 PM