First method of Linq giving error - c#

In my webmethod of my WCF rest service i am trying to find record using Linq's First method like below
[WebInvoke(UriTemplate = "UpdateProductObject", Method = "PUT")]
public Result UpdateProductObject(ProductObjectToSave prodSave)
{
IUnitOfWork unitOfWork = new UnitOfWork((IObjectContext)_objectSetFactory);
var versions = prodSave.VersionDetails;
foreach (var versionDetail in versions)
{
var detail = versionDetail;
var dbVersionentity = _productVersionEntityRepository.First(x => x.Id == detail.Id);
if (detail.Id < 0)
{
dbVersionentity.Id = GetNextTableId("vProductVersion");
}
dbVersionentity.Name = detail.Name;
dbVersionentity.Code = detail.Name;
if (detail.Id > 0){
_productVersionEntityRepository.Update(dbVersionentity);
}
else
{
_productVersionEntityRepository.Insert(dbVersionentity);
}
}
try
{
unitOfWork.Commit();
}
catch (Exception e)
{
return new Result() { Error = e.Message };
}
return new Result() { Error = "Record updated successfully" };
}
The "_productVersionEntityRepository" is defined as below in my service.
private readonly Repository<ProductVersionEntity> _productVersionEntityRepository;
When there are no records it throws exception "Sequence contains no elements" . I have done some finding that we can use FirstOrDefault method. But somehow i am not getting that option to use FirstOrDefault. I am really new to this so may be i have missed some links where solution could have been described. Please help me or suggest me other way to do some error handling if First method fails

That's the way that First() works, it will throw an exception if the element can't be found. You can use FirstorDefault() instead and check if the element is null.
Edit: I realised that you're using a custom repository. If you have access to the source code, I advise you to add a new method called .FirstOrDefault() that will take as parameter a predicate and returns null if no entities are found.
Edit 2: Add this method to your repository:
T FirstOrDefault(Expression<Func<T, bool>> where, params Expression<Func<T, object>>[] includeProperties)
{
IQueryable<T> query = AsQueryable();
query = PerformInclusions(includeProperties, query);
return query.FirstOrDefault(where);
}
Then you can do something like this in your code:
foreach (var versionDetail in versions)
{
bool isNew = false;
var detail = versionDetail;
var dbVersionentity = _productVersionEntityRepository.FirstOrDefault(x => x.Id == detail.Id);
// not found in database
if(dbVersionentity == null)
{
isNew = true;
// create entity here
dbVersionentity = new .....;
// you don't need to do this if id is auto-generated,
// i.e. Identity column in SQL Server
dbVersionentity.Id = GetNextTableId("vProductVersion");
}
dbVersionentity.Name = detail.Name;
dbVersionentity.Code = detail.Name;
if (isNew)
{
_productVersionEntityRepository.Insert(dbVersionentity);
}
else
{
_productVersionEntityRepository.Update(dbVersionentity);
}
}

try
var dbVersionentity = _productVersionEntityRepository.Where(x => x.Id == detail.Id).FirstOrDefault();

Related

Performance issue when performing operations on entity objects

Im facing performance issue in below code in multiple foreach loops. First im getting a list of ReturnDetails and then based on detail id get the HandlingInfo object. Then based on value of action, update the ReturnsDetail Object again.
It take more than a minute for loading 3000 records of ReturnsDetail. While debugging locally, it runs for infinite amount of time.
Please let me know in anyway i can refactor this code .
Thanks for your help.
lstReturnsDetail = dcReturnsService.GetReturnDetailsInfo(header_id);
List<HandlingInfo> lstHandlingInfo = null;
foreach (ReturnsDetail oReturnsDetail in lstReturnsDetail)
{
using (DCReturns_Entities entities = new DCReturns_Entities())
{
lstHandlingInfo = entities.HandlingInfoes.Where(f => f.detail_id == oReturnsDetail.id).ToList();
if(lstHandlingInfo != null)
{
foreach (HandlingInfo oHandlingInfo in lstHandlingInfo)
{
if (oHandlingInfo.action == "DST")
{
oReturnsDetail.destroy += Convert.ToInt32(oHandlingInfo.qty);
}
else if (oHandlingInfo.action == "SHP")
{
oReturnsDetail.to_shop += Convert.ToInt32(oHandlingInfo.qty);
}
else if (oHandlingInfo.action == "RBX")
{
oReturnsDetail.in_stock += Convert.ToInt32(oHandlingInfo.qty);
}
}
}
}
oReturnsDetail.received_qty = oReturnsDetail.destroy + oReturnsDetail.to_shop + oReturnsDetail.in_stock;
}
dgReturnsDetail.DataSource = lstReturnsDetail.OrderByDescending(g => g.id).ToList();
Session[DCReturnsConstants.Returns_Detail_Entity] = lstReturnsDetail;
dgReturnsDetail.DataBind();
this is su-do code! but you should get the jist.
//modify this to return all of them into mem, and then filter on this...
//if it can not be done here then do below..
var lstReturnsDetail = dcReturnsService.GetReturnDetailsInfo(header_id);
//then create a list here which fetches all,
List<[type]> somelist
List<int> listId = lstReturnsDetail.select(x=>x.id).tolist();
using (var db = new DCReturns_Entities())
{
somelist = db.HandlingInfoes.Where(f => listId.Contains( f.detail_id)).ToList();
}
foreach (ReturnsDetail oReturnsDetail in lstReturnsDetail)
{
//performance issue is here
//using (DCReturns_Entities entities = new DCReturns_Entities())
//{
// lstHandlingInfo = entities.HandlingInfoes.Where(f => f.detail_id == oReturnsDetail.id).ToList();
//}
//insead fetach all before, into mem and filter from that list.
var lstHandlingInfo = somelist.Where(f => f.detail_id == oReturnsDetail.id).ToList();
//code ommited for reaablity
}
//code ommited for reaablity

