Map DB tables to objects in Swift - ios

So the problems goes as follows. I have a working database with all relational tables already created, and I need to do a object-relational mapping of that database into objects in the code, in such manner that I am afterwards able to do something like this:
User user = new User(data);
db.save(user);
In other words, I need a mapping of DB tables into code objects such that these objects in code can be interacted with the database - from the code.
What would be the best way to do so?

Related

Entity framework multiple DbContext in single execution

I have one master & detail in my 'db1' and there is one column named 'EntryByUserId' in master table.
User table is available in 'db2'.
When all the tables are available in one single database we can directly get user detail by using include function. But here my reference table is in another database so in my case user object will return null value. So anyone please help me to achieve this.
I have created multiple dbcontext in my project but don't know how to get this.
Below is the code we use when all tables are available in single database.
dbcontext1.tbl_Master.Include(m => m.tbl_Detail).Include(m => m.tbl_user)
.AsNoTracking().FirstOrDefault();
One option to accommodate this cleanly, especially for something as frequently accessed as a "User" reference for something like reporting on CreatedBy or ModifiedBy tracking on rows would be to implement a view within Db2 that lists the users from Db1. Then in your main application context you can map a User entity to the view rather than a table. I would put guards in your DbContext and entities to discourage/prevent modifications to this User entity, and leave maintenance of users to a DbContext overseeing the Db1 tables.
If this is something like a multi-tenant system with a system database for authentication and separate DBs per tenant which are tracking things like CreatedBy against records, I would recommend considering a system to inspect and replicate users between the auth database and the respective tenant databases. The reason for this would be to help enforce referential integrity for the data and the user references. The issue with the view approach is that there is no constraint available to ensure that a UserId reference actually corresponds with a row in the Users table over in the other database. It can be indexed, but not constrained so you have to handle the possibility of invalid data.

Core Data: Which record loads by default in core data if you do not specify which one?

I have a static table for settings where I want to pull some stuff from an entity in Core Data. The use case does not lend itself to a table of records as you usually see. Rather each row of the static table is really a field related to the user--as in a user profile. I have a feeling that in testing I may have created more than one record in the entity. I know there are programs that let you see the SQL lite database underneath, but my question assumes you do not have this tool and are relying just on Xcode.
My question is when you have more than one record in a Core Data entity/table, and you try to load data from the managed object context into a VC, one field into one element, what record is shown by default?
Related to this, if you don't know how many managed object or rows are in the database, is there anyway to specify which record you want since there are no auto ids as you would use in a traditional database?
The record that gets loaded from the fetch first. Depending on your sort that might be consistent or it might be random.

Can we map database view in Entity Framework?

I have 5 tables to use in one mvc view. So I created a database view and want to map this view into EF for using it as a model. How can I do?
I have successfully used the following in a code-first application where I used Sql Server Functions as 'views' to run complex queries against the database to fetch data (I did not need inserts or updates).
List<MyClass> myClassItems =
dbContext.Database.SqlQuery<MyClass>("Select * from dbo.MyClassFunction(#param)", param).ToList();
I returned myClassItems as the result of a Repository function, with MyClass a class with properties that match the columns that the MyClassFunction returned.

Can Entity Framework be used to partially map an existing database?

Just getting started with EF v6 and trying to see if I can retrofit it to an existing database. In some ways it looks like the existing database will map well to an object model (HealthProviders, Patients, Visits) however in other ways some of the tables won't map easily to objects. That being said does EF require mapping all database objects to code or can you pick and choose which database objects are modeled in EF?
Yes, you can. Using database first - the default - you get to choose which objects - tables, views, stored procedures - you want to map.

EF Code First - Entity joining multiple tables

I currently have an EF class that is backed by a database view (joining multiple tables). To make it updatable, I need to change the class to be backed by a database table. I am using Entity Framework 4.1 Code First and can't figure out how to set up those relationships?
To simplify, I currently have a Categories class that returns the Category Name (Categories table) and Category Type Name (CategoryTypes table). These are both in the database view that I currently use. I want to change to a ViewModel that brings back both of these fields directly from their tables and joins properly, that way when a user updates a Category Name, EF should be able to properly handle the update (since it will be updating the table instead of the view). Any tips on how to do this?
Table is a table - it is a single database object. If you want to remove your view and replace it with a table you need to delete your current tables (Categories and CategoryTypes) and create a single table which will contain denormalized data. That is pretty bad solution and it will cause you problems in the whole application.
Just to simplify description: It is not possible to replace your view constructed by joins among several tables with a table and it is not possible to make your view updatable.
You are doing it wrong because you are obviously mapping view models directly to your database. Map Catagories and CategoryTypes to entities load Category with its CategoryType and flatten them to your view model in your application logic (or load the view model through projection). Once user updates your view model decompose it back to separate entities and save them.

Resources