ASP.NET Core MVC : GET details using view model not working - entity-framework-core

I am having a problem using a view model to view get the details of an item selected in the view. I am loading a result into my index view as follows
public async Task<IActionResult> Index(string sortOrder, string currentFilter, string searchString, int? page)
{
ViewBag.CurrentSort = sortOrder;
ViewBag.ConOrgSortParm = String.IsNullOrEmpty(sortOrder) ? "Client_desc" : "Client";
ViewBag.AssignedSortParm = String.IsNullOrEmpty(sortOrder) ? "Assigned_desc" : "Assigned";
ViewBag.ExpiresSortParm = String.IsNullOrEmpty(sortOrder) ? "Expires_desc" : "Expires";
ViewBag.LastActiveSortParm = String.IsNullOrEmpty(sortOrder) ? "LastActive_desc" : "LastActive";
ViewBag.IDSortParm = String.IsNullOrEmpty(sortOrder) ? "Id_desc" : "";
if (searchString != null)
{
page = 1;
}
else
{
searchString = currentFilter;
}
ViewBag.CurrentFilter = searchString;
var results = (from node in _context.Nodes
join bnbridge in _context.BundleNodes on node.Id equals bnbridge.NodeId into NodeBundleIDGroup
from ax in NodeBundleIDGroup.DefaultIfEmpty()
join bundle in _context.Bundles on ax.BundleId equals bundle.Id into NodeBundleGroup
from bx in NodeBundleGroup.DefaultIfEmpty()
join agreement in _context.Agreements on bx.AgreementId equals agreement.Id into agGroup
from cx in agGroup.DefaultIfEmpty()
join org in _context.Organizations on cx.OrgId equals org.Id into oGroup
from ex in oGroup.DefaultIfEmpty()
join conorg in _context.Organizations on node.OrgId equals conorg.Id into tGroup
from fx in tGroup.DefaultIfEmpty()
select new NodeIndexViewModel
{
Id = node.Id,
Name = node.Name,
AssignedOrg = fx.ShortName,
ContractingOrg = ex.ShortName,
Expiry = bx.EndUtc,
LastActive = node.ActiveDate,
NodeType = bx.NodeTypeID
});
if (!String.IsNullOrEmpty(searchString))
{
results = results.Where(s => s.AssignedOrg.Contains(searchString)
|| s.Id.ToString().StartsWith(searchString));
}
switch (sortOrder)
{
case "Client_desc":
results = results.OrderByDescending(s => s.ContractingOrg);
break;
case "Client":
results = results.OrderBy(s => s.ContractingOrg);
break;
case "Assigned":
results = results.OrderBy(s => s.AssignedOrg);
break;
case "Assigned_desc":
results = results.OrderByDescending(s => s.AssignedOrg);
break;
case "Expires":
results = results.OrderBy(s => s.Expiry);
break;
case "Expires_desc":
results = results.OrderByDescending(s => s.Expiry);
break;
case "LastActive":
results = results.OrderBy(s => s.LastActive);
break;
case "LastActive_desc":
results = results.OrderByDescending(s => s.LastActive);
break;
case "Id_desc":
results = results.OrderByDescending(s => s.Id);
break;
default:
results = results.OrderBy(s => s.Id);
break;
};
int pageSize = 10;
int pageNumber = (page ?? 1);
return View(await results.ToPagedListAsync(pageNumber, pageSize));
}
This is working as expected. The problem comes when I want to access the Details of one of the listed results.
I have tried the following in the controller:
public async Task<IActionResult> Details(int? id)
{
if (id == null || _context.NodeIndexViewModel == null)
{
return NotFound();
}
var nodeIndexViewModel = await _context.NodeIndexViewModel
.FirstOrDefaultAsync(m => m.Id == id);
if (nodeIndexViewModel == null)
{
return NotFound();
}
return View(nodeIndexViewModel);
}
I get an error
PostgresException: 42P01: relation "NodeIndexViewModel" does not exist

Can't answer the question because I don't know what the problem was but I rescaffolded the database and it's working now.

Related

EF Core 3.x: duplicate value

