0

I am wondering how to convert this sql command using it with Entity Framework.

SELECT IDENT_CURRENT('tblKunde') + 1

Can someone help me with that?

Tom el Safadi
  • 6,164
  • 5
  • 49
  • 102
  • I'm not able to answer your question, but when doing some research I came across an interestingly titled blog post on `IDENT_CURRENT`: [For the last time, NO, you can't trust IDENT_CURRENT()](https://sqlperformance.com/2014/01/t-sql-queries/ident-current) - it might be worth a read. – Tony Nov 25 '16 at 00:29
  • 2
    You wouldn't. Because `IDENT_CURRENT` is a database artifact and has no place in an object model. – Robert McKee Nov 25 '16 at 00:42
  • So is there a way to achieve the same with EF? @RobertMcKee – Tom el Safadi Nov 25 '16 at 00:42
  • If by the same, you mean how do you insert a record into table, and have it assign the auto incrementing column with a new value, then yes. You just have to identify the column that is your auto-incrementing field and EF will do it for you. Similar to the post here http://stackoverflow.com/questions/16079217/how-to-generate-and-auto-increment-id-with-entity-framework or here http://stackoverflow.com/questions/10427540/entity-framework-auto-incrementing-field-that-isnt-the-id – Robert McKee Nov 25 '16 at 00:44
  • Yes I know how how to insert records but I need a way to get the next ID which will be created by the database. This is for something else. If I execute the SQL command above it will return the next ID... I need to do this with EF though. @RobertMcKee – Tom el Safadi Nov 25 '16 at 00:48
  • Well you can do it like this: `context.SqlQuery("SELECT IDENT_CURRENT('tblKunde') + 1");` but conceptually, you are doing it wrong. Bad design. – Robert McKee Nov 25 '16 at 00:49
  • I know that this is not a good way. But there is no other solution for me @RobertMcKee – Tom el Safadi Nov 25 '16 at 00:50
  • 2
    I'm sure there is another solution, but you haven't given enough information on what you are actually trying to achieve for anyone to give you any kind of good answer. More than likely, I'm guessing you are trying to generate a child record, for a new parent that isn't in the database quite yet so it needs the id of what the parent will be when it finally does get inserted (nevermind that is a bad design itself). In EF, you create the child record, attach it to the parent record and save. It does all the id stuff for you. – Robert McKee Nov 25 '16 at 00:52
  • No I don't want to insert anything. Look it is like that. I am having a UI where users can insert new customers. Every customer is linked with an ID which is the primary key with Auto increment. For information purpose I want to show the customer ID to the user. Only for information, no operation! Inside the operation Entity Framework will handle the ID automatically. @RobertMcKee – Tom el Safadi Nov 25 '16 at 00:56
  • And if you have two people that are both inserting customer records, and the latest customer record is #29, you will show them both that the customer they are about to put in is record #30? How does that work out? – Robert McKee Nov 25 '16 at 00:58
  • Yes that is true.. I don't know how to handle that. But what other option do I have? @RobertMcKee – Tom el Safadi Nov 25 '16 at 01:00
  • 2
    Same way everyone else does it, and you don't tell them the customer id until you actually save it (If they even need to know the id, which not sure why they would). For example, if you shop at amazon.com, do you know the id they put in their customer table for you? No? Of course not, why would/should you care? – Robert McKee Nov 25 '16 at 01:00
  • 1
    @Anokrize - **Beware!** IDENT_CURRENT will return "The last identity value generated can be for any session and any scope." - From the [documentation](https://msdn.microsoft.com/en-us/library/ms175098.aspx) – Tony Nov 25 '16 at 01:05
  • Look into the SEQUENCE object introduced in SQL 2012 . It solves a lot of problems with identity. https://msdn.microsoft.com/en-us/library/ff878091.aspx – under Nov 25 '16 at 02:39

0 Answers0