lapadets
lapadets

Reputation: 1095

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");
    }

Upvotes: 0

Views: 304

Answers (3)

lapadets
lapadets

Reputation: 1095

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.

Upvotes: 0

Steve Padmore
Steve Padmore

Reputation: 1740

without seeing the declaration of 'Customer' and 'NewProduct', I can only guess that you may have declared it as a Property with a Getter only - no Setter.

If this is not the case, could you show more code that demonstrates what is failing.

Upvotes: 1

Chris Cook
Chris Cook

Reputation: 2841

I suspect the issues you're encountering relate to the many changes between the early 2011 beta versions and the RTM release.

Whilst I'm a little bit rusty on the Silverlight side of things (having focused on the HTML 5 LightSwitch route in recent years) I'll try and provide some pointers which may help.

As regards your Report_InitializeDataWorkspace code (which I'm guessing is intended to default the this.Customer values based upon a passed parameter) you should be able to tackle this as follows: -

if (this.CustomerId.HasValue)
{
    var c = this.DataWorkspace.ApplicationData.Customers.Where(w => w.Id == this.CustomerId.Value).Single();
    this.Customer.Name = c.Name;
    this.Customer.AddressLine1 = c.AddressLine1;
}

If this isn't your intention, please can you provide a little more background regarding what you're trying to implement.

Regarding the CreateNewProduct code you should be able to implement something along the following lines: -

partial void CreateNewProduct_Execute()
{
    Product newProduct = this.DataWorkspace.ApplicationData.Products.AddNew();
    this.Products.SelectedItem = newProduct;
    this.OpenModalWindow("NewProduct");
}

Again, if I've misunderstood your intentions, please can you provide more background.

The following article may also help with this area (though it only covers the vb approach and not c# code): -

LightSwitch Team Blog - Creating a Custom Add or Edit Dialog (Sheel Shah)

Whilst the article is circa the 2011 edition, it should still be reasonably relevant to the 2013 update 4 version you're using.

Upvotes: 2

Related Questions