Jasper Manickaraj
Jasper Manickaraj

Reputation: 837

How to get the ID from table by passing Name in Entity Framework in Silverlight mvvm?

In my Silverlight with MVVM project, I'm using Entity Framework. I have one table named Customer, with fields CustomerID, Username, age. I have inserted one row programmatically.

Here the CustomerID field is an auto-incremented one. So how can I get the CustomerID value by passing UserName that was inserted?

Need the LINQ Query to get it from Entity Framework..?

Any Help?

Upvotes: 0

Views: 112

Answers (1)

McGarnagle
McGarnagle

Reputation: 102753

The auto-incremented ID should be set in the object, after you call SubmitChanges. That is, for example, newId here should contain the value:

var customer = new Customer { Username = "test", Age = 100 };
dataContext.InsertOnSubmit(customer);
dataContext.SubmitChanges();
var newId = customer.CustomerID;

If you need to get load it subsequently from the database, then use a simple query:

string name = "test";
var customer = dataContext.Customers.Where(customer => customer.Username == test).FirstOrDefault();
if (customer != null)
{
    var newId = customer.CustomerID;
}

Upvotes: 1

Related Questions