I'm trying to create a repository of data that I can use for testing purposes for an emerging car production and design company.
Beginning Automapper Question:
In this project, I have 2 classes that share the same properties for the most part. I don't need the Id, so I am ignoring that.
My existing code looks like this:
Mapper.CreateMap<RaceCar, ProductionCar>()
.Ignore(d => d.fId) //ignore the ID
.ForMember(d=> d.ShowRoomName,
o=> o.MapFrom(s => s.FactoryName) //different property names but same thing really
//combine into my new test car
var testCarObject = Mapper.Map<RaceCar, ProductionCar>()
My main requirements are:
1) I need to create 100 of these test car objects
2) and that for every ProductionCar I use, it needs to have a corresponding RaceCar which are matched up by the name(ShowRoomName & FactoryName)
So is there a way of sticking this in some type of loop or array so that I can create the needed 100?
Also, is there a way to ensure that each new test car has the combined FactoryCar and RaceCar?
Thanks!
Use AutoMapper with AutoFixture:
var fixture = new Fixture();
var items = Enumerable.Range(1, 100)
.Select(i => fixture.Create<RaceCar>())
.Select(car => new { RaceCar = car, ProductionCar = Mapper.Map<RaceCar, ProductionCar>(car))
.ToList();
items.Profit()
Related
I have CoreData with simple relationships as you can see below. One entity Word with 4 attributes and a Chapter entity with a one to many relationship (each word figures in only one chapter and chapters contains several words). When I try to import a file with a list of words and associated chapter, the chapters which are not yet in the database are created (which is what I want) but the chapters that already exists are created a 2nd time (new same entry in coredata). Is there an option I can activate in xcdatamodel to check and avoid duplicate entries on the relation entity?
Code details ->
fileprivate func saveAllWords(_ items: [(name: String, definition: String, example: String, chapter: String)]?) {
for item in items! {
let newWord = Word(context: self.context)
newWord.name = item.name.trimmingCharacters(in: .whitespaces)
newWord.definition = item.definition.trimmingCharacters(in: .whitespaces)
newWord.example = item.example.trimmingCharacters(in: .whitespaces)
newWord.option = 10 // option tag indicating that it's a new entry from external fileI generate a classic word
//
let myNewChapter = Chapter(context: self.context)
myNewChapter.name = item.chapter
newWord.chapter = myNewChapter
}
……
// Save the data in Core Data
do {
try self.context.save()
}
catch {
}
Any recommendation how to implement this uniqueness constraint to solve my duplicate issue?
You have to create a constraint for the unique attribute. It looks like you want the name of the Chapter to be unique
In your xcdatamodeld select your attribute, then right constraint and add they attribute.
Last but not least you will have to add merge policy for your Context, mostly likely in your AppDelegate. There are different merge policy. You should check them out which fits your needs most
context.mergePolicy = NSMergeByPropertyObjectTrumpMergePolicy
It may be I'm still thinking in the Linq2Sql mode, but I'm having a hard time translating this to OrmLite.
I have a customers table and a loyalty card table.
I want to get a list of customers and for each customer, have a list of express cards.
My strategy is to select customers, join to loyalty cards, group by whole customer table, and then map the cards to a single property on customer as a list.
Things are not named by convention, so I don't think I can take advantage of the implicit joins.
Thanks in advance for any help.
Here is the code I have now that doesn't work:
query = query.Join<Customer, LoyaltyCard>((c, lc) => c.CustomerId == lc.CustomerId)
.GroupBy(x => x).Select((c) => new { c, Cards = ?? What goes here? });
Edit: I thought maybe this method:
var q = db.From<Customer>().Take(1);
q = q.Join<Customer, LoyaltyCard>().Select();
var customer = db.SelectMulti<Customer,LoyaltyCard>(q);
But this is giving me an ArgumentNullException on parameter "key."
It's not clear from the description or your example code what you're after, but you can fix your SelectMulti Query with:
var q = db.From<Customer>()
.Join<Customer, LoyaltyCard>();
var results = db.SelectMulti<Customer,LoyaltyCard>(q);
foreach (var tuple in results)
{
Customer customer = tuple.Item1;
LoyaltyCard custCard = tuple.Item2;
}
Is it possible to select a single object from a group created like this?
var r = new fabric.Rect(...);
var l = new fabric.Line(...);
var roadGroup = new fabric.Group([r,l],{ ... });
So I want to have a group, but select objects l or r separately.
The simple answer is yes, but you should make sure you take into account the purpose of a group.
To get a handle on an object that is wrapped in a group you can do something like this:
var r = roadGroup._objects[0];
var l = roadGroup._objects[1];
To select a child of a group try something like this:
fabricCanvas.setActiveObject(roadGroup._objects[0]);
soapbox:
The purpose of creating a group is to treat several objects as if they were a single one. The purpose of selecting an object is to allow user interactions with an object. If you want your user to interact with a portion of a group, you might want to consider not grouping them in the first place, or else un-grouping them prior to selecting the child object.
/soapbox
I believe _objects is to be used internally only and may thus change in the future.
To me it group.item(indexOfItem) seems to be the way
So I had this scenario where I have multiple images in a box. Those all images move along with the box (as a group) but user should also be able to select an individual image and move it.
Basically I wanted to select individual objects (in my case images) of group, I did it like this:
groupImages.forEach(image => image.on('mousedown', function (e) {
var group = e.target;
if (group && group._objects) {
var thisImage = group._objects.indexOf(image);
var item = group._objects[thisImage];//.find(image);
canvas.setActiveObject(item);
}
}));
groupImages could be list of objects which you want to select individually.
Hi I have an existing database with a table with 30 fields, I want to split the table into many models so I could retrieve/save fields that I need and not every time retrieve/save the whole object from the db. using c#.
I think I should be using Code-First. Could someone provide an example or a tutorial link?
thanks,
You don't need to split table to be able to load a subset of field or persist subset of fields. Both operations are available with the whole table mapped to single entity as well.
For selection you simply have to use projection:
var data = from x in context.HugeEntities
select new { x.Id, x.Name };
You can use either anonymous type in projection or any non-mapped class.
For updates you can simply use:
var data = new HugeEntity { Id = existingId, Name = newName };
context.HugeEntities.Attach(data);
var dataEntry = context.Entry(data);
dataEntry.Property(d => d.Name).IsModified = true; // Only this property will be updated
context.SaveChanges();
Or:
var data = new HugeEntity { Id = existingId };
context.HugeEntities.Attach(data);
data.Name = newName;
context.SaveChanges(); // Now EF detected change of Name property and updated it
Mapping multiple entities to single table must follows very strict rules and it is possible only with table splitting were all entities must be related with one-to-one relation (and there are some problems with more than two entities per split table in code first) or with table-per-hierarchy inheritance. I don't think that you want to use any of them for this case.
Yet another newbie SubSonic/ActiveRecord question. Suppose I want to insert a couple of records, currently I'm doing this:
using (var scope = new System.Transactions.TransactionScope())
{
// Insert company
company c = new company();
c.name = "ACME";
c.Save();
// Insert some options
company_option o = new company_option();
o.name = "ColorScheme";
o.value = "Red";
o.company_id = c.company_id;
o.Save();
o = new company_option();
o.name = "PreferredMode";
o.value = "Fast";
o.company_id = c.company_id;
o.Save();
scope.Complete();
}
Stepping through this code however, each of the company/company_option constructors go off and create a new myappDB object which just seems wasteful.
Is this the recommended approach or should I be trying to re-use a single DB object - and if so, what's the easiest way to do this?
I believe you can use the same object if you want to by setting its IsNew property to true, then change its data properties, save it again, repeat. Easy enough.
I 'm not so sure that you should bother, though. It depends on how bad those constructors are hurting you.
In my eyes assigning multiple objects to a single var is never a good idea but thats arguable. I would do this:
// Insert some options
company_option o1 = new company_option();
o1.name = "ColorScheme";
o1.value = "Red";
o1.company_id = c.company_id;
o1.Save();
company_option o2 = new company_option();
o2.name = "PreferredMode";
o2.value = "Fast";
o2.company_id = c.company_id;
o2.Save();
I you are worried about performance, that shouldn't be a issue unless you want to insert or update many objects at once. And again, in this case the time used for inserting the data takes longer than for the object creation.
If you are worried about performance you can skip the object creation and saving part completly by using a Insert query:
http://www.subsonicproject.com/docs/Linq_Inserts
db.Insert.Into<company_option>(
x => x.name,
x => x.value,
x => x.company_id)
.Values(
"ColorScheme",
"Red",
c.company_id
).Execute();