I have the following code:
public class DeviceVerizon
{
public int DeviceId { get; set; }
public string InternalDeviceId { get; set; }
}
and
public class DeviceVerizonMap : IEntityTypeConfiguration<DeviceVerizon>
{
public void Configure(EntityTypeBuilder<DeviceVerizon> builder)
{
builder.ToTable(nameof(DeviceVerizon));
builder.HasKey(d => d.DeviceId);
builder.HasIndex(v => v.InternalDeviceId).IsUnique();
builder.HasOne(o => o.Device)
.WithOne(o => o.VerizonData)
.HasForeignKey<DeviceVerizon>(a => a.DeviceId)
.OnDelete(DeleteBehavior.Cascade)
.IsRequired()
;
}
}
so, InternalDeviceId is just unique index.
some code change InternalDeviceId for existing record and add new record with the previous InternalDeviceId.
So, localDevice.VerizonData.InternalDeviceId = 1420531689
I update it for new (remoteDevice.VerizonData.InternalDeviceId = '111111111')
localDevice.VerizonData = remoteDevice.VerizonData;
_context.Devices.Update(localDevice);
then add new record with remoteDevice.VerizonData.InternalDeviceId = 1420531689:
_context.Devices.Add(remoteDevice);
and after it I call:
await _context.SaveChangesAsync();
but I get an exception:
Cannot insert duplicate key row in object 'dbo.DeviceVerizon' with
unique index 'IX_DeviceVerizon_InternalDeviceId'. The duplicate key
value is (1420531689).
The full code:
foreach (var remoteDevice in remoteVerizonDevices)
{
var localDevice = allLocalDevicesWithIMEI.Where(a =>
(string.IsNullOrWhiteSpace(remoteDevice.IMEI)
&& a.VerizonData != null
&& a.VerizonData.InternalDeviceId == remoteDevice.VerizonData.InternalDeviceId
&& a.IMSI == remoteDevice.IMSI && a.ICCID == remoteDevice.ICCID)
|| a.IMEI == remoteDevice.IMEI).FirstOrDefault();
if (localDevice != null) // device with such imei already exists or IMEI is empty on Verizon, but InternalDeviceId is the same and ICCID with IMSI are the same
{
var verizonStatus = remoteDevice.VerizonData.State.ConvertToDeviceStatusFromVerizonStatus();
var existingRequest = _context.VerizonRequests.Where(a => a.DeviceId == localDevice.Id && a.Result == VerizonRequestResult.Pending).FirstOrDefault();
if (verizonStatus == DeviceStatus.Active && remoteDevice.IsVerizonLastActiveMoreThanDays(60))
{
// existing request for this device does not exist, create new
if (existingRequest == null)
{
localDevice.Status = DeviceStatus.PendingSuspend;
localDevice.DateStatusChanging = DateTime.UtcNow;
localDevice.LastStatusChangeSource = DeviceStatusChangeSource.Verizon;
// create verizon request to change status, because device is offline more than 60 days
verizonSuspensionList.Add(localDevice.Id);
}
}
else
{
// Is Verizon status of device is different than locally?
if (localDevice.Status != verizonStatus)
{
// check whether we have active verizon request, if not, then
if (existingRequest == null)
{
// device is suspended on suremdm, we should suspend on Verizon
if (localDevice.Status == DeviceStatus.Suspended
&& verizonStatus == DeviceStatus.Active)
{
if (localDevice.Tablet != null && localDevice.Tablet.TabletGroup.GroupName == SpecialSureMdmGroups.Suspended)
verizonSuspensionList.Add(localDevice.Id);
}
else if (localDevice.Status != DeviceStatus.Suspended && verizonStatus == DeviceStatus.Suspended)
{
// if device is suspended on verizon, we need to suspend on SureMdm
localDevice.Status = DeviceStatus.PendingSuspend;
localDevice.DateStatusChanging = DateTime.UtcNow;
localDevice.LastStatusChangeSource = DeviceStatusChangeSource.Verizon;
if (localDevice.Tablet != null)
{
localDevice.Tablet.SuspensionDate = DateTime.UtcNow;
sureMdmSuspensionList.Add(new Core.Dto.DeviceManagement.SureMdm.SureMdmMoveDeviceToSuspendedGroupDto
{
TabletId = localDevice.Tablet.TabletId,
CurrentGroupId = localDevice.Tablet.TabletGroupId
});
}
}
else
{
// other cases that device on verizon has different status, that locally
VerizonStatusIsDifferentEvent?.Invoke(
remoteDevice: remoteDevice,
verizonStatus: remoteDevice.VerizonData.State.ConvertToDeviceStatusFromVerizonStatus(),
localStatus: localDevice.Status);
}
}
}
}
var changes = DeviceIsChanged(remoteDevice, localDevice);
if (changes != null && changes.Count > 0)
modifiedDevices.Add((localDevice, changes));
var continueToUpdate = true;
// if verizon data does not exist, add it
if (localDevice.VerizonData == null)
{
var existingInternalVerizonDeviceIdRecord = allDeviceVerizons.Where(a => a.InternalDeviceId == remoteDevice.VerizonData.InternalDeviceId).FirstOrDefault();
if (existingInternalVerizonDeviceIdRecord != null)
{
DeviceWithTheSameVerizonDeviceIdAlreadyExistsEvent?.Invoke(remoteDevice: remoteDevice,
localDevice: existingInternalVerizonDeviceIdRecord.Device,
verizonDeviceId: remoteDevice.VerizonData.InternalDeviceId);
continueToUpdate = false;
}
}
if (continueToUpdate)
{
localDevice.VerizonData = remoteDevice.VerizonData;
if (!string.IsNullOrWhiteSpace(remoteDevice.ICCID))
localDevice.ICCID = remoteDevice.ICCID;
localDevice.EID = remoteDevice.EID;
localDevice.ESN = remoteDevice.ESN;
if (!string.IsNullOrWhiteSpace(remoteDevice.IMSI))
localDevice.IMSI = remoteDevice.IMSI;
if (!string.IsNullOrWhiteSpace(remoteDevice.MDN))
localDevice.MDN = remoteDevice.MDN;
localDevice.MEID = remoteDevice.MEID;
localDevice.MIN = remoteDevice.MIN;
localDevice.MSISDN = remoteDevice.MSISDN;
localDevice.SKU = remoteDevice.SKU;
localDevice.SyncVerizon = true;
if (string.IsNullOrWhiteSpace(localDevice.Name))
localDevice.Name = localDevice.IMEI;
if (!localDevice.DeviceModelId.HasValue)
{
var verizonGroup = allVerizonGroups.Where(a => a.VerizonGroupName == remoteDevice.VerizonData.GroupName).FirstOrDefault();
if (!verizonGroup.DeviceModelId.HasValue)
VerizonGroupIsNotLinkedWithDeviceModelEvent?.Invoke(remoteDevice);
else
localDevice.DeviceModelId = verizonGroup.DeviceModelId;
}
_context.Devices.Update(localDevice);
}
}
else
{
// validate by Internal Verizon DeviceId
var existingInternalVerizonDeviceIdRecord = allDeviceVerizons.Where(a => a.InternalDeviceId == remoteDevice.VerizonData.InternalDeviceId).FirstOrDefault();
if (existingInternalVerizonDeviceIdRecord != null)
{
DeviceWithTheSameVerizonDeviceIdAlreadyExistsEvent?.Invoke(remoteDevice: remoteDevice,
localDevice: existingInternalVerizonDeviceIdRecord.Device,
verizonDeviceId: remoteDevice.VerizonData.InternalDeviceId);
}
else
{
// add new record
if (!string.IsNullOrWhiteSpace(remoteDevice.IMEI))
remoteDevice.Name = remoteDevice.IMEI;
else if (!string.IsNullOrWhiteSpace(remoteDevice.VerizonData.PrimaryPlaceOfUseFirstName) || !string.IsNullOrWhiteSpace(remoteDevice.VerizonData.PrimaryPlaceOfUseLastName))
remoteDevice.Name = $"{remoteDevice.VerizonData.PrimaryPlaceOfUseFirstName} {remoteDevice.VerizonData.PrimaryPlaceOfUseLastName}";
else
remoteDevice.Name = Core.Constants.Common.Undefined;
// set device Model
var verizonGroup = allVerizonGroups.Where(a => a.VerizonGroupName == remoteDevice.VerizonData.GroupName).FirstOrDefault();
if (!verizonGroup.DeviceModelId.HasValue)
VerizonGroupIsNotLinkedWithDeviceModelEvent?.Invoke(remoteDevice);
else
remoteDevice.DeviceModelId = verizonGroup.DeviceModelId;
// set device status
remoteDevice.Status = remoteDevice.VerizonData.State.ConvertToDeviceStatusFromVerizonStatus();
if (!string.IsNullOrWhiteSpace(remoteDevice.VerizonData.CarrierName))
{
var verizonCarrier = allVerizonCarriers.Where(a => a.VerizonCarrierName == remoteDevice.VerizonData.CarrierName).FirstOrDefault();
if (verizonCarrier.CarrierId.HasValue)
remoteDevice.CarrierId = verizonCarrier.CarrierId;
else
{
// notify that carrier is not found locally
LocalCarrierIsNotFoundForNewDeviceEvent?.Invoke(remoteDevice);
}
}
remoteDevice.SyncVerizon = true;
_context.Devices.Add(remoteDevice);
newDevices.Add(remoteDevice);
}
}
}
await _context.SaveChangesAsync();
why so? only one record has 1420531689, old was updated for new value (111111111) and it saved in the one transaction... How to do it correctly?

