none
Help with building LS app, I am stuck and any advice is appreciated

    Dotaz

  • I want to make sure I am able to do this in LS and if what I have been doing is correct because I cannot get it to work.  I would like to explain from the beginning on what I am trying to accomplish.   I  have the following Tables as you can see by my pictures below.  All three tables have relationships to a USERS, Authors, Clients, and NetworkLoc Tables in a Many-to-One relationship. (board only lets me upload to pictures so you know the 3rd one is basically the same with just different table name)

    Now I am able to Create the CreateData and Details Screen without a problem.  The issue I am running into is, I do not want to have a seperate Search Screen for each of the tables below (articles, blogs, PressReleases).  I would like to have 1 search screen Grid that would show me the following from all three tables.

    ID, EntryDate, TargetMonth, Clients(FK), Users(FK), and Status.  The ID field will then be a link field to Open the details screen for each corresoponding table.

    I have followed WCF-RIA-Service-Combining-Two-Tables-by-Michael but it doesn't let me to query relationship entities which I need in order to show them in the Search Screen.  I then tried to combined How to create a RIA service wrapper for OData Source-by-Sheel Shah and I would get a reference not found error when adding the data source.  The WCF_RIA would build fine but the datasources errored out.

    In order to accomplish this search screen of 3 tables with FK into, what direction should I take? I know what I am trying to do has to be possible I am just not having luck trying to accomplish it by following the blogs posts.  Is there someone who has done this and can maybe show an example or knows a website that Bing.com is not showing me when I search.

    Thanks for taking the time to read and hopefully I can get this issue resolved.

    8. března 2012 5:15

Odpovědi

  • For eg: (from my head)

    Class MixedTable
     <Key()>
    Public Id As Guid
    Public Type As String
    Public EntryDate as Date
    Public TargetMonth As Date
    Public User As String
    End Class  
    
    <Query(IsDefault:=True)>   
     Public Function GetMixedTable() As IQueryable(Of MixedTable)
    
        Dim articles = From a In Me.Context.Articles
                   Select New MixedTable With {.Id = Guid.NewGuid,
                                                 .Type = "Article",
                                                 .ItemId = a.Id,
    					     .EntryDate = a.EntryDate,
    					     .TargetMonth = a.TargetMonth}
    
        Dim blogs = From a In Me.Context.Blogs
                   Select New MixedTable With {.Id = Guid.NewGuid,
                                                 .Type = "Blog",
                                                 .ItemId = a.Id,
    					     .EntryDate = a.EntryDate,
    					     .TargetMonth = a.TargetMonth}
    
        Dim pr = From a In Me.Context.PressRelease
                   Select New MixedTable With {.Id = Guid.NewGuid,
                                                 .Type = "PR",
                                                 .ItemId = a.Id,
    					     .EntryDate = a.EntryDate,
    					     .TargetMonth = a.TargetMonth}
    
    Return articles.Union(blogs).Union(pr)       
    
       End Function





    Like Yann Said : "If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". This will help people find the answers that they're looking for more quickly."

    • Navržen jako odpověď Nadjib Bait 9. března 2012 0:09
    • Označen jako odpověď Dbdmora 13. března 2012 0:49
    9. března 2012 0:04

