0

i want to set a property value in a table to default auto increment,but their are no options to do so in lightswitch2012 to my knowledge which is given that i recently started learning lightswitch,very light.

ok heres the real problem,this is the table

[customer][id,customer_id,name]

i want to set customer_id by default to id unless it is manually changed to different value.

how to acheive this?

Rakon_188
  • 573
  • 2
  • 7
  • 15

3 Answers3

0

You're right, there is no "auto-increment" data type available in LightSwitch. The ID property auto-increments, but that's a special case, handled by LightSwitch.

If you were attaching to an external SQL database, if you added a column that was an Integer Identity column, although it'll just appear as an Integer property in LightSwitch, it would still auto-increment because that's actually done in the SQL database itself.

The problem with all auto-increment properties is that you won't get the actual value until the record is saved.

Can I ask why you need an auto-increment property?

Yann Duran
  • 3,842
  • 1
  • 24
  • 24
  • i have a "customer" table with id(auto generated), customer_id, name and so on.some customers have presetid's and for others it has to be auto generated.so when inserting a new customer row its customer_id is by default set to id of the row.if customer has preset id the customer_id can be manually edited to set preset id. – Rakon_188 Apr 19 '13 at 14:17
  • is there any way i could do that? – Rakon_188 Apr 19 '13 at 14:24
  • No, you can't mix & match auto-increment & setting the ID manually. – Yann Duran Apr 20 '13 at 15:17
  • i understand i cant set id manually but what if i have another property which by default has value of id and unless manually set to other value? – Rakon_188 Apr 21 '13 at 17:35
  • You can't **manually** set the value of an _auto-incrementing_ property, so that property would have to be a **non auto-incrementing property**. But in LightSwitch, you shouldn't be attempting to use manual ID's, you should be using relationships between tables, & setting the resulting navigation properties with a retrieved entity object, you don't set ID's manually in LightSwitch. – Yann Duran Apr 21 '13 at 23:41
0

I may be misunderstanding what you are trying to achieve, but if you are using either a table or a grid, and you want to set the values for various entities for each new row your user adds (like customer_id = id, etc.), you can use the _Changed method and Add event to programmatically set any of the new row entities.

If this is along the lines of what you're looking for, watch Beth Massi's video How Do I: Copy Data from One Row into a New Row? You should be able to adapt her code to accomplish what you have in mind I think.

0

In the Entity designer make your Customer_ID not required. Write Code for Customers_Inserted.

Then, check to see if the Customer_ID is null. If it is, copy the ID field to it.

private void Customers_Inserted(Customer entity)
{
    if (entity.Customer_ID == null) {
        entity.Customer_ID = entity.ID;
    }
}
bigelowr
  • 431
  • 2
  • 11