How to do both Add and Update in Entity Framework ASP.NET Core?

I want to do both updates and add in the same action method in ASP.NET Core by Entity Framework Core, but after making an addition to a list of records in the database, I cannot update the same records to the table. It always creates a new set of records.
What is my mistake? Can anybody please help me?
[HttpPost]
public IActionResult InsertProductDetails()
{
using WebClient wc = new WebClient();
string contentString = wc.DownloadString(baseurl);
List<Dictionary<string, string>> ListJsonProductContent = new List<Dictionary<string, string>>();
var token = JToken.Parse(contentString);
if (token.Type == JTokenType.Array) // "["
{
ListJsonProductContent = JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(contentString);
}
else if (token.Type == JTokenType.Object) // "{"
{
var ObjectResponse = JsonConvert.DeserializeObject<Dictionary<string, object>>(contentString);
foreach (var x in ObjectResponse)
{
string key = x.Key.ToString();
string val = x.Value.ToString();
foreach (var dicItemML in JsonConvert.DeserializeObject<List<Dictionary<string, string>>>(val))
{
ListJsonProductContent.Add(dicItemML);
}
}
}
List<K360MappingMaster> ListMappedDataDb = new List<K360MappingMaster>();
var VLinqQuery = from KMM in _context.K360MappingMasters
where KMM.ApiUrl != null && KMM.ApiUrl == baseurl
select KMM;
ListMappedDataDb = VLinqQuery.ToList();
foreach (var dicItemML in ListJsonProductContent)
{
Dictionary<string, string> updItem = new Dictionary<string, string>();
foreach (var itemMl in dicItemML)
{
if (ListMappedDataDb.Select(s => s.ApiCatalog).ToList().Contains(itemMl.Key))
{
if (updItem.ContainsKey(ListMappedDataDb.Where(s => s.ApiCatalog == itemMl.Key).Select(s => s.K360Catalog).FirstOrDefault()))
{
if (ListMappedDataDb.Where(s => s.ApiCatalog == itemMl.Key).Select(s => s.K360Catalog).FirstOrDefault() == "Specification")
{
updItem[ListMappedDataDb.Where(s => s.ApiCatalog == itemMl.Key).Select(s => s.K360Catalog).FirstOrDefault()] += "<p>" + itemMl.Key + " :" + itemMl.Value + "<p>";
}
else
{
updItem[ListMappedDataDb.Where(s => s.ApiCatalog == itemMl.Key).Select(s => s.K360Catalog).FirstOrDefault()] += " " + itemMl.Value;
}
}
else
{
if (ListMappedDataDb.Where(s => s.ApiCatalog == itemMl.Key).Select(s => s.K360Catalog).FirstOrDefault() == "Specification")
{
updItem.Add(ListMappedDataDb.Where(s => s.ApiCatalog == itemMl.Key).Select(s => s.K360Catalog).FirstOrDefault(), "<p>" + itemMl.Key + " :" + itemMl.Value + "<p>");
}
else
{
updItem.Add(ListMappedDataDb.Where(s => s.ApiCatalog == itemMl.Key).Select(s => s.K360Catalog).FirstOrDefault(), itemMl.Value);
}
}
}
dicItemML.Remove(itemMl.Key);
}
foreach (var itemM2 in updItem)
{
dicItemML.Add(itemM2.Key, itemM2.Value);
}
}
List<CatalogProduct> ListKp = new List<CatalogProduct>();
foreach (var dicItem in ListJsonProductContent)
{
try
{
CatalogProduct Ctgkp = new CatalogProduct
{
Name = dicItem.ContainsKey("Name") ? dicItem["Name"] : "No Product",
Slug = dicItem.ContainsKey("Name") ? string.Concat(dicItem["Name"].Where(c => !char.IsWhiteSpace(c))).ToLower() : "No Slug",
Price = dicItem.ContainsKey("Price") ? decimal.Parse(dicItem["Price"], CultureInfo.InvariantCulture) : default,
ShortDescription = dicItem.ContainsKey("ShortDescription") ? dicItem["ShortDescription"] : null,
Description = dicItem.ContainsKey("Description") ? dicItem["Description"] : null,
Specification = dicItem.ContainsKey("Specification") ? dicItem["Specification"] : null,
RatingAverage = dicItem.ContainsKey("RatingAverage") ? double.Parse(dicItem["RatingAverage"], CultureInfo.InvariantCulture) : null,
MetaTitle = dicItem.ContainsKey("MetaTitle") ? dicItem["MetaTitle"] : null,
MetaKeywords = dicItem.ContainsKey("MetaKeywords") ? dicItem["MetaKeywords"] : null,
MetaDescription = dicItem.ContainsKey("MetaDescription") ? dicItem["MetaDescription"] : null,
Sku = dicItem.ContainsKey("Sku") ? dicItem["Sku"] : null,
Gtin = dicItem.ContainsKey("Gtin") ? dicItem["Gtin"] : null,
NormalizedName = dicItem.ContainsKey("NormalizedName") ? dicItem["NormalizedName"] : null,
StockQuantity = dicItem.ContainsKey("StockQuantity") ? int.Parse(dicItem["StockQuantity"], CultureInfo.InvariantCulture) : 50,
ReviewsCount = dicItem.ContainsKey("ReviewsCount") ? int.Parse(dicItem["ReviewsCount"], CultureInfo.InvariantCulture) : default,
DisplayOrder = dicItem.ContainsKey("DisplayOrder") ? int.Parse(dicItem["DisplayOrder"], CultureInfo.InvariantCulture) : 1,
OldPrice = dicItem.ContainsKey("OldPrice") ? decimal.Parse(dicItem["OldPrice"], CultureInfo.InvariantCulture) : null,
SpecialPrice = dicItem.ContainsKey("SpecialPrice") ? decimal.Parse(dicItem["SpecialPrice"], CultureInfo.InvariantCulture) : null,
SpecialPriceStart = dicItem.ContainsKey("SpecialPriceStart") ? DateTimeOffset.Parse(dicItem["SpecialPriceStart"], CultureInfo.InvariantCulture) : null,
SpecialPriceEnd = dicItem.ContainsKey("SpecialPriceEnd") ? DateTimeOffset.Parse(dicItem["SpecialPriceEnd"], CultureInfo.InvariantCulture) : null,
IsPublished = true,
PublishedOn = DateTimeOffset.Now,
CreatedById = 10,
IsDeleted = false,
CreatedOn = DateTimeOffset.UtcNow,
LatestUpdatedOn = DateTimeOffset.UtcNow,
LatestUpdatedById = 10,
HasOptions = false,
IsVisibleIndividually = true,
IsFeatured = true,
IsCallForPricing = false,
IsAllowToOrder = true,
StockTrackingIsEnabled = true
};
ListKp.Add(Ctgkp);
}
catch (Exception ex)
{
TempData["Exceptionmsg"] = ex;
return RedirectToAction("Index");
throw;
}
}
int numP = 0;
try
{
using (var transaction = _context.Database.BeginTransaction())
{
int getcount = (from gcM in _context.CatalogProducts
select gcM).Count();
if (getcount == 0)
{
_context.CatalogProducts.AddRange(ListKp);
_context.SaveChanges();
}
else
{
var catalogProducts = (from gcM in _context.CatalogProducts select gcM).ToList();
for (int i = 0; i < catalogProducts.Count; i++)
{
catalogProducts[i].Name = ListKp[i].Name;
catalogProducts[i].Price = ListKp[i].Price;
catalogProducts[i].Description = ListKp[i].Description;
catalogProducts[i].Specification = ListKp[i].Specification;
//...
}
_context.CatalogProducts.UpdateRange(catalogProducts);
_context.SaveChanges();
}
transaction.Commit();
}
if (numP > 0)
{
TempData["Sucessmsg"] = "No conflicts. " + numP + " product details saved.";
(from p in _context.K360MappingMasters
where p.ApiUrl == baseurl
select p).ToList()
.ForEach(x => x.InsertStatusFlag = true);
_context.SaveChanges();
return RedirectToAction("Index");
}
}
catch (Exception ex)
{
TempData["Exceptionmsg"] = ex;
return RedirectToAction("Index"); ;
throw;
}
return RedirectToAction("Index");
}
I have accepted your answer. Thanks. But I have few more clarifications need for the same question
1.CatalogProduct table has Auto increment primary key Id field only. I am inserting data from JSON URL. The unique field should be the product name. How to check whether the product name already exists in the table before adding/updating records.
2.If I need to add another JSON URL data, how do I differentiate the two URL product details?
There is no entities in db being tracked when doing Update, so it will directly add them.
You can try the below codes.
else
{
var catalogProducts= (from gcM in _context.CatalogProducts select gcM).ToList();
for (int i = 0; i < catalogProducts.Count; i++)
{
catalogProducts[i].Name = ListKp[i].Name;
catalogProducts[i].Price = ListKp[i].Price;
//...
}
_context.CatalogProducts.UpdateRange(catalogProducts);
numP = _context.SaveChanges();
}
Update:
using (var transaction = _context.Database.BeginTransaction())
{
var catalogProducts = _context.CatalogProducts.ToList();
foreach(var kp in ListKp)
{
if(!catalogProducts.Any(x => x.Name == kp.Name)){
_context.CatalogProducts.Add(kp);
_context.SaveChanges();
}
else
{
//Use AutoMapper automatically do the mapping
var config = new MapperConfiguration(cfg => cfg.CreateMap<CatalogProduct, CatalogProduct>().ForMember(c => c.Id, opt => opt.Ignore()));
var oldone = catalogProducts.FirstOrDefault(c => c.Name == kp.Name);
var mapper = config.CreateMapper();
oldone = mapper.Map<CatalogProduct,CatalogProduct>(kp, oldone);
_context.CatalogProducts.Update(oldone);
_context.SaveChanges();
}
}
transaction.Commit();
}

