Breeze NotMapped (Database) properties - not working - entity-framework

This is actually reposting a question that already exists but I don't think it was properly understood and for us is really important to know if is possible or if it will be:
https://stackoverflow.com/questions/16079703/how-would-one-go-with-saving-a-complex-object-graph-as-xml-in-sql-database-whil
So, what we would like to know is how do we get to transfer from Breeze to server NON MAPPED TO DB entities/properties. For example, let's consider XML (I would't want to generate xml in JS, but I do have XML db columns that need to be populated from complex forms - so we will collect the data in Breeze/KO, will transfer it to server and on server will process and generate the XML, from the NON MAPPED Entities/Properties).
P.S.
I see there is already a NODB approach (http://www.breezejs.com/samples/nodb), so would be really nice if we would be able to make the 2 approaches work together (EF + NODB)

As of Breeze v 1.3.6, there is now an EntityInfo.UnmappedValuesMap property available during the save that exposes all of the unmapped properties on any entity being saved.

Providing I'm understanding your question correctly, any properties declared as 'unmapped' on a breeze entity do get transferred to the server on a save for exactly this purpose. You can intercept and work with this data within the server side BeforeSaveEntity and BeforeSaveEntities methods.
There is more info here regarding "unmapped" properties:
http://www.breezejs.com/documentation/extending-entities

Related

Push Client-Metadata to Server

we have a very dynamic metamodel which changes frequently. We don't use EF but an own Database-Technology.
As we didn't find any good documentation on how the server-side metamodel has to be created "by hand", we decided to create the client-side metamodel and import this one into breeze when loading the application.
Now we have the problem, that eventhough we tell breeze to get all Person-Objects and take 3, the server expands all associations and returns more than 3 results. Breeze then cuts the result on the client to 3 and fills the properties.
Now I'm wondering, if we just didn't understand some of the concepts. All we want to do is to tell breeze dynamically how our metamodel looks like without using EF or NHybernate. Is there any documentation on that?
The only documentation on a Schema I found was this one:
http://www.breezejs.com/documentation/metadata-schema
But it only explains the client-Side-Metamodel but not the Schema that our Server-Side would have to generate. As far as I see it, the Server-Side-Metamodel gets translated into the client-Side-Metamodel anyways.
Would be nice if someone could clarify or provide a link with all the "basic information" about the topic.
The client metadata is for the Breeze client. The server shouldn't need it, because it already knows what the domain model looks like. The content of server-side metadata typically comes from the database and/or the mapping layer, when using an ORM.
Breeze's server-side WebApi filters attempt to apply the OData query parameters to the IQueryable that comes from the data provider (EF, NH, or whatever). If the data provider's LINQ implementation is incomplete, that could cause the problem you mentioned, in which take didn't work.
Unfortunately, the metadata-schema document is out-of-date. The structure of the Breeze metadata JSON format has changed since then. You are better off looking at an example of current metadata, and following the Metadata by Hand guidelines.

Hide Columns in Breeze data returns

In our entity framework model that identify the customer, a simple Customer_GUID. We are using breeze with asp.net mvc and doing IQueryable.
Is there a way to globally not return those columns in the JSON? This would reduce a good bit of data coming across the wire. We don't want to remove it from mapping in our EF model because we still use it when we save.
You might want to look at the Json.NET documentation in particular the [JsonIgnore] attribute. Look at "Conditional Property Serialization" for more sophisticated scenarios.
Do be careful about insert and update data coming from the client. You'll have to do something if your client uploads a new entity for insertion and it lacks the properties you require on the server side.
To be clear, your configuration of Json.NET has no affect on your server-side EF model ... exactly what you wanted.
That also means that metadata generated from your EF model will describe properties the client can't see. You'll want to compensate for that I imagine. Such compensation is beyond the scope of this question; look to the Breeze documentation on metadata ... particularly "Metadata by hand" and "EF as a design tool".

Saving a doctrine2 entity to cache to speed up the page load

Let's say I have an entity called Product and this entity is loaded every time user hits the product information page. Usually I'd save the object in Zend_Cache (memcache) for an hour to avoid hitting the db for each request but as far as I understand that's not possible with Doctrine2 entities because of the Proxy objects.
So my question is, how can I avoid loading the same entity from the database for each request?
[EDIT]
I tried using Doctrine Cache like this
$categoryService = App_Service_Container::getService('\App\Service\Category');
$cache = $categoryService->getEm()->getConfiguration()->getResultCacheImpl();
$apple = $cache->fetch('apple');
But I get the following error
Warning: require(App/Entity/Proxy/_CG_/App/Entity/Category.php)
[function.require]: failed to open stream: No such file or directory
in /opt/vhosts/app/price/library/Doctrine/Common/ClassLoader.php on
line 163
This is same for Zend Cache as well as you can't serialize the entity because of the Proxy class
You've got several options:
Use Doctrine's built-in result caching
Try just sticking entity in memcache via Zend_Cache. When you pull it out, you may need to merge() the Product back into the EM so proxies can be dereferenced. If you fetch-join any associations you need to display the product info, and you're only doing reads, this shoudl work fine.
Don't cache the entity at all. Cache whatever output you generate instead.
EDIT: If you don't care about the hydration overhead, you're using mysql, and your Products and associated tables don't change very often, you might prefer to just rely on the mySQL query cache. It's a fairly blunt object, but useful enough to mention.
You might want to try implementing __sleep or __wakeup methods for your entity class, as Doctrine 2 has special requirements and limitations concerning serialization/deserialization of entities (which is what happens when storing them in Zend_Cache).
There is this guidance.
General information about limitations including serialization.
I find this extremely strange since i just messed around with this myself and didn't have any issues with the proxy object being stored in the database. So im guessing your configuration is not setup 100% ?
If you find the issue with your configuration then be very aware of what timdev said you MUST merge the object back into the EntityManager else you will have weird bugs down the line.
A fourth solution available for you is also to retrieve the data as an array instead of an object, but then of course you lose all the functionality connected to your module which might not be exactly want you wanted.
It seems to me more like a configuration error. Either Proxies have not been generated or there is something wrong with the proxy directory and namespace.
Depending on your configuration, proxies can be either generated automatically or manually. Does your proxies have been indeed generated under App/Entity/Proxy ? Is this indeed the right directory?
FYI proxies can be manually generated by executing doctrine orm:generate-proxies <dest-dir>
Seconding what timdev says: Doctrine has built-in caching, you want to use it.
I also wonder from your question if you are experiencing any performance issues or if you are a victim of overly eager optimisation.

OData / WCF Data Services / EDM - Mapping to disparate data

I'm researching OData as a RESTful interface for a database. The data is structured in a very unusual way and normal tables and rows do not apply, in fact, some stuff just exists in in-memory collections and objects.
Can I build my own arbitrary mapping system between the entities that make up 'feeds' and the sources behind, this might mean aggregating from sources and building the entities on the fly?
I'm just looking for yes/no (why not) and maybe some pointers to relevant reading material.
Many thanks
Luke
Yes and no.
You can build an OData feed of anything. In a WCF Data Service implementation of same, you can implement IDataServiceMetadataProvider.
However, the easiest way to define an EF data service is:
public class MyOData : DataService<MyObjectContext>
...and that won't work if you need to return non-entity objects. Such services are limited to entities and simple types only.
So yes, you can do it, but it's quite a bit more work than the one-liner above!

Is there any easy way to generate the Entity Framework Code-First classes?

The method - Entity Framework Code-First - looks good. But its very difficult to create all the classes for a large database.
Is there any easy way to generate the Entity Framework Code-First classes?
You can use the recently released Entity Framework Power Tools CTP1. The tool gives you the ability to reverse engineer code first, meaning the Database will be mapped to Code.
Note that all tables in your large database will be mapped. There currently is no way to choose which tables will be mapped to code. Reading through the comments, this feature will most likely be implemented in a future release.
The point of EF Code-First is that you define your domain model in code, then your user-interface and database can be easily generated from that domain model. This has a number of advantages including reducing the amount of tedious code which needs to be written, and helping to ensure your database, your domain model, and your UI match each other.
However, at some point you are going to have to write your domain model - there's no way that can be "generated" (by which I assume you mean computer-generated) as it is personal to your application.
If I've misunderstood your question, please leave a comment and I'll update my answer.
If you want to use the code-first model, but already have an existing database, you can use the Entity Framework Power Tools to generate classes.
If you're reading this after May/2012, the above tool may be out of beta!
No there is no way to generate classes for you if you are using code-first. Code first means that there is no model and no database so you can't generate classes unless you have some upfront design in any case system (UML) which will autogenerate code for you. Simply generating classes without any input about how they should look like sounds like AI from Sci-fi, doesn't it?
If you already have databse you are not using code first but database first. In such case you can have your classes generated.
Check out the link below. It's a program that will generate POCO classes from your databases. I think that's what you're looking for.
http://msormcodegen.codeplex.com/
Generate the code from the database first using database first generation and then modify the resulting code to start your code first version