How to use NSBatchUpdateRequest with dynamic values - ios

I have entity called "Student" which has column called "firstName" and "lastName". My Existing entity looks like this
firstName
lastName
Peter
Morgan
Adam
Hayden
Andrew
Giles
Now, I have created a new attribute called "compositeName" which should contain joint name like "Peter Morgan", "Adam Hayden". How can I achieve this with NSBatchUpdateRequest in core data?
After batch update entity should look like this
firstName
lastName
compositeName
Peter
Morgan
Peter Morgan
Adam
Hayden
Adam Hayden
Andrew
Giles
Andrew Giles
I searched for tutorials on web but I couldn't find one with dynamic values. I found solution in which NSBatchUpdateRequest contains static value

You can use dynamic values via NSExpression in the propertiesToUpdate part of the expression, but unfortunately it looks like it isn't possible to perform a string concatenation using NSExpression.
I would question if you need this additional property at all - why not make it a calculated variable on the managed object:
extension Student {
var compositeName: String { firstName + lastName }
}
This prevents you ending up with the composite name and name components getting out of sync.
An alternative approach would be to look at derived properties in Core Data, but again, they are NSExpression based, so string concatenation is not available.

Related

Use Enum in Entity Framework Database first?

I have a prolem when use Enum for Database first.
I have two table:
Table1:
Id int
Label nvarchar(50)
StatusId int
Table2:
StatusId
Label
I created a Constrains key for two table, and in Asp Mvc. i load both into a Model.edmx. so I want to convert StatusId in Table1 to an enum for easy operation, but i cannot do it.
What is wrong? How can i do it? I don't want to remove Table2 from Model.
Thank in advance!
enum is implemented in the CLR as an int32, but it is not an int32. It is a separate type, so you cannot use in the Entity Framework. But could use it to reference an int32 in the other entity.
I would rather suggest to go Code First Approach for implementing your ideas.
Use this link for details and samples about Code First Approach

How to fetch records from a class which has parent class reference?

I'm new to CoreData structure, I have two classes one is "Person.h" and another is "Education.h" which has one to many relations ship Person<--->> Education`.
Here's the attributes for each classes,
Person.h
personID (unique) Number
pName String
pAge Number
educations Set
here, p stands for person
Education.h
educationID (unique) Number
eName String
eState Number
eStarted String
eCompleted String
eCenterName String
eBy Person
here, e stands for education
Ok, now I want to fetch (all / some) education details for a Person. I've successfully inserted records in both the classes with proper inputs. How to get this done? Any suggestion? Please consider me to correcting, even if this flow would not clear to you (or its wrong).
Thanks,
Hagile
Normally you'd have a Core Data relationship on Person that points to the Education entity, configured as to-many. Then once you have an instance of Person, you just look up the value of that relationship like you'd look up the value of any property. You get back a collection of zero or more related Education instances, and you don't need to do an additional fetch.
Your eBy relationship on Education suggests that you're thinking of this as if you were working with SQL. With Core Data it's normal to have a to-many relationship defined on the entity that has the relationship (and indeed, eBy should really have an inverse relationship).

Creating dynamic fields in a form MVC/SQL Server

Let's say we have predefined form that has 3 fields : FirstName, LastName, Email and this form is mapped to table users with : ID, FirstName, LastName, Email
Let's say a user want to add a new field for instance BirthDate. the user will pick the type of the field, here DateTime texbox and the text beside the field like Birth Date but how to add this column in the users Table?
Any help would be highly appreciated
Joseph
There is no realistic way for a user to dynamically add a field to the actual database table, not so that you have a new field like 'DateOfBirth' or 'ShoeSize'.
You can however, capture dynamic data from users by changing the core design of your application. Instead of storing your data in a fixed 'horizontal' table what you need to do is abstract your database and store each value as a seperate record - kind of like a 'vertical' table.
So you would have a table like
Field, Data, Type
---------------------------
"FirstName", "John", string
"LastName", "Smith", string
"DateOfBirth", "01/01/1950", datetime
"ShoeSize", "4", int
Its a issue of architecture not of technology.
There is a really good article here that discusses it in much greater depth.
It is important to understand that this does make your application quite a bit more abstract, less type safe and as such far more complex. I would not suggest trying to do this unless you fully understand the extra work involved.

EF4 DB-first: TPH approach?