Mongodb processes only one record at a time?

I am using Mongodb in .net core.
At the same time I have many applications that query the same table. I have the problem that 1 record is processed in many applications.
I used the version in mongo but it didn't work well.
Here is my code:
try
{
TopupRequest topupReturn = null;
do
{
var topupRequest = await _paygateMongoRepository.GetOneAsync<TopupRequest>(p =>
p.Status == TopupStatus.Init && categoryCodes.Contains(p.CategoryCode) &&
p.ServiceCode == serviceCode);
if (topupRequest == null)
break;
var version = topupRequest.Version;
var versionPlus = topupRequest.Version + 1;
Expression<Func<TopupRequest, bool>> filter = p => p.Id == topupRequest.Id && p.Version == version;
var options = new FindOneAndUpdateOptions<TopupRequest, TopupRequest>
{
IsUpsert = false,
ReturnDocument = ReturnDocument.After
};
_logger.Info("Version: " + version + " version plus: " + versionPlus);
topupReturn = await _paygateMongoRepository.GetAndUpdateOne<TopupRequest, Guid>(filter,
Builders<TopupRequest>.Update.Set(m => m.Status, TopupStatus.InProcessing)
.Set(m => m.Version, versionPlus)
.Set(m => m.WorkerApp, workerApp), options);
_logger.Info("Version: " + version + " version plus: " + versionPlus);
} while (topupReturn == null);
if (topupReturn == null) return null;
if (timeout > 0 && topupReturn.CreatedTime.AddSeconds(timeout) <= DateTime.UtcNow) return null;
_logger.Info($"GetTopupPriorityAvailableVersionAsync success: {topupReturn.ToJson()}");
return topupReturn.ConvertTo<TopupRequestDto>();
}
catch (Exception e)
{
_logger.Info($"GetTopupPriorityAvailableVersionAsync error: {e}");
return null;
}
I used a transaction solution. But it also doesn't work for me.
using (var session = MongoDbContext.Client.StartSession())
{
var topups = MongoDbContext.GetCollection<TopupRequest>();
try
{
session.StartTransaction(new TransactionOptions(
readConcern: ReadConcern.Local,
readPreference: ReadPreference.Primary,
writeConcern: WriteConcern.WMajority));
var sort = Builders<TopupRequest>.Sort.Descending("CreatedTime");
var filter = Builders<TopupRequest>.Filter.Eq(p => p.Status, TopupStatus.Init)
& Builders<TopupRequest>.Filter.In("CategoryCode", categoryCodes)
& Builders<TopupRequest>.Filter.Eq(p => p.ServiceCode, serviceCode);
var options = new FindOneAndUpdateOptions<TopupRequest, TopupRequest>
{
IsUpsert = false,
ReturnDocument = ReturnDocument.After,
Sort = sort
};
var update = Builders<TopupRequest>.Update.Set("Status", TopupStatus.InProcessing);
var result = await topups.FindOneAndUpdateAsync(session, filter, update,options);
if (result == null)
session.AbortTransaction();
session.CommitTransaction();
return result;
}
catch (Exception e)
{
session.AbortTransaction();
Console.WriteLine(e);
return null;
}
}
I still had the same problem at the same time 1 record was used many times.
Please help me