Value cannot be null. Parameter name: first

When I run the code below, I get the following error:
Value cannot be null. Parameter name: first
Here is my code:
private async void CallWebApiDetails()
{
WebApiService oWS = new WebApiService();
lstLocalDBAlertsID = new List<string>();
try
{
ErrorHandle err = await oWS.GetAllAlerts();
var lstdistinct = err.lstServerAlertsIDs.Except(lstLocalDBAlertsID).ToList();
if (lstdistinct != null)
{
var lstOrderedLst = lstdistinct.OrderBy(i => i).ToList();
if (lstOrderedLst.Count > 0)
{
for (int i = 0; i < lstOrderedLst.Count; i++)
{
AlertData oAlertData = new AlertData();
oAlertData.Where.AlertId.Value = lstOrderedLst[i];
if (!oAlertData.Query.Load())
{
ErrorHandle oErr = new ErrorHandle();
oErr = await oWS.getSpecificAlert(lstOrderedLst[i]);
await SaveAlertData(Convert.ToInt32(lstOrderedLst[i]), oErr);
}
}
}
}
}
catch (Exception ex)
{
LogError oLE = new LogError();
oLE.logEx(ex, "CallWebApiDetails");
}
}
Can somebody tell me what's wrong with my code?
The Except extension method has first as the this parameter; it is defined as
public static IEnumerable<TSource> Except<TSource>(
this IEnumerable<TSource> first, IEnumerable<TSource> second)
{
if (first == null)
{
throw Error.ArgumentNull("first");
}
// ...
}
so presumably in this line:
var lstdistinct = err.lstServerAlertsIDs.Except(lstLocalDBAlertsID).ToList()
the value of err.lstServerAlertsIDs is null. So: fix that.
Note: it is a good idea to get familiar with using the debugger with breakpoints or at least the stack trace to identify which line is failing. You can't always (or even often) infer the context like this.

Entity.Contains(AttributeName) works for all annotation fields but not working for notetext

