Search code examples
c#visual-studio-lightswitchlightswitch-2012

Cannot assign to foreign keys in Lightswitch - Property or indexer cannot be assigned to -- it is read only


I looked at other similar questions, but I cannot figure this out on my own. I am currently using Lightswitch 12.0.3 Update 4 version of Lightswitch and I previous versions of Lightswitch I could do these kind of things with ease... So I don't understand what changed and why I am not able to do this anymore.

I get an error:

Property or indexer 'LightSwitchApplication.Report.Customer' cannot be assigned to -- it is read only

Where Report is my screen and Customer is my table. So in the code behind (of the screen) I am trying to do this:

    partial void Report_InitializeDataWorkspace(List<IDataService> saveChangesTo)
    {
        if (this.CustomerId.HasValue)
        {
            this.Customer = this.DataWorkspace.ApplicationData.Customers.Where(w => w.Id == this.CustomerId.Value).Single();
        }
    }

And in this case, CustomerId is a local int property added to my screen.

Now the error is that this.Customer cannot be assigned to, because it is read-only.

What am I missing?

Also, I am getting the same error in another place:

Property or indexer 'LightSwitchApplication.Report.NewProduct' cannot be assigned to -- it is read only

    partial void CreateNewProduct_Execute()
    {
        this.NewProduct = this.DataWorkspace.ApplicationData.Products.AddNew();

        this.OpenModalWindow("NewProduct");
    }

Solution

  • I solved the "problem" - It was my fault and failure to understand the difference between a data item as Query of type Customer and data item as Local Property of type Customer.

    So in other words, I added both Customer and Product as local screen members instead of queries and now my code works as intended.