Všechny reakce

  • I'm afraid that the only solution is WCF RIA Service. You should try to make it work. I advice you to create a new project in LS (test project), add two tables (or better, download Northwind db and import it for eg.) with two fields for eg and try to combine them using WCF RIA.Then you can go forward and add relationships between tables and update your WCF RIA service. Then you can try to port this to your current project.

    This article http://blogs.msdn.com/b/lightswitch/archive/2011/04/08/how-do-i-display-a-chart-built-on-aggregated-data-eric-erhardt.aspx is the easiest concerning RIA Service, just follow it step by step and you should be able to have a WCF Service running very quickly.

    Hope it helps.


    Like Yann Said : "If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". This will help people find the answers that they're looking for more quickly."


    8. března 2012 5:24
  • Hello Nadjib, I followed the article by Eric and I got it to work.  Now I need to do is find  two tables with relationships in the NorthwindDB and see if I can query it.  It is 1am my time and I need to get some sleep.  I will let you know if I need additional Help, thanks.
    8. března 2012 6:15
  • Hello Nadjib, do you happen to have any code that shows how to use the association(relationship) of a table?  The link above does not show this.
    8. března 2012 23:04
  • What do you want exaclty to do Dbdmora? Have a single Grid? or Three grids with a unique search text box where you can put a username, an article title...?



    Like Yann Said : "If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". This will help people find the answers that they're looking for more quickly."

    8. března 2012 23:48
  • For eg: (from my head)

    Class MixedTable
     <Key()>
    Public Id As Guid
    Public Type As String
    Public EntryDate as Date
    Public TargetMonth As Date
    Public User As String
    End Class  
    
    <Query(IsDefault:=True)>   
     Public Function GetMixedTable() As IQueryable(Of MixedTable)
    
        Dim articles = From a In Me.Context.Articles
                   Select New MixedTable With {.Id = Guid.NewGuid,
                                                 .Type = "Article",
                                                 .ItemId = a.Id,
    					     .EntryDate = a.EntryDate,
    					     .TargetMonth = a.TargetMonth}
    
        Dim blogs = From a In Me.Context.Blogs
                   Select New MixedTable With {.Id = Guid.NewGuid,
                                                 .Type = "Blog",
                                                 .ItemId = a.Id,
    					     .EntryDate = a.EntryDate,
    					     .TargetMonth = a.TargetMonth}
    
        Dim pr = From a In Me.Context.PressRelease
                   Select New MixedTable With {.Id = Guid.NewGuid,
                                                 .Type = "PR",
                                                 .ItemId = a.Id,
    					     .EntryDate = a.EntryDate,
    					     .TargetMonth = a.TargetMonth}
    
    Return articles.Union(blogs).Union(pr)       
    
       End Function





    Like Yann Said : "If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". This will help people find the answers that they're looking for more quickly."

    • Navržen jako odpověď Nadjib Bait 9. března 2012 0:09
    • Označen jako odpověď Dbdmora 13. března 2012 0:49
    9. března 2012 0:04
  • I want to have a single search grid.  I will try the code above.  My solution is in C# so I'm going to try to convert it. Thanks for your time, will let you know.  I really appreciate this.
    9. března 2012 4:00
  • Nadjib, Thanks for your help and I am so close.  I built the WCF_RIA service and there are no errors, below is my code.  Now the issue I have is that the LS solution will not build, I get all these warnings.

    Warning 1 The type 'ApplicationData.Implementation.ApplicationDataObjectContext' in 'D:\Documents\Visual Studio 2010\Projects\AWG\AWG\ServerGenerated\GeneratedArtifacts\ApplicationData.cs' conflicts with the imported type 'ApplicationData.Implementation.ApplicationDataObjectContext' in 'D:\Documents\Visual Studio 2010\Projects\AWG\AwgCL\bin\Debug\AwgCL.dll'. Using the type defined in 'D:\Documents\Visual Studio 2010\Projects\AWG\AWG\ServerGenerated\GeneratedArtifacts\ApplicationData.cs'. D:\Documents\Visual Studio 2010\Projects\AWG\AWG\ServerGenerated\GeneratedArtifacts\DataServiceImplementation.cs 19 125 ServerGenerated

    in my linked applicationdata.cd, then in my DataServiceImplementation.cs I get the same error

    Warning 1 The type 'ApplicationData.Implementation.ApplicationDataObjectContext' in 'D:\Documents\Visual Studio 2010\Projects\AWG\AWG\ServerGenerated\GeneratedArtifacts\ApplicationData.cs' conflicts with the imported type 'ApplicationData.Implementation.ApplicationDataObjectContext' in 'd:\Documents\Visual Studio 2010\Projects\AWG\AwgCL\bin\Debug\AwgCL.dll'. Using the type defined in 'D:\Documents\Visual Studio 2010\Projects\AWG\AWG\ServerGenerated\GeneratedArtifacts\ApplicationData.cs'. D:\Documents\Visual Studio 2010\Projects\AWG\AWG\ServerGenerated\GeneratedArtifacts\DataServiceImplementation.cs 19 125 ServerGenerated

    what would cause my WCF service conflicting with my linked application.cs, somethign about "The Type"? 

    Then when I add the WCF Service as an external datasources it errors out with this below. 

     It is fustrating that I got I think I got my WCF_RIA service coded right that now my LS is failing.  I once again appreciate you helping me with this.  I am sure you got better things to do but don't leave me yet :)

    P.S anyway we can chat via email?

        using System;
        using System.Collections;
        using System.Collections.Generic;
        using System.Data;
        using System.ComponentModel;
        using System.ComponentModel.DataAnnotations;
        using System.Linq;
        using System.ServiceModel.DomainServices.Hosting;
        using System.ServiceModel.DomainServices.Server;
        using System.Data.EntityClient;
        using System.Web.Configuration;
        using ApplicationData.Implementation;
    
    
    namespace AwgCL
    {
        // TODO: Create methods containing your application logic.
        // TODO: add the EnableClientAccessAttribute to this class to expose this DomainService to clients.
        public class Awg_DS : DomainService
        {
    
            #region Database connection
            private ApplicationDataObjectContext context;
            public ApplicationDataObjectContext Context
            {
                get
                {
                    if (this.context == null)
                    {
                        string connString =
                            System.Web.Configuration.WebConfigurationManager
                            .ConnectionStrings["_IntrinsicData"].ConnectionString;
                        EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder();
                        builder.Metadata =
                            "res://*/ApplicationData.csdl|res://*/ApplicationData.ssdl|res://*/ApplicationData.msl";
                        builder.Provider =
                            "System.Data.SqlClient";
                        builder.ProviderConnectionString = connString;
                        this.context = new ApplicationDataObjectContext(builder.ConnectionString);
                    }
                    return this.context;
                }
            }
            #endregion
    
            public class MixedTable
            {
                [Key()]
                public Int32 Id { get; set; }
                public string Type { get; set; }
                public System.DateTime EntryDate { get; set; }
                public int Clients { get; set; }
                public int Users { get; set; }
                public System.DateTime TargetMonth { get; set; }
                public string status { get; set; }
            }
    
            [Query(IsDefault = true)]
            public IQueryable<MixedTable> GetMixTable()
            {
                var article = from a in this.context.Articles
    
                              select new MixedTable
                              {
                                  Id = a.Id,
                                  Type = "Article",
                                  EntryDate = a.EntryDate.Value,
                                  Clients = a.Article_Clients,
                                  Users = a.Article_Users,
                                  TargetMonth = a.TargetMonth.Value,
                                  status = a.Status
                              };
    
                var blogs = from a in this.Context.Blogs
                            select new MixedTable
                                 {
                                     Id = a.Id,
                                     Type = "Blog",
                                     EntryDate = a.EntryDate.Value,
                                     Clients = a.Blog_Clients,
                                     Users = a.Blog_Users,
                                     TargetMonth = a.TargetMonth.Value,
                                     status = a.Status
                                 };
    
                var pr = from a in this.Context.PressReleases
                         select new MixedTable
                         {
                             Id = a.Id,
                             Type = "PR",
                             EntryDate = a.EntryDate,
                             Clients = a.PressRelease_Clients,
                             Users = a.PressRelease_Users,
                             TargetMonth = a.TargetMonth,
                             status = a.Status
                         };
    
    
                return article.Union(blogs).Union(pr); 
    
    
            }
    
             protected override int Count<T>(IQueryable<T> query)
                {
                    return query.Count();
                }
        }
    
    
    
    
    }
    
    
    
    

    9. března 2012 6:24
  • Did you update your DataSource after building your WCF Project? Because if you change the data structure in your WCF project, you must update your DataSource (Right Click on dataSource then select Update) like this LS will update the table definition by adding the new fields etc...

    Like Yann Said : "If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". This will help people find the answers that they're looking for more quickly."

    • Navržen jako odpověď Nadjib Bait 9. března 2012 13:27
    9. března 2012 13:26
  • Also, You need to make your tables classes (MixedTable) OUTSIDE of the AWG_DS class! Once I had the same issue! 


    Like Yann Said : "If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". This will help people find the answers that they're looking for more quickly."


    9. března 2012 13:30
  • Like this:
    
    
    
    namespace AwgCL
    {
        // TODO: Create methods containing your application logic.
        // TODO: add the EnableClientAccessAttribute to this class to expose this DomainService to clients.
        public class Awg_DS : DomainService
        {
    
            #region Database connection
            private ApplicationDataObjectContext context;
            public ApplicationDataObjectContext Context
            {
                get
                {
                    if (this.context == null)
                    {
                        string connString =
                            System.Web.Configuration.WebConfigurationManager
                            .ConnectionStrings["_IntrinsicData"].ConnectionString;
                        EntityConnectionStringBuilder builder = new EntityConnectionStringBuilder();
                        builder.Metadata =
                            "res://*/ApplicationData.csdl|res://*/ApplicationData.ssdl|res://*/ApplicationData.msl";
                        builder.Provider =
                            "System.Data.SqlClient";
                        builder.ProviderConnectionString = connString;
                        this.context = new ApplicationDataObjectContext(builder.ConnectionString);
                    }
                    return this.context;
                }
            }
            #endregion
    
           
    
            [Query(IsDefault = true)]
            public IQueryable<MixedTable> GetMixTable()
            {
                var article = from a in this.context.Articles
    
                              select new MixedTable
                              {
                                  Id = a.Id,
                                  Type = "Article",
                                  EntryDate = a.EntryDate.Value,
                                  Clients = a.Article_Clients,
                                  Users = a.Article_Users,
                                  TargetMonth = a.TargetMonth.Value,
                                  status = a.Status
                              };
    
                var blogs = from a in this.Context.Blogs
                            select new MixedTable
                                 {
                                     Id = a.Id,
                                     Type = "Blog",
                                     EntryDate = a.EntryDate.Value,
                                     Clients = a.Blog_Clients,
                                     Users = a.Blog_Users,
                                     TargetMonth = a.TargetMonth.Value,
                                     status = a.Status
                                 };
    
                var pr = from a in this.Context.PressReleases
                         select new MixedTable
                         {
                             Id = a.Id,
                             Type = "PR",
                             EntryDate = a.EntryDate,
                             Clients = a.PressRelease_Clients,
                             Users = a.PressRelease_Users,
                             TargetMonth = a.TargetMonth,
                             status = a.Status
                         };
    
    
                return article.Union(blogs).Union(pr); 
    
    
            }
    
             protected override int Count<T>(IQueryable<T> query)
                {
                    return query.Count();
                }
        } public class MixedTable
            {
                [Key()]
                public Int32 Id { get; set; }
                public string Type { get; set; }
                public System.DateTime EntryDate { get; set; }
                public int Clients { get; set; }
                public int Users { get; set; }
                public System.DateTime TargetMonth { get; set; }
                public string status { get; set; }
            }}


    Like Yann Said : "If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". This will help people find the answers that they're looking for more quickly."

    • Navržen jako odpověď Nadjib Bait 9. března 2012 13:33
    9. března 2012 13:33
  • Thanks for the tip, that worked by moving the class(mixedtable) outside the namespace.  I don't get any more errors when building the solution but it looks like my query is missing a null reference eventhough I don't have any null values in my data. 

    My datasource is refreshed, I created the screen but when I click on the particular screen this happens below. 

    any suggestions on this below? Thanks again so much for your help.

    9. března 2012 18:48
  • Hi, 

    Maybe you need to add some "Include" clauses in your query. 

    For example: 

    var query = from d in ctx.OrdersDetails.Include("Order").Include("Order.Customer")

    select o.Date, d.Order.OrderID, d.Order.Customer.FirstName, ..., ... 

    Include clauses help your query include data which is in related tables. 

    Would it be that? 

    Bye ... 

    9. března 2012 19:53
  • Well, I dont know exaclty what's the problem. Try isolating the field that causes problem. Just keep in the MixedTable just an Id and an ItemId and try to see if it works. Then add progressively one field at a time...

    Btw, you must change the Id type to a Guid, and not use the article Id as the MixedTable id, because Id must be unique, and if you join the three tables, you'll have problems (because Articles, Blogs and PR have all items with Id=1, Id=2...) 

    You need to have an Id which will be not useful at all for you (it's just to make each item unique) then you can create another field (ItemId) useful for you which holds the Id of the article, the blog, or PR.

     

    Like Yann Said : "If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". This will help people find the answers that they're looking for more quickly."

    • Navržen jako odpověď Nadjib Bait 9. března 2012 20:40
    9. března 2012 20:39
  • Hello Nadjib, I feel with your help I am so close.  I am still getting the Null reference error.  It is not a particular field but the whole query I believe.  Here is the new code I did for the query.  All I have is Guid = Guid.NewGuid(), & ItemId = a.Id, for each query.  I also did change my public Class mixedtable

     [Query(IsDefault = true)]
            public IQueryable<MixedTable> GetMixTable()
            {
                var article = from a in this.context.Articles
    
                              select new MixedTable
                              {
                                  Guid = Guid.NewGuid(),
                                  ItemId = a.Id,
                                 // Type = "Article",
                                  
                              };
    
                var blogs = from a in this.Context.Blogs
                            select new MixedTable
                                 {
    
                                     Guid = Guid.NewGuid(),
                                     ItemId = a.Id,
                                    // Type = "Blog",
                                     
                                 };
    
                var pr = from a in this.Context.PressReleases
                         select new MixedTable
                         {
    
                             Guid = Guid.NewGuid(),
                             ItemId = a.Id,
                             //Type = "PR",
                            
                             
                         };

    In order for me to make the GUID work, I had to add two keys. [key()] for the guid and [key] for the rest of the fields.  As you can see below in my code, If I did not do this, I would get this error when I refreshed the data source.

    Error: The Entity 'MixedTable' in DomainService 'Awg_DS' does not have a key defined. Entity types exposed by DomainService operations must have at least one public property marked with the KeyAttribute.

    public class MixedTable
    {
        [Key()]
        public Guid Guid;
        [Key]
        public int ItemId { get; set; }
        //public string Type { get; set; }
       
    }

    Btw the Mixedtable in lightswitch all it is showing is the Itemid, is that suppose to be correct?  The guid will be invisible?

    Here is the error again

    Once again thanks so much for your help. any other suggestions on how to fix the Null error?

    10. března 2012 19:33
  • No, the table must show the Guid field (rename it to Id its better)

    LS will accept only ONE primary key, so do this:

    public class MixedTable
    {
        [Key()]
        public Guid Id {get; set;}  //did your forget get; set; ?
        public int ItemId { get; set; }
        //public string Type { get; set; }
       
    }


    Like Yann Said : "If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". This will help people find the answers that they're looking for more quickly."





    10. března 2012 20:12
  • I made the mixedtable class as follow:

    public class MixedTable
    {
        [Key()]
        public Guid id { get; set; }
        public int ItemId { get; set; }
        //public string Type { get; set; }
       
    }

    I am still getting the Null reference issue

      select new MixedTable
                              {
                                  id = Guid.NewGuid(),
                                  ItemId = a.Id,
                              //    Type = "Article",
                                  
                              };

    Based on the error it seems the whole query is giving the error not a particular field.  Anything else we can look at or change?  Thanks again for your help

    10. března 2012 20:51
  • Well I think it's just a syntax error, I'm a VB developer, not having much experience with C#, even if Linq is nealry the same for the two languages.

    click on view details and show me what it says (copy/paste)



    Like Yann Said : "If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". This will help people find the answers that they're looking for more quickly."

    10. března 2012 21:12
  • Haa I found it!

    your forgot to remove the "," after commenting Type = "Article"

    select new MixedTable { Guid = Guid.NewGuid(), ItemId = a.Id //a "," was here ! // Type = "Article", };


    Like Yann Said : "If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". This will help people find the answers that they're looking for more quickly."


    10. března 2012 21:22
  • I removed the comma but I still get the error, Here are the details of the exception and a screenshot.  See if it makes sense.  Thanks so much again

    System.NullReferenceException was unhandled by user code
      Message=Object reference not set to an instance of an object.
      Source=AwgCL
      StackTrace:
           at AwgCL.Awg_DS.GetMixTable()
           at GetMixTable(DomainService , Object[] )
           at System.ServiceModel.DomainServices.Server.ReflectionDomainServiceDescriptionProvider.ReflectionDomainOperationEntry.Invoke(DomainService domainService, Object[] parameters)
           at System.ServiceModel.DomainServices.Server.DomainOperationEntry.Invoke(DomainService domainService, Object[] parameters, Int32& totalCount)
           at System.ServiceModel.DomainServices.Server.DomainService.Query(QueryDescription queryDescription, IEnumerable`1& validationErrors, Int32& totalCount)
      InnerException:

    11. března 2012 1:19
  • Ah sorry I meant the comma before that one, just after ItemId = a.Id


    Like Yann Said : "If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". This will help people find the answers that they're looking for more quickly."


    11. března 2012 1:40
  • Hello Nadjib, with removing the comma I still get the above Null refrence error.   It says I should do one of these

    http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(EHNULLREFERENCE);k(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22);k(DevLang-CSHARP)&rd=true

    But which one should I try and where in the code can I put it? 

    any other suggestions?  Thanks again for your help.

    11. března 2012 15:06
  • I'm really confused about this. I don't know, try removing white lines and all comments from the Linq statement.

    Like Yann Said : "If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". This will help people find the answers that they're looking for more quickly."

    11. března 2012 15:22
  • Hi Nadjib, I removed everything and still same error.  So fustrating that it will not work.  Do you think the class is not creating a GUID, that is why it thinks the value is null?  Can you check this link out and see if I need to add this somewhere in the class.  Thanks so much

    http://msdn.microsoft.com/en-us/library/system.guid.aspx

    12. března 2012 2:24
  • A NullReferenceException occurs when you reference a member on an uninitialized object (one that is null). So I would guess that either "context", "Articles", or "a" is null. Place a breakpoint on the expression, run the code, and inspect the values for each. You'll then see which one is null.

    Justin Anderson, LightSwitch Development Team

    12. března 2012 10:47
    Moderátor
  • Justin and Nadjib, I am going to shoot my Keyboard, the problem was a syntax error, I had context instead of "C"ontext.  The lowercase c was making it throw the null exception.

    Nadjib, I want to thank you so much for your patience and help with this.  You are a great asset to this forum.  I am hoping now I can finish up this project.  Thanks once again for your help.  I cannot wait for your custome controls to show month/year only so I can add it to my project.

    13. března 2012 0:49
  • Ah yes! I'm a VB developer, in VB there's no difference between Context and context :p, but in C# yes! I forgot about that. It's for those small things I prefer VB to C# (and I'm not a fan of { ; also... reminds me of JavaScript which I HATE ;D).

    Anyway I'm happy that it finally worked! Now you'll really enjoy LS since you made a WCF service.


    Like Yann Said : "If you found this post helpful, please "Vote as Helpful". If it actually answered your question, remember to "Mark as Answer". This will help people find the answers that they're looking for more quickly."

    14. března 2012 11:33