In following code snippet I am retrieving notes related to an order. It works fine only if notetext does contain data. Now, while debugging I found that, in other case it throws the exception that Object reference not set to an instance of an object.
I think following snippet looks good, but not sure what is missing, any idea to sort out the problem?
private void fetchDocument(IOrganizationService service, Guid vOrderId)
{
EntityCollection results = null;
string tempNote = string.Empty;
string tempFileName = string.Empty;
ColumnSet cols = new ColumnSet("subject", "filename", "documentbody", "mimetype","notetext");
QueryExpression query = new QueryExpression {
EntityName = "annotation" ,
ColumnSet = cols,
Criteria = new FilterExpression
{
Conditions = {
new ConditionExpression("objectid",ConditionOperator.Equal,vOrderId)
}
}
};
results = service.RetrieveMultiple(query);
Entity defaultRecord = results.Entities.ElementAtOrDefault(0);
if(defaultRecord.Contains("notetext"))
{
tempNote = defaultRecord.GetAttributeValue<string>("notetext");
}
if (defaultRecord.Contains("filename"))
{
tempFileName = defaultRecord.GetAttributeValue<string>("filename");
}
}
You haven't guarded defaultrecord against null.
results = service.RetrieveMultiple(query);
if (results.Entities == null || !results.Entities.Any()) return;
Entity defaultRecord = results.Entities.ElementAt(0);
Extending the answer to backup result.Entities == null check.
Retrieve multiple EntityCollection is not foolproof.
EntityCollection property:
Decomplied SDK retrieve multiple core:
protected internal virtual EntityCollection RetrieveMultipleCore(QueryBase query)
{
bool? retry = new bool?();
do
{
bool forceClose = false;
try
{
using (new OrganizationServiceContextInitializer(this))
return this.ServiceChannel.Channel.RetrieveMultiple(query);
}
catch (MessageSecurityException ex)
{
..
}
finally
{
this.CloseChannel(forceClose);
}
}
while (retry.HasValue && retry.Value);
return (EntityCollection) null;
}
Decomplied SDK Cached Organization Serivce Context Retrieve multiple:
public override EntityCollection RetrieveMultiple(QueryBase query)
{
RetrieveMultipleRequest retrieveMultipleRequest = new RetrieveMultipleRequest();
retrieveMultipleRequest.Query = query;
RetrieveMultipleResponse multipleResponse = this.Execute<RetrieveMultipleResponse>((OrganizationRequest) retrieveMultipleRequest);
if (multipleResponse == null)
return (EntityCollection) null;
else
return multipleResponse.EntityCollection;
}
public EntityCollection EntityCollection
{
get
{
if (this.Results.Contains("EntityCollection"))
return (EntityCollection) this.Results["EntityCollection"];
else
return (EntityCollection) null;
}
}
Your issue is actually at this line:
Entity defaultRecord = results.Entities.ElementAtOrDefault(0);
There are no results found, meaning
there is no Annotation that exists with an objectid of "vOrderId", or the user that is performing the query, doesn't have rights to read that record.
Regardless, you should just check for defaultRecord being null or not, and exiting if it is.
This check of null is a common occurrence, which is why I've written this ExtensionMethod:
public Entity GetFirstOrDefault(this IOrganizationService service, QueryBase qb) {
return service.RetrieveMultiple(qb)?.Entities.FirstOrDefault();
}
This would simplify your code to this:
private void fetchDocument(IOrganizationService service, Guid vOrderId)
{
EntityCollection results = null;
string tempNote = string.Empty;
string tempFileName = string.Empty;
ColumnSet cols = new ColumnSet("subject", "filename", "documentbody", "mimetype","notetext");
QueryExpression query = new QueryExpression {
EntityName = "annotation" ,
ColumnSet = cols,
Criteria = new FilterExpression
{
Conditions = {
new ConditionExpression("objectid",ConditionOperator.Equal,vOrderId)
}
}
};
var defaultRecord = service.GetFirstOrDefault(query);
if(defaultRecord != null)
{
if(defaultRecord.Contains("notetext"))
{
tempNote = defaultRecord.GetAttributeValue<string>("notetext");
}
if (defaultRecord.Contains("filename"))
{
tempFileName = defaultRecord.GetAttributeValue<string>("filename");
}
}
}

C# LinqToSql SubmitChanges() does not update, even though PK is set

I have the following code:
public int DeActivate(User entity) {
try {
using (UsersDataContext usersDC = new UsersDataContext()) {
users user = new users();
user = usersDC.users.Where(x => x.id == entity.Id).
Select(x => new users {active = x.active}).FirstOrDefault();
//user.active = entity.Active;
user.active = false;
usersDC.SubmitChanges();
return 1;
}
}
catch {
return 0;
}
}
While running an NUnit test on the method, the method returns 1, as it is supposed to do, and while de-bugging no exceptions are thrown. But, when i cross check with the DB the records have not being affected. I have tried the following: Re-created DBML file, checked for existance of PK, and checked the following sites:
MSDN question, StackOverflow question, but to no avail.
Your select statement is wrong. Try this.
public int DeActivate(User entity) {
try {
using (UsersDataContext usersDC = new UsersDataContext()) {
var user = usersDC.users.Single(x => x.id == entity.Id);
user.active = false;
usersDC.SubmitChanges();
return 1;
}
} catch {
return 0;
}
}

Does EF upsert have to be done manually?

