My Customer object has a CustomerNote property. When AutoFixture creates my Customer, it also creates a CustomerNote. My CustomerNote has a property (Guid) of the CustomerId.
Without expliciting setting a variable (var cId = Guid.New //and then passing that in at Customer creation), can set my CustomerNote.CustomerId to 'this' Customer?
NB: I'm using Autofixture to seed an EF db
var Biller = _Fixture.Create<Biller>();
var Customer = _Fixture.Build<Customer>().With(x => x.Biller, Biller)
.With(x => x.BillerId, Biller.Id)
.Create();
Related
I'm trying to use sObject to dynamically change Name field objects across while organization.
I've tried using SomeId.getSObjectType().newSObject(SomeId) to create the sObject, but when I try to change the Name field I have error
Variable does not exist: Name
Map<Id, string> idsToUpdate = new Map<Id, string>();
// Put the Id's and associated name values in the map
List<SObject> sObjectsToUpdate = new List<SObject>();
foreach(Id idToUpdate : idsToUpdate.keySet) {
SObject o1 = idToUpdate.getSObjectType().newSObject(idToUpdate);
o1.Name = idsToUpdate.get(idToUpdate);
sObjectsToUpdate.add(o1);
}
update sObjectsToUpdate;
As I can see other posts, this is the way of creation dynamic update of objects.
Any idea why this happens?
Not all objects have a name field, you should check for the existence of the name field before trying to set the field also you must use the put method
Map <String, Schema.SObjectField> fieldMap = o1.getSobjectType().getDescribe().fields.getMap();
if(fieldMap.containsKey('Name')){
o1.put('Name', 'Test');
}
I have a method that returns the list of custom types, those values are being used in both the dropdown lists, but now I want to remove a subset of them based on the type and then add to first drop down. I am using angular js, asp.net mvc. What is the best way to apply filter even before data is being rendered and keeping the same method.
Below is the method in the controller which returns names and departments as a json response. Now I want this method to return two different json objects one with subset of the current set being returned based on department they belong to.
Public JsonResult GetDetails()
{
List<Cust> Customers = new List<Cust>();
Customers = GetCustomerDetails();
var name = Customers.Select(e => new{e.custname}).Distinct().ToList();
var dept = Customers.Select(e => new{e.deptname}).Distinct().ToList();
var response = new{CustomerNames = name, CustomerDepartments = dept};
return Json(response, JsonRequestBehaviour.AllowGet();
}
I have a structure like this
DRDLines:
ID
DrawingRevisionID
DrawingRevision:
ID
Name
They're related in a one-to-many relationship.
In this code example
DRDLine line;
using (var db = new AMPX_DCEntities())
{
line = db.DRDLines.Single(p => p.ID == 1);
System.Console.WriteLine(line.DrawingRevision.ID);
}
using (var db = new AMPX_DCEntities())
{
var id = 12;
line.DrawingRevisionID = id;
}
using (var db = new AMPX_DCEntities())
{
db.Entry(line).State = System.Data.Entity.EntityState.Modified;
db.SaveChanges();
}
I get this error
A referential integrity constraint violation occurred: The property value(s) of 'DrawingRevision.ID' on one end of a relationship do not match the property value(s) of 'DRDLine.DrawingRevisionID' on the other end.
What I've found: it doesn't update relations in DRDLines inside DrawingRevision
Debugging I see:
line.DrawingRevision.DRDLines[0].ID != line.DrawingRevisionID
If I remove line
System.Console.WriteLine(line.DrawingRevision.ID);
or write it like this
System.Console.WriteLine(line.DrawingRevisionID);
everything goes without errors. But I need that line to be used.
So, how can I fix that?
My guess is that the problem is caused by repeatedly creating a new context and then disposing it. When you set the DrawingRevisionID here
using (var db = new AMPX_DCEntities())
{
var id = 12;
line.DrawingRevisionID = id;
}
line is detached from the dbcontext from which it was retreived, but isn't attached to the new DbContext you've created, hence EF won't wire up the relationships when you change the ID.
You could attach the line object back to the context before changing the ID
db.DRDLines.Attach(line);
That will change both the IDs (although you could just change the other ID manually). Since that context is then disposed, you may need to set the EntityState to Modified for the DrawingRevision (or at least of the ID property) in the last DbContext session.
Also, I would add an Include to the original query to eagerly load the DrawingRevision. At the moment its only loaded when you query the ID on the System.Console line, hence why the behaviour is different. This also causes an extra trip to the database. Putting it into an include will be more efficient and more predictable.
Gurus,
Here is my scenario:
I am defining a new client-side property (i.e. fullName) on one my entities using breeze's registerEntityTypeCtor function. The fullName property is coded to check the values of the firstName and lastName properties on the entity to determine it's value. It works when I am doing a query and receiving entities back from the db.
However, when I create a new entity on the client side (calling breeze's createEntity function) or make changes to the firstName or LastName properties without doing a save, then the custom fullName property is never updated until I perform another db pull. With breeze change tracking shouldn't the fullName property update any time either of the name properties changes?
During debug, I noticed that when I use a getter in code: (i.e. var fullName = entity.fullName) -- as I step through the code the ctor is hits the "backingStore" value of my entity which is either default value (using the createEntity) or the last db value, but never the current value of the entity.
What am I missing? Thanks
Here is an example I used for setting up the property:
function registerSpmoleSurvey(metadataStore) {
metadataStore.registerEntityTypeCtor('SpmoleSurvey', spmoleSurvey);
function spmoleSurvey() { }
Object.defineProperty(spmoleSurvey.prototype, 'fullName', {
get: function () {
var ln = this.lastName;
var fn = this.firstName;
return ln ? fn + ' ' + ln : fn;
}
});
}
Look at this page for examples of adding computeds to your Breeze entities -
http://www.breezejs.com/documentation/extending-entities
Pass in an anonymous function as the third parameter that extends the entity.
HI figure out what I was doing wrong...seems that I was a victim of camelCasing and I capitalize the property name inappropriately. Wrong as advertise now :}
I've created a database using the EF4 model-first approach. In my model, there's an N-to-M relationship between two entities:
I've filled my database with some dummy data, including 3 records of type Diagnosis and 3 records of type TreatmentSchema and associations between them. Here's the code snippet I used to do this:
using(var container = new SmartTherapyContainer()) {
var diagnosisA = new Diagnosis() { Id = Guid.NewGuid(), Name = "Diagnosis A" };
var diagnosisB = new Diagnosis() { Id = Guid.NewGuid(), Name = "Diagnosis B" };
var diagnosisC = new Diagnosis() { Id = Guid.NewGuid(), Name = "Diagnosis C" };
container.Diagnoses.AddObject(diagnosisA);
container.Diagnoses.AddObject(diagnosisB);
container.Diagnoses.AddObject(diagnosisC);
var schemaA = new TreatmentSchema() { Id = Guid.NewGuid(), Name = "Schema 1" };
var schemaB = new TreatmentSchema() { Id = Guid.NewGuid(), Name = "Schema 1" };
var schemaC = new TreatmentSchema() { Id = Guid.NewGuid(), Name = "Schema 1" };
container.Schemas.AddObject(diagnosisA);
container.Schemas.AddObject(diagnosisB);
container.Schemas.AddObject(diagnosisC);
diagnosisB.TreatmentSchemas.Add(schemaA);
diagnosisC.TreatmentSchemas.Add(schemaA);
diagnosisC.TreatmentSchemas.Add(schemaB);
diagnosisC.TreatmentSchemas.Add(schemaC);
container.SaveChanges();
}
I verified that the associations are indeed stored in the reference table created through EF4's mapping. However, when I retrieve a Diagnosis via the container.Diagnoses collection later, its .TreatmentSchemas collection is always empty.
I tried debugging into the EF4-generated code and all it does is lazily create said collection, but it doesn't fill it with the associated objects. Ayende's Entity Framework Profiler shows no queries being generated at all when the property is accessed, which leads me to believe that I'm doing something wrong here.
How can I get a list of the associated TreatmentSchemas?
Navigation properties are not loaded by default. You must use either eager loading or lazy loading but because you are using self tracking entities your choice is only eager loading because STEs don't support lazy loading. So if you want to get Diagonstic instance with all related TreatmentSchemas you must call:
var diagnosis = context.Diagnoses.Include("TreatmentSchemas").FirstOrDefault();