"Predict" next model ID - c#

Sheesh...
I think this can be done but I just can't figure it out at the moment.
So I've created this app, that's working fine. But for some reason (too long to explain sorry) I would need to predict next ID to come from data table.
Note that last id + 1 will not work. I've tried this.
var lastProperty = db.Properties.OrderByDescending(p => p.PropertyID).FirstOrDefault();
int propID;
if (lastProperty == null)
{
propID = 1;
}
else
{
propID = 1 + lastProperty.PropertyID;
}
And as long as properties don't get deleted it works...
As soon as one is deleted, it messes up of course, since lets say we delete 6th Property,
last one will be 5th now, and with that code we'll get 6 5(last one) + 1, and I save my model related to Property with PropertyID 6 which I got from that code, and next Property will be 7 since database still remembers that 6 existed and was deleted... Model I intended to have same PropertyID as THAT last Property will not have it, and it'll fail...
Also, I can't add this AFTER saving Property, I realize that might seem as a solution but it's not. It has to be predicted.
Thank you... Please help...
UPDATE
This is the use case I'm trying to accomplish.
Property model with ID Name DataType properties.
List model with ID ListValue PropertyID properties.
When new Property is created user types in the Name, and from premade dropdown list for DataType selects a value, if that value is List, it opens additional form that contains a listbox, textbox and a button (add_Button). User types in the value in a textbox, and clicks the add_Button to add it to List.
Listbox is populated from List model, and add_Button, saves values from textbox to ListValue property of List, also as you've seen, I'm trying to manually add PropertyID to List by predicting it's value...
Then, upon finishing adding all the elements wanted through textbox, by clicking Create button, Property is then saved.
Hope this was clear enough.

It seems like you want to know the current state of the identity column:
SELECT IDENT_CURRENT('Table')

When need to know what your primary key values are going to be before committing to the Db you could use a uniqueidentifier (GUID) as the Pk for your Property and therefore as the Fk for your List entity. That way you can create it your self and it'll always be unique on committal.

You mention Entity Framework, have you tried adjusting your model?
StoreGeneratedPattern can be adjusted to allow automatic update of your local model when you store to the database.

Related

Best way to update a collection of SQL data

I am writing a .net/entity framework code snippet that's supposed to update/delete a bunch of MS SQL rows with the latest data passed from UI.
Say the table originally has 20 rows and the latest collection contains 15 records. Out of the 15, 9 have changes and 6 remain the same. So 9 rows will be updated, and the 5 rows that are not in the latest collection, will be deleted from the table.
So my question is, what's the best way of doing this: If I iterate over all 20 rows and try to find each of them, it would be O(mn). Deleting all table rows and re-insert them may be faster but I am not sure.
All help appreciated!
So you have a user interface element filled with items of some class. You also have a database with a table filled with items of the same class.
IEnumerable<MyClass> userInterfaceElements = ...
IQueryable<MyClass> databaseElements = ...
Note: the query is not executed yet!
You want to update the database such, that after your update your database contains the items from your user interface elements.
User interface elements that are not in the database yet will be added
Database elements that are not in the user interface need to be removed
User interface elements that are also in the database need to be updated.
You didn't write how you decide whether a user interface element is also in the database.
Let's assume you don't invent primary keys. This means that elements with a default value (zero) for your primary key are elements that are not in the database.
var itemsToAdd = userInterfaceElements.Where(row => row.Id == 0);
var itemsToUpdate = userInterfaceElements.Where(row => row.Id != 0);
var idsItemsToKeep = itemsToUpdate.Select(row => row.Id);
var itemsToRemove = databaseElements.Where(row => !idsItemsToKeep.Contains(row.Id))
The last one: remove all items that have an Id that is not in your user interface elements anymore.
Note: we still have not executed any query!
Adding the items to your database will change databaseElements, so before you make any changes you need to materialize the items
var addList = itemsToAdd.ToList();
var updateList = itemsToUpdate.ToList();
var removeList = itemsToRemove.ToList();
By now you've queried your database exactly once: you fetched all items to remove. You can't order entity framework to remove items without fetching them first.
dbContext.MyClasses.RemoveRange(removeList);
dbContext.MyClasses.AddRange(addList);
To update in entity framework, the proper method would be to fetch the data and then change the properties.
Some people prefer to attach the items to the dbContext's change tracker and tell that it is changed. This might be dangerous however, if someone else has changed some properties of these items, especially if you don't show these values in your user interface elements. So only do this if you really have a long list of items to update.
Proper way:
foreach(var itemToUpdate in updateList)
{
var fetchedItem = dbContext.MyClasses.Find(itemToUpdate.Id);
// TODO: update changed properties of the fetchedItem with values from itemToUpdate
}
Dangerous method:
foreach(var itemToUpdate in updateList)
{
dbContext.Entry(itemToUpdate).State = entityState.Modified;
}
Finally:
dbContext.SaveChanges();
Improved delete method
You've got a problem when you filled your user interface element with database values, and some other process removed one of these values from your database.
When your code looks at the primary key, it will think it is in the database, however it is not there anymore. What to do with this element? Add it again? Act as if the user also wanted it to be deleted?
To solve this kind of problems, quite often people don't delete items from their database, but declare them obsolete instead. They add a Boolean column to the table that indicates whether the item is to be deleted in the near future. This solves the problem that people want to update items while others want them to be removed.
Regularly, every month or so, a process is started to remove all obsolete objects. The chance that you want to update an obsolete object are much lower.
If this needs to be full save: don't remember a Boolean obsolete, but the obsolete date. Periodically remove all items that are obsolete for a longer time.
The nice thing about the obsolete, is that if someone declared an item obsolete by accident, there is still some time to repair this.