I want to upsert reference members of an existing entity.
Do I have to write specific code for the upsert?
meaning: I have to check if I'm handling an existing reference member or a new one.
Is there any other simple way to do so?
What happens when you do only Save ?
public void SaveCofiguration(MamConfiguration_V1Ui itemUi)
{
var itemEf = mMamConfiguration_V1UiToEfConvertor.ConvertToNewEf(itemUi);
using (var maMDBEntities = new MaMDBEntities())
{
IDal<MamConfiguration_V1> mamConfigurationDal = mDalFactory.GetDal<MamConfiguration_V1>(maMDBEntities);
mamConfigurationDal.Save(itemEf);
}
}
public MamConfiguration_V1 GetById(object id)
{
id.ThrowIfNull("id");
int configurationId = Convert.ToInt32(id);
var result =
mMaMDBEntities.MamConfiguration_V1.SingleOrDefault(item => item.ConfigurationId == configurationId);
return result;
}
public MamConfiguration_V1 Save(MamConfiguration_V1 item)
{
item.ThrowIfNull("item");
var itemFromDB = GetById(item.ConfigurationId);
if (itemFromDB != null)
{
UpdateEfItem(itemFromDB, item);
// if (mMaMDBEntities.ObjectStateManager.GetObjectStateEntry(itemFromDB).State == EntityState.Detached)
// {
// mMaMDBEntities.MamConfiguration_V1.AddObject(itemFromDB);
// }
// Attached object tracks modifications automatically
mMaMDBEntities.SaveChanges();
return item;
}
private void UpdateEfItem(MamConfiguration_V1 itemFromDb, MamConfiguration_V1 itemFromUi)
{
itemFromDb.UpdatedDate = DateTime.Now;
itemFromDb.Description = itemFromUi.Description;
itemFromDb.StatusId = itemFromUi.StatusId;
itemFromDb.Name = itemFromUi.Name;
itemFromDb.NumericTraffic = itemFromUi.NumericTraffic;
itemFromDb.PercentageTraffic = itemFromUi.PercentageTraffic;
itemFromDb.Type = itemFromUi.NumericTraffic;
foreach (var item in itemFromDb.MamConfigurationToBrowser_V1.ToList())
{
if (itemFromUi.MamConfigurationToBrowser_V1.All(b => b.BrowserVersionId != item.BrowserVersionId))
{
mMaMDBEntities.MamConfigurationToBrowser_V1.DeleteObject(item);
}
}
for (int i = 0; i < itemFromUi.MamConfigurationToBrowser_V1.Count; i++)
{
var element = itemFromUi.MamConfigurationToBrowser_V1.ElementAt(i);
var item = itemFromDb.MamConfigurationToBrowser_V1.SingleOrDefault(b => b.BrowserVersionId == element.BrowserVersionId);
if (item != null)
{
// copy properties from element to item
}
else
{
element.Browser = mMaMDBEntities.Browsers.Single(browserItem =>
browserItem.BrowserID == element.BrowserID);
//element.MamConfiguration_V1 = itemFromDb;
//have also tried: element.MamConfiguration_V1 = null;
//element.MamConfiguration_V1Reference = null;
itemFromDb.MamConfigurationToBrowser_V1.Add(element);
}
}
}
But I would have expecte Save(itemUi) and SaveChanges() to work fine. No?
public void InsertOrUpdate(DbContext context, UEntity entity)
{
context.Entry(entity).State = entity.Id == 0 ?
EntityState.Added :
EntityState.Modified;
context.SaveChanges();
}
http://forums.asp.net/t/1889944.aspx/1
To avoid the overhead of a query and then insert, or throwing exceptions, you can take advantage of the underlying database support for merges or upserts.
This nuget package does the job pretty well: https://www.nuget.org/packages/FlexLabs.EntityFrameworkCore.Upsert/
Github: https://github.com/artiomchi/FlexLabs.Upsert
Example:
DataContext.DailyVisits
.Upsert(new DailyVisit
{
// new entity path
UserID = userID,
Date = DateTime.UtcNow.Date,
Visits = 1,
})
// duplicate checking fields
.On(v => new { v.UserID, v.Date })
.WhenMatched((old, #new) => new DailyVisit
{
// merge / upsert path
Visits = old.Visits + 1,
})
.RunAsync();
The underlying generated sql does a proper upsert. This command runs right away and does not use change tracking, so that is one limitation.
See 'AddOrUpdate' method of System.Data.Entity.Migrations.
http://msdn.microsoft.com/en-us/library/system.data.entity.migrations.idbsetextensions.addorupdate%28v=vs.103%29.aspx
using System.Data.Entity.Migrations;
public void Save(Person person) {
var db = new MyDbContext();
db.People.AddOrUpdate(person);
db.SaveChanges();
}
"optimistic" approach for simple scenarios (demos)...
dbContext.Find()'s intellisense help tells us that it either retrieves entity by key if already present in current context, or queries the database to get it... then we know if it exists to either add or update. i'm using EFCore v2.2.0.
var existing = _context.Find<InventoryItem>(new object[] {item.ProductId});
if (existing == null) _context.Add(item);
else existing.Quantity = item.Quantity;
_context.SaveChanges();
DbContext.Update Method
For entity types with generated keys if an entity has its primary key value set then it will be tracked in the Modified state. If the primary key value is not set then it will be tracked in the Added state. This helps ensure new entities will be inserted, while existing entities will be updated. An entity is considered to have its primary key value set if the primary key property is set to anything other than the CLR default for the property type.
For entity types without generated keys, the state set is always Modified.
read this article
you can use this sample

Categories

Resources