I know this should not be trivial, but so far couldn't find the resolution...
Working with an EF4 DB-First model, using LINQ-to-Entities with POCOs which will be consumed by an MVC3 app.
I have three entities Customer, CustomerAdress and a lookup CustomerAddressType.
Customer CustomerAddress CustomerAddressType
---------- ---------------- -------------------
CustomerId (PK) CustomerAddressId (PK) CustomerAddressTypeId (PK)
LastName CustomerId (FK) Description (Values: Mailing, Billing)
FirstName CustomerAddressTypeId (FK)
MiddleInitial Address
.... City
State
Zip
StartDate
EndDate
As you can see CustomerAddress has a FK CustomerAddressTypeId, which identifies what type of address this is, i.e. Mailing or Billing.
I would like to:
Have is ability to do something like this: Customer.CustomerAddress.OfType<MailingAddress> to get the collection of mailing addresses for the customer.
Have a CurrentMailingAddress and CurrentBillingAddress properties, that would return the single instance CustomerAddress.OfType<> with the highest StartDate and EndDate in the future.
Would be also nice to take Address thru Zip properties and refactor those propertiess into a Complex Type Address.
I tried creating 2 inherited entities off of CustomerAddress (assuming it is TPH [table-per-hierarchy] strategy):
MailingAddress and BillingAddress, CustomerAddressTypeId being the discriminator. I did this in the model designer, and as soon as I tried adding a second inherited entity, it told me that the properties with those names already existed, and wouldn't let me rename them to match the properties of the first entity.
Any ideas how to accomplish this? Please dumb it down for me :)
Thanks!!!
It is not such trivial. TPH will be possible but you must place all properties to the base CustomerAddress and derive two sub entities which will not hold any property because all properties are shared (= must be in the parent). You will use CustomerAddressTypeId as discriminator and because of that you will not be able to map this field as property in the entity. I'm also not sure if you can have the field both in discriminator and association mapping (that is actually nice homework for me). If not you will not be able to map association between CustomerAddress and CustomerAddressType.
Both CurrentMailingAddress and CurrentBillingAddress are computed properties and they are not part of mapping. It is up to you to implement their logic in your partial part of Customer entity.
I don't understand the last point with Zip and complex type.

Entity Framework 4.0, PoCo & Navigation Properties for Lookup Tables

I have the following Models
DeltaDirectionType,
int Id
string Name
Delta,
int Id
string Name
DeltaDirectionType DeltaDirectionType
Double Value
Trade
int Id
DateTime BusinessDate
IList<Delta> deltas
So DeltaDirectionType is a lookup table, Trade holds a collection of Deltas
In the database it is implemented as follows
DeltaDirectionTypes
Id int
Name varchar(max)
Deltas
Id int
Name varchar(max)
DeltaDirectionType_Id int
Trade_Id int
Value float
Trades
Id int
BusinessDate DateTime
Delta_Id int
When I generate the model from code for the Edmx file, and (un check the foreign keys) as my model does not have properties for these. I am having problem with the navigation properties. Something like this in nHibernate would be a simple one to many mapping for the DeltaDirectionType and Delta and a many to many for Delta and Trades however, how do I firstly get it to recognise that DeltaDirectionType is a lookup and secondly get the Icollection to work for me.
I am struggling with this, Entity Framework does not make it easy for you. I have tried the usual, delete the navigation property that EF puts in for you on one side, but then you get some mapping fragments errors, properties not mapped etc.
Please help or point in the right direction.
Lookup tables are real life problems, not sure why it is so hard withing EF to implement.
Any help much appreciated
Thanks
You must either create foreign keys or navigation properties in the model to navigate the relationship.
Navigation properties can be defined as one-way, ie from the deltas table to the look-up table. A one-way navigation like this would add the appropriate property to the delta's object but not to the looup table
What you actually mean by lookup? Do you except that Delta entity will have DeltaDirectionType_Name directly mapped?
In EF you will get navigation property to DeltaDirectionType and you can access the name through this navigation property. If you don't like it you can add new property to partial class of your generated POCO and provide the Name directly in Delta entity like:
public string DeltaDirectionTypeName
{
get
{
return DeltaDirectionType != null? DeltaDirectionType.Name : String.Empty;
}
}
The only problem is that you can't use this property in Linq-To-Entities queries. In queries you always have to use navigation properties.

Resources