I´m working on a project using ASPNET Boilerplate where some entities must be versioned, so they have an Id and a versionNumber, where the same entity can have several versions like:
Documents:[
{Document:{id:1, version:1}},
{Docuemnt:{id:1, version:2}}]
So my question is if there is an easy way of doing this, like the implementation of Soft Delete, where I can intercept the update method so it creates a new version.
You can override ApplyAbpConcepts in your DbContext to create a new entity when your Document entity is modified, then reload the original entity so that its changes are not saved.
CancelDeletionForSoftDelete does something similar.
But just doing this won't work because primary key is unique. You can create a composite key.
You will also have to handle relationships (i.e. foreign keys) to avoid linking to multiple versions.
Related
I have an Jhipster app with an existing entity Company.
Now I want to add an Entity Vehicle and created an "vehicle.jh" file for the generator, like this:
entity Vehicle {
name String
category String
// ...
}
relationship OneToMany {
Company{vehicles} to Vehicle{owner}
}
On the first try, the generator complained it does not know about "Company".
I then tried to add it an empty declaration, like this
entity Company
But this overwrites the existing Company-entity classes and removes the existing fields in them.
Is it possible to generate a new entity and connects it with the existing entities, if yes, how? Or should I redoing the steps done the generator by hand, or (or use git to merge them somehow with the original source files)
JHipster requires that all your entities are defined in one single JDL file. If you haven't saved the definition of your previous entities you can export them using jhipster export-jdl app.jh then edit app.jh to add your new entity and re-import using jhipster import-jdl app.jh
We have an Spring 3.2, Hibernate 4.2 application.
Our application has an upload module, where you can basically upload all kinds of files. A file upload generates also a database entry:
id, directory, filename, mimeType, userId
So basically it should be possible to upload a file from everywhere in the application. And we don't want a new UploadEntity for every possible Entity. So we thought about using some kind of generic table for uploads:
id, directory, filename, mimeType, userId, FOREIGN_KEY
The problem of course is, that we can't set a concrete data type for this foreign key in JPA, because it can point to Entity A or B or C or ... We use UUID as keys in our application, so we thought about two solutions:
Make the foreignKey simply of type UUID and always save the foreign entity's id.
Make the foreignKey of type Object with a #ManyToOne annotation, but of course we can't provide a target entity for that.
But maybe there's some other much better/easier solution for this. What do you think?
I forgot to tell you, that each entity we use implements Persistable<UUID>, so is it possible to use this interface as type for the foreign key?
Btw: We need never to use the reference from UploadedItem -> SomeEntity. We only need the other way: SomeEntity -> UploadedItem.
I'm having a few tables on SQL Server, which have similar structure - int Id and string Value.
This tables linked to main table via foreign key, so I'm wrote a bit of logic for mapping a string values to id's in models in MVC Razor. This feature requires that models used as dictionary implement simple IKeyValue interface with Id and Value, but after updating model from database I can loose interface implementation from models and must write it again.
Any way to automate this?
Are you modifying the auto-generated file? If so, you should not do this, for the exact reason you describe in your question -- it will get overwritten.
All of the classes in the generated file should be partial. You can take advantage of this by creating another class (in a different file, but in the same project), make sure it has the same declaration (and namespace), and have it implement the interface. This way the class will implement the interface, but will not be overwritten the next time you refresh the schema from the database.
I have three views that I've manually created in the DB.
First view is "Region", the second is "FIPS" and the last is a many-to-many between them called "Region2FIPS". These are all views, and I only need read access the data, so I'm not worried about having updateable views.
I have added each of these views to Entity Framework, and created the appropriate associations between them.
Region to Region2FIPS is a 1 to many.
FIPS to Region2FIPS is a 1 to many.
The "Region2FIPS" view contains only two columns, one called "FIPSID" the other "RegionID". These column are associated with their respective views in the relationships I defined above.
When this type of association is made on tables in the DB, Entity Framework knows that it is a many-to-many relationship and it creates a navigation property on "Region" called "FIPS" that I can use to navigate through the child collection of FIPS. It does likewise for "FIPS" to "Region".
However, when done manually, with views, it does not exhibit that behavior. Instead, my "Region" object has a collection of "Region2FIPS" objects, which each have a navigation property called "FIPS" which is of type "FIPS". And my "FIPS" object has a collection of "Region2FIPS" objects, which each have a navigation property called "Regions" of type "Region".
I assume this has something to do with the fact that I can't create foreign key references on the views, so entity framework doesn't realize the many-to-many relationship. But I thought that if I manually created the many-to-many relationship between the views it would recognize it and properly handle the navigation between the types. Is there a way for me to force it to do this?
It's possible, but the designer doesn't really help you here. You have to do the mapping manually.
One fairly easy way is to use Code First mapping. But this means your model has to be Code First to begin with. If you're writing a new model, just do that.
If you're using DB First mapping, however, you will have to do the mapping manually. Your SSDL will probably already be correct, once you define the "primary keys" of the views. You would then have to remove the "Region2FIPS" objects from the CSDL (not just from the designer!) and manually patch up the MSL.
Perhaps the easiest way to do this would be to use the designer to automatically map real DB tables (not views) with a similar schema and then replace the table names with view names in the EDMX, using the XML editor.
I've made a custom entity that will work as an data modification audit (any entity modified will trigger creating an instance of this entity). So far I have the plugin working fine (tracking old and new versions of properties changed).
I'd like to also keep track of what entity this is related to. At first I added a N:1 from DataHistory to Task (eg.) and I can indeed link back to the original task (via a "new_tasksid" attribute I added to DataHistory).
The problem is every entity I want to log will need a separate attribute id (and an additional entry in the form!)
Looking at how phone, task, etc utilize a "regardingobjectid", this is what I should do. Unfortunately, when I try to add a "dataobjectid" and map it to eg Task and PhoneCall, it complains (on the second save), that the reference needs to be unique. How does the CRM get around this and can I emulate it?
You could create your generic "dataobjectid" field, but make it a text field and store the guid of the object there. You would lose the native grids for looking at the audit records, and you wouldn't be able to join these entities through advanced find, fetch or query expressions, but if that's not important, then you can whip up an ASPX page that displays the audit logs for that record in whatever format you choose and avoid making new relationships for every entity you want to audit.
CRM has a special lookup type that can lookup to many entity types. That functionality isn't available to us customizers, unfortunately. Your best bet is to add each relationship that could be regarding and hide the lookups that aren't in use for this particular entity.