Adding methods to Class when using Model-first in EntityFramework 5 - entity-framework-5

In EF4, I would generate my model from the DB. Then I would extend the classes using partials. This allowed me to regenerate from the DB without losing my code.
In EF5, when generating from the model, it creates a .cs file for every item in DB. I actually like this a lot more except, I am not sure what the best way to extend the objects is. If I write my changes right in the MyObj.cs file, I will lose them if I have to re-generate the model. I guess I chould create file MyObjPartial.cs and make the class partial there... thought?
~S

You should follow the same pattern as before, by using a partial class. This is basically the same with all auto-generated file types and EF is not any different. You want to make sure you don't lose the changes when you regenerate the file.
Partial classes are also great in this example as a separation concept, that way you have the stuff that's important to your application separate to the stuff that's important for the running of Entity Framework

Related

Why does Jooq Code Generator makes un-necessary changes to all files?

I'm using Jooq Code Generator and I noticed that when I make a change in the DB and run the code generator, it makes changes to all files, not just to those that should have been changed.
For example, I add a new table, so I see the new class for the table, and the new class for the table record, and I see that the table was added to the schema class. But in classes that represent other tables, that shouldn't have been affected at all, I also see changes, in the "imports" section.
In some files I see that just an empty line was added. In others I see that the location of specific import lines was changed. And in other classes, a line of import that contains * is replaced by the entire list of imported classes, or vice-versa.
Example:
My questions are: Why does Jooq does that?? And can I turn this behavior off?
Thanks!
The jOOQ code generator doesn't produce that particular import you've shown, i.e. import org.jooq.*;. You must have done that yourself, either manually, or via some post processing on generated code.
If you did this manually, then this is clearly against the idea of what jOOQ generated code should be, namely derived code, which shouldn't be modified manually. It wouldn't be possible for the code generator to detect which parts you modified (and intend to keep) and which parts you didn't modify (and are thus allowed to be overwritten). Just don't do that.
If you did this via automated post processing, then jOOQ's code generator still cannot detect this, but after overwriting everything, you would just have to re-apply this post processing, and the end result should be unmodified code.

jhipster import-jdl your-jdl-file.jh

After doing "jhipster import-jdl your-jdl-file.jh" to generate entities, if I found something missing (ie. relationship, entity, or field within an entity), can I redo the jh file and rerun the command again to make the changes? Appreciate the help!
Yes, you can. But you must be carful to:
Your code is in source control
No file is dirty, I mean all are committed
Regenerate and overwrite all files
Review changes carefully file by file to bring back your overwritten
changes.
In case of any problem, it is safe to just remove all local changes.
Another approach that I prefer, is to use the command line to do entity changes commands to affect changed entities only. Use
jhipster entity entity-name
It will overwrite affected entity files only, but be carful that, these changes will be outside your JDL file.
You still must be carful using source control as described above.
It is indeed possible but this might erase some of the custom modifications to the model you've made as it is regenerating them.
That being said, I strongly advise you take a look at the liquibase capabilities offered by Jhipster (For SQL databases). With that, you just have to update your model classes using the right annotations and that will update your database model accordindly (After a few other steps). That can be used for small adjustments when you've already been working on your app, rather that re-generating everyhting, which would seem a bit overkill to me.
If you want the full procedure, take a look at the (well detailed) documentation : https://jhipster.github.io/development/ and go down to Using MySQL, MariaDB or PostgreSQL in development.
Hope this helps !

Customizing a jdbcChannelMessageStore in Spring Integration

I'm relatively new with SI (I say "relatively" because I did some work with SI version 0.6 to 1, but I had to stop then and I'm now on it again in 4.2.5) and for now I'm writing some prototypes for POCs. In one of then I configured a channel backed by a jdbcChannelMessageStore which I wanted to customize in a simple way. To change the column MESSAGE_BYTES from bytea to text.
So I changed the schema-postgresql.sql to include that change and hope that I could only rewrite the jdbc statement for the INSERT. However, even if the statement itself is easily changeable, setting the parameters is not, since it is buried inside a lambda inside the jdbcTemplate.update itself inside the addMessageToGroup method. So the only solution would be to override the entire addMessageToGroup method, which seems not a good solution at all, since it contains more logic than the simple jdbc insert.
So what ended up doing was what I commented on my code as // very big hack. I overriden the DefaultLobHandler to actually not use the lob at all but a setString(...) instead.
So, I have a question and a suggestion:
Is there a way customize the JdbcChannelMessageStore to have our own schema structure and/or our own statements, without using things like this "big hack"?
If there is no better way, can I suggest to at least put the prepared statement fields setters on it's own protected (or public) method, instead of a lambda inside the jdbcUpdate?
Thanks in advance.
We should probably make it easier to override that logic, perhaps by delegating to an overridable method.
Contributions are always welcome :).

Simple way to extend the default ApplicationDbContext in asp mvc5 using Ef6 Code First

When i run the entity framework Reverse Engineer Code First in an asp mvc project, it runs fine and generates all the mapping and poco classes, but i now have two context and conflicting classes in both the ApplicationDbContext and the new Auto Generated DbContext.
When generating EF Classes using Code First from Database, often there are more steps as the generated code is not always exactly as you want. I typically rename all files to "EntityNameDAO" I then right click refactor all classes to "EntityNameDAO" to match the file name appropriately.
After this, you will typically find that you have additional or less needs or perhaps even circular dependencies in the json result from these generated classes. To deal with this, I create specific domain objects for each objective EntityName_SpecificUseCase.
You will notice I use EntityName"AdditionalData" so that the alphabetic sorting keeps each of my entities next to eachother and I can verify I have the right flavors for each entity quickly and easily.
To deal with the circular json, you will need to add this code to your appconfig.cs:
var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling =
Newtonsoft.Json.PreserveReferencesHandling.All;

Should Change Tracking Proxies work if I use a Database First model?

Should Change Tracking Proxies work if I use a Database First model?
After creating the model with the database first designer, though navigational properties are marked virtual, other properties are not.
If I edit the classes so that the properties are virtual (and of course public, not sealed, use ICollection where needed and remove initializing of navigators from the classes constructor), this will get overwritten if I ever update the model from the database while within the designer.
And, if I make all (what I believe are) the necessary changes to allow for Change Tracking Proxies, when I test with "x is IEntityWithChangeTracker" it still returns false.
So, either I'm really doing something wrong or I'm doing something that wasn't meant to be. I hope it's the former.
EF6.x uses System.Data.Entity.Core.Objects.DataClasses for its IEntityWithChangeTracker. If you use this version of IEntityWithChangeTracker, it will work. Using System.Data.Objects.DataClasses.IEntityWithChangeTracker was my problem here.

Resources