Database First Entity.Add(EntityObject WithAutoIncrementing Key)

I am using Entity-Framework Database First , I have my ObjectContext.Entity bound to a binding source which in turn is bound to my winforms controls. When I change text on my winforms controls it populates down to Current ObjectContext.Entity - I click CreateCopy - to copy the current ObjectContext.Entity , now when I try to Add the new object - it fails, I tried simply to add the current object and it failed with the duplicate key error (of course), if I set the Key (it is ID type int, auto increment) to 0 it does not add it to the ObjextContext.EntitySet , so I said ok I will just save changes as well and see what happens - but it is not in the context so how can it save it ?? Neither does it save it. So what could I be doing wrong.
This is my CreateCopy Code
BrandItem = new BRAND();
BrandItem = TestBrand(BrandItem);// just a test case BrandItem ID, Name Columns
this.ContextEntities.BRAND.AddObject(BrandItem); // It is not adding anything here but I sure would like it to;
I have also tried
BrandItem = (ObjectContext.BRAND)bsBrand.Current; // This fails with a non-unique key error , if I change the ID to 0 it still does not work
Does any one know how to do this with an AutoIncrementing ID key and Entity Framework Database First ??

C# assigning a drop down list value to a session value

I have a drop down list which retrieves values from a session I declare as follows.
var autoData = autoRetriever.GetAutoDataByUserId(user.Id);
Session["AutoData"] = autoData;
I then use autoData to populate my drop down list with a “Name” and a int “Value” as indexed in the database. Name and value are both part of my table in the database.
What I am trying to do now is once a user selects from the drop down I am passing the value (e.g 0,1,2,3,n) the id on the db table to a method. OnSelected changed I want to use this value and retrieve another column “Path” from my session (autoData). Namely the path that is associated with the id. Once I have the correct “Path” associated with this value I can move forward with it.
With my OnSelectedIndexChanged method I then have the following. This string selectedAuto contains my value (0,1,2,3,n)
string selectedAuto = AutoDropDown.SelectedValue;
Does anyone have any advice on how I could move forward with this? If I have not explained enough please let me know and I will delve deeper into it.
As #David mentioned yes correct i am trying to use that value to identify that record. That is the part i am finding tricky. How can i retrieve that "Path" based on the value i select.

How to set a property value to auto increment in visual studio lightswitch 2012

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?
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;
}
}
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?
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.

Assign property value from another property value that is in a related table

I have a table called PRODUCT, in which the Price of a Product is stored.
Because I know that the price of a product can change at any time, I have a table called ORDER, which holds the Quantity of the product that is being ordered by someone, and the TodaysPrice, which is the price that should be extracted from the column Price in the moment where the transaction is being done.
The problem is, I don't know how to extract a property's value and assign this value to another property in LightSwitch.
So far, what I've managed to think is to include this in the Order_Created():
this.Order.SelectedItem.Price = this.Order.SelectedItem.Product.Price;
But the code above doesn't work, I get this exception:
NullReferenceException was unhandled by user code
I'm not sure if the is correct, any help would be much appreciated. I want to know how can I do this in LightSwitch 2011 with Visual Studio 2010.
Thanks so much in advance!
Your code is probably running when SelectedItem, or Product, does not currently have a value. You should always check for null before referencing any property of an entity.
Like this (notice how each entity is checked before referencing any properties of that entity:
if (this.Order != null)
&& (this.Order.SelectedItem != null)
&& (this.Order.SelectedItem.Product != null)
{
this.Order.SelectedItem.Price = this.Order.SelectedItem.Product.Price;
}
The *Order_Created* method is not the right place to have this code, because at the point when the Order is created, you don't yet know which Product is going to be selected. The correct place to put this code is in the *Product_Changed* method, so that when the Product is selected (or changed), the Product's current price then gets enetered in the Order.
Just on another note, unless you've simplified your tables for the example, I think you're missing a table. An Order will usually will have things like an associated Customer, a Date etc. It will also have a collection of OrderLines (or some similar name) It's the OrderLine that should have the product/qty/price details, not the Order itself.

Categories