Alter entity to generate ids for legacy table - jpa

I have a legacy (old) table with data in a database. Currently, data is put along with ids, so ids are not generated. I'm going to change a source of data. Data from a new source have other ids. These could clash with ids from the legacy table. So I don't want to use them. I'm going to map this data without ids and generate ids with jpa. Will it work if I change my entity for auto-generation and the id column to auto increment? Which options do I have otherwise?

Related

Entity Framework ValueGeneratedOnAdd with no requery

Say I have EntityFramework Core 2.1.14.
Say I have to integrate data into a legacy MySql database which was created by a self-taught.
Say also that one of the tables in this database has a surrogate key field that is generated on Add.
Say then that I want to use context.SaveChanges() to handle the Insert DML generation for me because I want to be lazy and because these tables are messy.
Say finally that I do not want Entity Framework to perform a follow-up query to retrieve this surrogate field; I don't care what it is. I just need it generated on insert.
How would one call context.SaveChanges() with an Added object having a property configured as .ValueGeneratedOnAdd() but also instruct Entity Framework to do nothing but generate my INSERT ? I don't want it to return the id.

How to correctly add a list of objects to database, working with multiple tables?

My database looks like this: Database Diagram MSSMS
So everytime i'm adding a fish to the database, i want to add the fishes's continent, waterlagen and verbanden.
Tables Continenten, Waterlagen & Verbanden are already filled with objects from an array after creating the database.
Im using List Continenten; for example to store multiple continents.
So i tryed:
Vis nieuweVis = new Fish();
nieuweVis.Naam = "Molly";
foreach(Continent c in Continenten)
nieuweVis.Continenten.Add(c)
so in the table VisContinenten
i assume that EF will automaticly fill in the FishId and ContinentId wich are foreignkeys.
I want records in that table also to be unique so i add a unique key to both the columns in VisContinenten so that fish 1 on continent 1 won't appear twice in that table.
Error i get:
An error occurred while saving entities that do not expose foreign key
properties for their relationships
Additional information: Unable to update the EntitySet 'VisContinenten' because it has a DefiningQuery and no element exists in the element to support the current operation.
Help me pls :)
Thank you

Entity Framework: auto-generate primary keys after initializing tables

I'm using Entity Framework (6) in a code-first arrangement to create a database and pre-populate it from a set of CSV files. Because the tables are coming from another application, they already have primary keys, and relationships embedded in the data make reference to those keys.
For that reason I've been using [DatabaseGenerated(DatabaseGeneratedOption.None)] when defining these tables, so that the PKs already in place get used.
However, after the data have been imported, I want to be able to add new records to the tables. How do I get the DB to assign new sequential PKs for the new records?

How can I replicate core data model using a traditional relational database?

I have my app using core data with the data model below. However, I'm switching to a standard database with columns and rows. Can anyone help me with setting up this new database schema?
First of all you need to create tables for each of the Entities and their attributes (note I added "id" to each of the tables for relationships):
Routine (name, timestamp, id)
Exercise - this looks like a duplicate to me, so leaving one only here (muscleGroup, musclePicture, name, timeStamp, id)
Session (timeStamp, id)
Set (reps, timeStamp, unit, weight, id)
Now that you have tables that describe each of the entities, you need to create tables that will describe the relationships between these entities - as before table names are in capitals and their fields are in parenthesis:
RoutineExercises (routine_id, exercise_id)
SessionExercises (session_id, exercise_id)
ExerciseSets (exercise_id, set_id)
That's it! Now if you need to add an exercise to a routine, you simply:
Add an entry into Exercise table
Establish the relationship by adding a tuple into RoutineExercises table where routine_id is your routine ID and exercise_id is the ID of the newly created entry in the Exercise table
This will hold true for all the rest of the relationships.
NOTE: Your core data model has one-to-many and many-to-many relationships. If you want to specifically enforce that a relationship is one-to-many (e.g. Exercise can only have 1 routine), then you will need to make "exercise_id" as the index for the RoutineExercises table. If you want a many-to-many relationships to be allowed (i.e. each exercise is allowed to have multiple routines), then set the tuple of (routine_id, exercise_id) as the index.

Adding a property to an Entity Framework Entity from another table

I'm just starting out with the Entity Framework and ADO.NET Data Services and I've run into an issue that I can't seem to figure out. I have two tables, one that has user information and the other that has a created by field. Within the database, there isn't a foreign key between these tables. The user table contains an arbitrary Id, a username, and a display name. The created by field contains the user's username. In my entity I would like to have the user's display name since this is what I need to display and expose over the ADO.NET Data Service? I'm aware that I could restructure the database, but I was hoping that I could do the join using the username as I would in a SQL statement.
Thanks in advance,
-Damien
You can make a view using a join of both tables, and then use this object to display the user's name.
There's some info on mapping custom queries here.