通常,我们都会尽量使数据模型的变化尽量简单。但有些情况下,不得不进行大的改动,甚至是重新设计数据模型。在这种情况下,之前提过的简单数据迁移已经无法适应了,需要引入Mapping Model这个中间层。
这时,又想起之前提过的一句话:
There is no problem in computer science that can’t be solved by adding another level of indirection.
这里做一个简单的变动,先为球员增加薪水属性:
然后创建一名球员,信息如下:
这时候我们打算为球员调薪,比如上涨10%。为了结合NSMappingModel,这里简单地增加了一个新的属性newSalary,并且希望在数据迁移时更新该属性。为此,我们创建了一个NSMappingModel映射模型:
选择好源数据模型和目标数据模型,设置newSalary和salary的关系:
这表示目标属性newSalary的值为源属性salary的1.1倍。
这个时候,我们不希望Core Data自动为我们映射模型,所以修改一下迁移选项:
- NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys:
- [NSNumber numberWithBool:YES], NSMigratePersistentStoresAutomaticallyOption,
- [NSNumber numberWithBool:NO], NSInferMappingModelAutomaticallyOption, nil];
- NSString *mappingModelPath = [[NSBundle mainBundle] pathForResource:@"mappingModel3to4" ofType:@"cdm"];
- NSURL *mappingModelUrl = [NSURL fileURLWithPath:mappingModelPath];
- NSMappingModel *mappingModel = [[[NSMappingModel alloc] initWithContentsOfURL:mappingModelUrl] autorelease];
- NSMigrationManager *migrationManager = [[[NSMigrationManager alloc] initWithSourceModel:sourceModel destinationModel:destinationModel] autorelease];
- if (![migrationManager migrateStoreFromURL:storeURL type:NSSQLiteStoreType options:nil withMappingModel:mappingModel toDestinationURL:tmpStoreURL destinationType:NSSQLiteStoreType destinationOptions:nil error:&error]) {
- NSLog(@"Error migrating %@, %@", error, [error userInfo]);
- abort();
- }
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSString *oldStoreName = @"cdNBA_old.sqlite";
- NSURL *oldStoreURL = [NSURL fileURLWithPath:[[self applicationDocumentsDirectory] stringByAppendingPathComponent:oldStoreName]];
- [fileManager moveItemAtURL:storeURL toURL:oldStoreURL error:&error];
- [fileManager moveItemAtURL:tmpStoreURL toURL:storeURL error:&error];
可以发现有一份旧的sqlite文件和一份新的。
通过查看新的sqlite文件中的数据,可以得知newSalary的值:
其中,newSalary为2420000.0,刚好是salary的值2200000.0的1.1倍。
相关推荐
Model, map, and access data effectively with Entity Framework Core 2, the latest evolution of Microsoft’s object-relational mapping framework that allows developers to access data using .NET objects,...
15 Adding the Core Data Framework...............................................................................................15 Creating the Data Model ...............................................
Mapping the Security Development Lifecycle to the Software Development Lifecycle Software Development Methodologies Waterfall Development Agile Development Chapter Summary References Security ...
Scenes, data sources, and visualization modules: the pipeline model Loading data into Mayavi Installation Installing ready-made distributions Requirements for manual installs Doing it yourself: ...
The Data Model 5 Different Physical Tables 6 Integration and Transformation ProcessingGranular Data 8 Historical Data 9 Timestamping 10 Data Relationships 10 Generic Data versus Specific Data 11 Data ...
EF是是微软以 ADO.NET 为基础所发展出来的对象关系对应 (O/R Mapping) 解决方案,现在已开源,是.NET web开发首选ORM框架,其中的Link to Entity和Code First模式用起来非常过瘾,推荐学习使用! EF最新版本6.10,...
Working with object-oriented software and a relational database can be cumbersome and time consuming in today's ...representation from an object model to a relational data model with a SQL-based schema.
Table of Contents * Here We Go Round Again * Core Data The What, Why & How * A Super Start: Adding, Displaying, and Deleting Data * The Devil in the Detail View * Preparing for Change: Migrations and...
Beginners will learn several techniques for each of the 4 core NHibernate tasks – mapping, configuration, session & transaction management, and querying – and which techniques fit best with various...
PRODUCT_MODEL := QUAD-CORE A33 y3 PRODUCT_MANUFACTURER := softwinner #include device/softwinner/polaris-common/prebuild/google/products/gms_base.mk 4、 R:\wyb\ap6212a0_a33_sc3817r\android\device\...
Entity Data Model (EDM) 为主,将数据逻辑层切分为三块,分别为 Conceptual Schema, Mapping Schema 与 Storage Schema 三层,其上还有 Entity Client,Object Context 以及 LINQ 可以使用。
At the beginning, learn the core concepts of Sencha Ext JS, components, data models, and mapping. This book also teaches you about event-driven development, forms and grids, charts and themes, and ...
return (List) super.getData(params, env); } } @Override protected boolean isPage() { return false; } } Content_list.html中的内容 [#list tag_list as a] <li><a href="${a.url}" target="_blank...
1.6 Eye-View Pixel Anti-Aliasing for Irregular Shadow Mapping 74 1.7 Overlapped Execution on Programmable Graphics Hardware 90 1.8 Techniques for Effective Vertex and Fragment Shading on the SPUs ...
本书为《Game Programming Gems》系列的第八卷。在这一版当中,我们将广泛地探讨重要的实时图形方面,如Larrabee, PlayStation 3, 以及DirectX 11计算着色器。此外在本书中, 处在第一线的业内顶级开发商如:...
5.2.3. Passing model data to the view 5.3. Accepting request input 5.3.1. Taking query parameters 5.3.2. Taking input via path parameters 5.4. Processing forms 5.4.1. Writing a form-handling ...
Data Instead Of Unicode Vs. 8-bit Overview Of Syntax Changes New Syntax Changed Syntax Removed Syntax Changes Already Present In Python 2.6 Library Changes PEP 3101: A New Approach To String ...