I am working on an application (EF6 Code First approach) that is interacting with a entity/table FloorPlan and has 10 records. I want to delete first 6 records as those are obsolete with new business requirements. Here's how the table currently looks:
To delete this in the Code First approach, I tried following code in disconnected state:
using (var newdbContext = new HomItUpContext())
{
var floorPlansOld = new List<FloorPlan>
{
new FloorPlan { Name = "Unitech Uniworld Gardens 2", MainImageUrl = "//cdn.homitup.com/resources/featured-ideas/floor-plans/unitech_uniworld_gardens_2/profile.jpg", IsActive = true, FloorPlanIdeas = new List<FloorPlanIdea>() },
new FloorPlan { Name = "Vatika Seven Lamps", MainImageUrl = "//cdn.homitup.com/resources/featured-ideas/floor-plans/vatika_seven_lamps/profile.jpg", IsActive = true, FloorPlanIdeas = new List<FloorPlanIdea>() },
new FloorPlan { Name = "Bestech Park View Spa", MainImageUrl = "//cdn.homitup.com/resources/featured-ideas/floor-plans/bestech_park_view_spa/profile.jpg", IsActive = true, FloorPlanIdeas = new List<FloorPlanIdea>() },
new FloorPlan { Name = "Imperia Esfera", MainImageUrl = "//cdn.homitup.com/resources/featured-ideas/floor-plans/imperia_esfera/profile.jpg", IsActive = true, FloorPlanIdeas = new List<FloorPlanIdea>() },
new FloorPlan { Name = "Raheja Vedas", MainImageUrl = "//cdn.homitup.com/resources/featured-ideas/floor-plans/raheja_vedas/profile.jpg", IsActive = true, FloorPlanIdeas = new List<FloorPlanIdea>() },
new FloorPlan { Name = "Tulip Violet Grandeur", MainImageUrl = "//cdn.homitup.com/resources/featured-ideas/floor-plans/tulip_violet_grandeur/profile.jpg", IsActive = true, FloorPlanIdeas = new List<FloorPlanIdea>() }
};
floorPlansOld.ForEach(a => newdbContext.FloorPlan.Remove(a));
floorPlansOld.ForEach(a => newdbContext.Entry(a).State = System.Data.Entity.EntityState.Deleted);
newdbContext.SaveChanges();
};
When I run update-database command via package manager console, I get following error:
The object cannot be deleted because it was not found in the ObjectStateManager.
I have also tried without changing the state of the entities but to no avail. I only want to do it in disconnected mode. Can you guys throws some pointers around this problem?
If you want to delete those records the only you need to do is create your entity instances with their existing Ids.
using (var newdbContext = new HomItUpContext())
{
var floorPlansOld = new List<FloorPlan>
{ //Put here the record's Ids you want to delete
new FloorPlan { Id=1 },
new FloorPlan { Id=2 },
new FloorPlan { Id=3 },
new FloorPlan { Id=4 },
new FloorPlan { Id=5 },
new FloorPlan { Id=6 }
};
newdbContext.RemoveRange(floorPlansOld);// You can use RemoveRange method instead a foreach to call Remove method.
newdbContext.SaveChanges();
};
Update
Well, in that case I suggest you make a query first seeking all the entities you want to delete by their names, and after that you can delete them using the RemoveRange method:
var names=new List<string>(){ "Unitech Uniworld Gardens 2", "Vatika Seven Lamps",...};
var entitiesToDelete=newdbContext.FloorPlan.Where(fp=>names.Contains(fp.Name));
newdbContext.RemoveRange(entitiesToDelete);
newdbContext.SaveChanges();
You are removing object from newdbContext.FloorPlan, buy you take them from floorPlansOld.
It looks completely wrong to me.
Try this
var a = newdbContext.FloorPlan.First();
newdbContext.FloorPlan.Remove(a);
Related
I have a DynamoDB query that works fine on the AWS Console but it doesn't on code.
Here is my query on the console:
Now here is my c# code to query it:
var query = new QueryOperationConfig
{
KeyExpression = new Expression
{
ExpressionStatement = "#pkey = :v_pkey and #skey >= :v_skey",
ExpressionAttributeNames = {
{ "#pkey", "MailingId" },
{ "#skey", "RegistroCarteiraId" },
},
ExpressionAttributeValues = new Dictionary<string, DynamoDBEntry>()
{
{ ":v_pkey", new Primitive("62", true) },
{ ":v_skey", new Primitive("00e0bbfc-aed0-4f0e-acef-a3623a9f9694") },
},
},
BackwardSearch = false,
ConsistentRead = true,
Limit = 1,
FilterExpression = new Expression
{
ExpressionStatement = "#psituacao = :v_psituacao and attribute_not_exists(#pdisponibilidade)",
ExpressionAttributeNames =
{
{ "#psituacao", "Situacao" },
{ "#pdisponibilidade", "Disponibilidade" }
},
ExpressionAttributeValues =
{
{ ":v_psituacao", new Primitive("1", true) },
}
}
};
var search = table.Query(query);
var docs = await search.GetNextSetAsync();
I get no errors, only an empty array as the result. If I change the sort key to different values, it works, but for this particular value it does not...
I've been at it all day and couldn't figure it out what is wrong.
Any help will be much appreciated.
Thanks
The problem was the LIMIT 1.
As I found out, the filter only happens on the fetched items and, since I was only fetching 1 item, when the filter occurred, the result had no records that matched the criteria.
Removing the Limit 1 solved the problem.
Is it possible to add a resource (user / facility) to an serviceappointment?
Here's my record to which I want to add resources:
var serviceAppointment = this.organizationService.Retrieve(
"serviceappointment",
serviceActivityGuid,
new ColumnSet(true));
I have a list of resources:
{
"ListOfResourceIds": [
{
"partyid": "9CDC2C51-6417-4550-A0FE-D825EE75D333"
},
{
"partyid": "9CDC2C51-6417-4550-A0FE-D825EE75D044"
}
]
}
How would I add these resources to ServiceAppointment above?
I suspect that after adding them, I would call:
organizationService.Update(serviceAppointment);
Resources are of the type ActivityParty (SystemUser). To update the service appointment, get the corresponding system user ids of the resources:
var serviceAppointment = organizationService.Retrieve(
"serviceappointment",
serviceActivityGuid,
new ColumnSet(true));
var updateServiceAppointment = new Entity("serviceappointment")
{
Id = serviceAppointment.Id
};
updateServiceAppointment["resources"] = new[]
{
new ActivityParty()
{
PartyId = new CrmEntityReference("systemuser", correspondingSystemUserId)
}
};
organizationService.Update(updateServiceAppointment);
What is the best way to do something like this:
var existingEntities = new []
{
new Product {Name = "Name1", Count = 10},
new Product {Name = "Name2", Count = 20}
};
using (var context = new ProductContext())
{
// attach several entities
context.SaveChanges();
}
Should I iterate every entity and set it to State = EntityState.Modified?
Yes, iterating and setting
context.Entry(existingproduct).State = EntityState.Modified;
is the way to go.
Source: https://msdn.microsoft.com/en-us/data/jj592676.aspx
In NetSuite SuiteTalk (Web Services), I am trying to create a search that will find all sales orders that have the status "Pending Approval". I think I have everything structured correctly and I think the problem is the status is not actually called "Pending Approval, but something else. I have tried other variants like "_pendingApproval", but my search never returns any results. If I comment out the status part, the search works correctly and returns every sales order for this particular customer.
Any thoughts on what the problem is?
C#
TransactionSearchBasic tsb = new TransactionSearchBasic() {
mainLine = new SearchBooleanField() {
searchValue = true,
searchValueSpecified = true,
},
type = new SearchEnumMultiSelectField() {
#operator = SearchEnumMultiSelectFieldOperator.anyOf,
operatorSpecified = true,
searchValue = new string[] { "_salesOrder" },
},
entity = new SearchMultiSelectField() {
#operator = SearchMultiSelectFieldOperator.anyOf,
operatorSpecified = true,
searchValue = new RecordRef[] {
new RecordRef() {
type = RecordType.customer,
internalId = "231"
}
}
},
status = new SearchEnumMultiSelectField() {
#operator = SearchEnumMultiSelectFieldOperator.anyOf,
operatorSpecified = true,
searchValue = new string[] {
"Pending Approval",
"_pendingApproval",
"pendingApproval",
"pendingapproval",
"pending approval",
"0"
}
}
};
SearchResult results = _nss.search(tsb);
It looks like the type of transaction needs to be prefixed to the status. For example:
status = new SearchEnumMultiSelectField() {
#operator = SearchEnumMultiSelectFieldOperator.anyOf,
operatorSpecified = true,
searchValue = new string[] {
"_salesOrderPendingApproval"
}
}
Try using :
"pendingApproval"
instead of
"_pendingApproval" & "Pending Approval"
I have the Relational Database as shown below on the EDM. When I am trying to submit a new F_Orden with two existing F_Producto_ProductoCompra and which has an exisitng FDistributor I get the Error mention on the title. What is the right way to do this? Please any help would really appreciated.
using (var context = new DryTypeEntities())
{
//**************************************
//Insert Order
FDistributor distributor1 = context.FDistributors.FirstOrDefault(p => p.Name == "Dverdesoto");
F_Producto_ProductoCompra productocompra1 = new F_Producto_ProductoCompra
{
FechaExpiracon = "3/13/2015",
CostoCompra = "1",
DescuentoCompra = "10%",
Id = 2
};
F_Producto_ProductoCompra productocompra2 = new F_Producto_ProductoCompra
{
FechaExpiracon = "3/13/2015",
CostoCompra = "1",
DescuentoCompra = "10%",
Id = 3
};
F_Orden orden1 = new F_Orden
{
Fecha = DateTime.Now,
Total = 12,
};
orden1.F_Producto_ProductoCompra.Add(productocompra1);
orden1.F_Producto_ProductoCompra.Add(productocompra2);
distributor1.F_Orden.Add(orden1);
context.F_Orden.AddObject(orden1);
context.FDistributors.AddObject(distributor1);
context.SaveChanges();
}
Why are you setting the value of Id manually?
I believe that identifier in table [F_Producto_ProductoCompra] is Id INT IDENTITY (1,1) PRIMARY KEY.
So there could be a collision with identifiers.
May be the row with the same Id really exists.