Dynamic Search Using Entity Framework

I have a search screen with optional fields using Entity Framework, I want to build a dynamic query without selecting the object and filter on it.
I want to Optimize the following Existing Search, I don't want to select "var query = from p in context.APLCN_TRCKR select p;" at this stage because the application will be used by more than 100 people at once:
using (var context = new VASTEntities())
{
var query = from p in context.APLCN_TRCKR select p;
if (!string.IsNullOrEmpty(searchObj.CUST_NAME_X))
query = query.Where(p => p.CUST_NAME_X == searchObj.CUST_NAME_X.Trim());
if (!string.IsNullOrEmpty(searchObj.SURNM_X))
query = query.Where(p => p.CUST_SURNM_X == searchObj.SURNM_X.Trim());
if (!string.IsNullOrEmpty(searchObj.QUEUE_ID))
query = query.Where(p => p.QUEUE_ID == searchObj.QUEUE_ID.Trim());
if (!string.IsNullOrEmpty(searchObj.BP_ID))
query = query.Where(p => p.BPID == searchObj.BP_ID.Trim());
if (!string.IsNullOrEmpty(searchObj.UserID))
query = query.Where(p => p.CURR_OWNR_USER_ID == searchObj.UserID.Trim());
if (!string.IsNullOrEmpty(searchObj.APLCN_TRCKR_ID))
query = query.Where(p => p.APLCN_TRCKR_ID == searchObj.APLCN_TRCKR_ID.Trim());
if (!string.IsNullOrEmpty(searchObj.APLCN_STTS_ID))
query = query.Where(p => p.APLCN_STTS_ID == searchObj.APLCN_STTS_ID.Trim());
if (!string.IsNullOrEmpty(searchObj.CUST_ID))
query = query.Where(p => p.CUST_ID == searchObj.CUST_ID.Trim());
if (!string.IsNullOrEmpty(searchObj.CELL_ID))
query = query.Where(p => p.CELL_ID == searchObj.CELL_ID.Trim());
if (!string.IsNullOrEmpty(searchObj.ORIGN_ID))
query = query.Where(p => p.ORIGN_ID == searchObj.ORIGN_ID.Trim());
if (!string.IsNullOrEmpty(searchObj.ORGTN_CHANL_ID))
query = query.Where(p => p.ORGTN_CHANL_ID == searchObj.ORGTN_CHANL_ID.Trim());
if (!string.IsNullOrEmpty(searchObj.CR_DCSN_ID))
query = query.Where(p => p.CR_DCSN_ID == searchObj.CR_DCSN_ID.Trim());
if (!string.IsNullOrEmpty(searchObj.SBSA_CUST_I))
query = query.Where(p => p.SBSA_CUST_I == searchObj.SBSA_CUST_I.Trim());
if (!string.IsNullOrEmpty(searchObj.USER_ID_APP_CRTD))
query = query.Where(p => p.USER_ID_APP_CRTD == searchObj.USER_ID_APP_CRTD.Trim());
if (!string.IsNullOrEmpty(searchObj.RGION_ID))
{
int r = int.Parse(searchObj.RGION_ID.Trim());
query = query.Where(p => p.RGION_ID == r);
}
if (!string.IsNullOrEmpty(searchObj.CR_REGION))
{
int x = int.Parse(searchObj.CR_REGION);
if (x == 0)
{
// check 0 - not applicable or null
query = query.Where(p => p.CR_REGION_ID == 0 || p.CR_REGION_ID == null);
}
else
{
query = query.Where(p => p.CR_REGION_ID == x);
}
}
if (!string.IsNullOrEmpty(searchObj.Process_Type))
query = query.Where(p => p.PRCES_TYPE_ID == searchObj.Process_Type.Trim());
query.ToList();
foreach (var a in query)
{
searchAppsObj.Add(Translator.TranslateReqObjToBO.TranslateDTOToSearchApp(a));
}
if (query.Count() == 0)
{
throw new Exception("No Applications Found.");
}
context.Connection.Close();
return searchAppsObj;
}
I want to do something like this but this one is not working properly:
string cust_name_x = "", surname_x = "", queue_id = "", bp_id = "", user_id = "", aplcn_trckr_id = "",
aplcn_stts_id = "", cust_id = "", process_type = "", cr_region = "", cell_id = "", Origin = "", region = "", channel = "", credit_verdict = "", sbsa_cust_id = "", app_creator_id = "";
if (!string.IsNullOrEmpty(searchObj.CUST_NAME_X))
cust_name_x = searchObj.CUST_NAME_X.Trim();
if (!string.IsNullOrEmpty(searchObj.SURNM_X))
surname_x = searchObj.SURNM_X.Trim();
if (!string.IsNullOrEmpty(searchObj.QUEUE_ID))
queue_id = searchObj.QUEUE_ID.Trim();
if (!string.IsNullOrEmpty(searchObj.BP_ID))
bp_id = searchObj.BP_ID;
if (!string.IsNullOrEmpty(searchObj.UserID))
user_id = searchObj.UserID.Trim();
if (!string.IsNullOrEmpty(searchObj.APLCN_TRCKR_ID))
aplcn_trckr_id = searchObj.APLCN_TRCKR_ID.Trim();
if (!string.IsNullOrEmpty(searchObj.APLCN_STTS_ID))
aplcn_stts_id = searchObj.APLCN_STTS_ID.Trim();
if (!string.IsNullOrEmpty(searchObj.CUST_ID))
cust_id = searchObj.CUST_ID.Trim();
if (!string.IsNullOrEmpty(searchObj.Process_Type))
process_type = searchObj.Process_Type.Trim();
if (!string.IsNullOrEmpty(searchObj.CR_REGION))
cr_region = searchObj.CR_REGION.Trim();
if (!string.IsNullOrEmpty(searchObj.CELL_ID))
cell_id = searchObj.CELL_ID.Trim();
if (!string.IsNullOrEmpty(searchObj.ORIGN_ID))
Origin = searchObj.ORIGN_ID.Trim();
if (!string.IsNullOrEmpty(searchObj.RGION_ID))
region = searchObj.RGION_ID.Trim();
if (!string.IsNullOrEmpty(searchObj.ORGTN_CHANL_ID))
channel = searchObj.ORGTN_CHANL_ID.Trim();
if (!string.IsNullOrEmpty(searchObj.CR_DCSN_ID))
credit_verdict = searchObj.CR_DCSN_ID.Trim();
if (!string.IsNullOrEmpty(searchObj.SBSA_CUST_I))
sbsa_cust_id = searchObj.SBSA_CUST_I.Trim();
if (!string.IsNullOrEmpty(searchObj.USER_ID_APP_CRTD))
app_creator_id = searchObj.USER_ID_APP_CRTD.Trim();
using (var context = new VASTEntities())
{
var query = from p in context.APLCN_TRCKR
where
p.CUST_NAME_X.Contains(cust_name_x) &&
p.CUST_SURNM_X.Contains(surname_x) &&
p.QUEUE_ID.Contains(queue_id) &&
p.BPID.Contains(bp_id) &&
p.CURR_OWNR_USER_ID.Contains(user_id) &&
p.APLCN_TRCKR_ID.Contains(aplcn_trckr_id) &&
p.APLCN_STTS_ID.Contains(aplcn_stts_id) &&
p.CUST_ID.Contains(cust_id) &&
p.PRCES_TYPE_ID.Contains(process_type) &&
p.CELL_ID.Contains(cell_id) &&
SqlFunctions.StringConvert((double)p.CR_REGION_ID).Contains(cr_region) &&
p.ORIGN_ID.Contains(Origin) &&
SqlFunctions.StringConvert((double)p.RGION_ID).Contains(region) &&
p.ORGTN_CHANL_ID.Contains(channel) &&
p.CR_DCSN_ID.Contains(credit_verdict) &&
p.SBSA_CUST_I.Contains(sbsa_cust_id)
select p;
query.ToList();
if (query.Count() == 0)
{
throw new Exception("No Applications Found.");
}
foreach (var a in query)
{
searchAppsObj.Add(Translator.TranslateReqObjToBO.TranslateDTOToSearchApp(a));
}
context.Connection.Close();
return searchAppsObj;
}
You can just create a collection of lambda expression like below:
var filters = new List<Expression<Func<Application, bool>>>();
if (!string.IsNullOrWhitespace(searchObj.CUST_NAME_X))
filters.Add(application => application .CUST_NAME_X.Contains(searchObj.CUST_NAME_X.Trim());
if (!string.IsNullOrEmpty(searchObj.SURNM_X))
filters.Add(application => application .CUST_SURNM_X.Contains(searchObj.SURNM_X.Trim());
// And so on for all criteria
After that you can do a loop on filters like below:
using (var context = new VASTEntities())
{
var query = context.APLCN_TRCKR;
foreach(var filter in filters)
{
query = query.Where(filter);
}
var result = query.ToList();
if (result.Count() == 0)
{
throw new Exception("No Applications Found.");
}
foreach (var a in result)
{
searchAppsObj.Add(Translator.TranslateReqObjToBO.TranslateDTOToSearchApp(a));
}
context.Connection.Close();
return searchAppsObj;
}
change
var query = from p in context.APLCN_TRCKR select p;
to
var query = context.APLCN_TRCKR.AsQueryable();
And when the filtering work is done:
await query.ToListAsync() // this one goes to the database
Also have a look at this: Linq query filtering
In Addition: don't call context.Connection.Close(); as this is going to be executed anyway because using behaves like try { ... } finally { // dispose work }

Entity Framework, building query based on criteria

I was wondering if anyone had a better idea how to do this. atm returning IQueryable<Member> as ObjectQuery<Member> seems dirty to me.
namespace Falcon.Business.Repositories
{
using System;
using System.Data.Objects;
using System.Linq;
using Falcon.Business.Criteria;
using Falcon.Business.Entities;
using Falcon.Business.Enums;
using Falcon.Business.Extensions;
using Falcon.Business.Repositories.Interfaces;
using Falcon.Business.Services;
using Falcon.Business.Services.Interfaces;
using Falcon.Core.Extensions;
public class MemberRepository : LinqRepository<Member>, IMemberRepository
{
public Member Fetch(MemberCriteria criteria)
{
ObjectQuery<Member> query = base.CreateQuery();
query = this.AddRelations(query);
query = this.AddCriteria(query, criteria);
query = this.AddCriteriaOrder(query, criteria);
return query.FirstOrDefault();
}
public IPagerService<Member> FetchAll(MemberCriteria criteria)
{
int page = (criteria.Page.HasValue) ? criteria.Page.Value : 1;
int limit = criteria.Limit;
int start = (page * limit) - limit;
int total = this.Count(criteria);
ObjectQuery<Member> query = base.CreateQuery();
query = this.AddRelations(query);
query = this.AddCriteria(query, criteria);
query = this.AddCriteriaOrder(query, criteria);
return new PagerService<Member>(query.Skip(start).Take(limit).ToList(), page, limit, total);
}
public int Count(MemberCriteria criteria)
{
ObjectQuery<Member> query = base.CreateQuery();
query = this.AddCriteria(query, criteria);
return query.Count();
}
public ObjectQuery<Member> AddCriteria(IQueryable<Member> query, MemberCriteria criteria)
{
if (criteria.Title.HasValue())
{
query = query.Where(q => q.Title == criteria.Title);
}
if (criteria.TitleUrl.HasValue())
{
query = query.Where(q => q.TitleUrl == criteria.TitleUrl);
}
if (criteria.EmailAddress.HasValue())
{
query = query.Where(q => q.EmailAddress == criteria.EmailAddress);
}
if (criteria.HostAddress.HasValue())
{
query = query.Where(q => q.HostAddress == criteria.HostAddress);
}
query = query.Where(q => q.Status == criteria.Status);
return query as ObjectQuery<Member>;
}
public ObjectQuery<Member> AddCriteriaOrder(IQueryable<Member> query, MemberCriteria criteria)
{
if (criteria.Sort == SortMember.ID)
{
query = criteria.Order == SortOrder.Asc
? query.OrderBy(q => q.ID)
: query.OrderByDescending(q => q.ID);
}
else if (criteria.Sort == SortMember.Posts)
{
query = criteria.Order == SortOrder.Asc
? query.OrderBy(q => q.Posts)
: query.OrderByDescending(q => q.Posts);
}
else if (criteria.Sort == SortMember.Title)
{
query = criteria.Order == SortOrder.Asc
? query.OrderBy(q => q.Title)
: query.OrderByDescending(q => q.Title);
}
else if (criteria.Sort == SortMember.LastLogin)
{
query = criteria.Order == SortOrder.Asc
? query.OrderBy(q => q.LastLogin)
: query.OrderByDescending(q => q.LastLogin);
}
else if (criteria.Sort == SortMember.LastVisit)
{
query = criteria.Order == SortOrder.Asc
? query.OrderBy(q => q.LastVisit)
: query.OrderByDescending(q => q.LastVisit);
}
else
{
query = criteria.Order == SortOrder.Asc
? query.OrderBy(q => q.Created)
: query.OrderByDescending(q => q.Created);
}
return query as ObjectQuery<Member>;
}
private ObjectQuery<Member> AddRelations(ObjectQuery<Member> query)
{
query = query.Include(x => x.Country);
query = query.Include(x => x.TimeZone);
query = query.Include(x => x.Profile);
return query;
}
}
}
I also do not like returning an objectquery, because doing so will make you very dependent on Entity Framwork. Knowing Microsoft they propably make a lot of changes in version 2, so you do not want to do this.
NHibernate uses criteria, a bit like you suggested, but their implementation is a lot more generic. I like the more generic implementation more then you example because then you do not need to build criteria for every object. On the other hand, you implementation is typed, which is also very neat. If you want the best of both, a more generic implementation that is typed, you might want to take a look at the NHibernate implementation but instead of using strings, use lambda functions and .Net generics. I could post an example how to do this, but I'm currently not on my own machine.