Search code examples
c#core-dataxamarin.iosmonomac

Generate classes from CoreData model


I'm trying to develop a pair of C# applications, one iOS, one Mac, that use the same CoreData database. Effectively, the desktop app populates it, and then it's distributed as part of the iOS app.

I can use XCode to generate an .xcdatamodeld file describing what I want the database to look like. I can use momc to compile that into a .momd file. I can include the .mom file from within that into my Mono project, and load it into a NSManagedObjectModel, from which I can access all the properties of the various entities.

What I haven't yet figured out how to do is create an object of a class from the database, rather than accessing the properties of the table. Any suggestions?


To clarify: I want to be able to create a table/class in XCode, call it Person. I give it two fields: Name and Phone. I want to be able to run code similar to this in Mono:

using (var context = new NSManagedObjectContext())
{
  var me = context.Person.GetByID(1);
  me.Name = "Bobson";
  context.Save();
}

Obviously, the specifics of getting it from the database and saving it back will be different, but the gist is there.


Solution

  • As per @t9mike's comment (which I will accept if he ever makes it an answer), I gave up on using CoreData in favor of a more cross-platform approach. I ended up using Vici CoolStorage for the ORM, although I had to embed the source in my project in order to get it working.