EF Code First - Duplicate Table With The Same Schema - c#

EF 6 Code First: How is it possible to have duplicate table of the same schema.
Like History table that serves as a replica for the original table. For example:
Several Important Notes to Consider:
No foreign key: I know it can be done by inheritance and some other such methods which generate foreign key to the original table. But I mean completely different duplicated table.
No Copy Past the Entity: Coping from the Employee entity to Employee_History entity is an option but in case the original entity is big, complected and has lots of inheritances, it might be really mess. Not to mention that every change to the original table should be reflected to the history table manually by copy.
DataAnnotation is preferable.

Related

EF Core 6 - Multiple Tables Based Off 1 Entity

I've seen very old answers relating to this (and conflicting answers), but I am wondering if it's possible to use 1 base entity to create multiple tables in EF Core.
My specific scenario is this: I have about 110 tables, and I need to preserve the data after they are "deleted".
However, because there are a number of 1-to-1 relationships, I cannot set a delete flag to mark the table as "deleted". I would like to have an "Archive" table for each entity, and on Delete in my repository I would find the entity from the original table, add it to the archive table, then delete it from the original table.
However it seems like EF Core creates tables based solely on the classes the models are themselves and not by the tables that are defined.
Is there any way to get around this without needing to create a separate "archive" model for my 110 existing model classes?

Using EntityFramework C# code-first from database - how to map table with no primary key

I am building an Ntier application with EntityFramework c#.
I am adding an Entity Data Model in my Data Access Layer with code-first approach from existing database.
Some of the tables of my db weren't included because they don't have primary key. I have seen some ways to work around this problem, modifying EntityFramework's edmx to force the mapping to the database, disguising some field like a key. But I am not using the .edmx, since I can't use automatic migrations with it. I only generate POCOs from my existing database and then go on with code first migrations.
Is there a way to force Entity Framework to generate a POCO for those tables without primary key ? Some only have one entry and really don't need PrimaryKey
In the end, I just wrote my own POCOs for the tables that weren't included.
I used an attribute [KEY] above the property i wanted to act like key. I added DbSet lines in the DataModel and EF did recognize them in my database.
I didn't want to generate primary keys because my boss didn't want, and thats a reason good enough. :) Hope the best for you thx for answer

SqlMetal cannot generate new foreign key associations to certain tables

My team are using SqlMetal to generate database classes from a SqlServer database. It works perfectly for all of our existing classes and foreign key associations but it's refusing to generate the code for a particular new foreign key association that we want to add. This key is from an audit table to a global event table detailing the time the audit record was created and the user it was created by. Many similar foreign key associations between other audit tables and this global "event" table exist in the system and SqlMetal generates code for those associations.
I've tried resolving this problem by:
Dropping and recreating the table
Removing the primary key
Creating a new identical table with a different name
Removing all other fields from the table
Dumping the indexes
Performing a fresh database build
Renaming the foreign key
None of the above seem to resolve the problem. However, SqlMetal does correctly generate code for foreign key associations from this table to some (but not all) other tables in the system. The association between these two tables would only generate when I altered the original create table script to include the foreign key association rather than running it (or a new equivalent table) in later. Unfortunately, we need to be able to deploy this change as a script to our existing production database so this isn't an option. I've seen a couple of articles and forum posts mentioning similar problems but none seem to give any solution or even an explanation.

Entity Framework table Mapped to an Association with No Table

I created a table in SQL that just contained 2 foreign keys, Which Linked a single Bulk upload entry to many titles. After updating my edmx model.
I noticed there is no table in the diagram.
Just an Association mapped between my title table and my bulkupload table.
There is no table in the diagram so I cannot delete the table.
I cannot add things to the table because there is no connection in the C# Code. '
Does anybody have information on why this is happening and how I can fix and add this table so its able to add entries to it?
You can leave it as an association if you'd like. To add things to you junction table you add things to your Icollection so in pseducode:
BulkEntry.Titles.add(TitleToAdd)

Entity framework - remove related records by id

Okay. assume I have structure:
School -> students -> StudentParents <- parents -> address
School can have many students, students can be relatives and have the same set of parents (may-to-many). Each parent can have multiple addresses.
Assume that students who have the same set of parents cannot study in different schools.
If given school_Id =5, I want to remove this school and all related records.
How to do this easily in Entity Framework 4?
Answer for your question would be same as this question.
You are trying to solve the problem in the wrong layer. You need to
reconsider your database design specially how you maintain the
referential integrity.
You need to set the "CASCADE DELETE"s of the foreign keys and reflect
that in your Entity Model. Then the database will make the necessary
changes to maintain the referential integrity when you delete that
entity.
Entity framework cannot delete data from database that is not instantiated as object in memory. This means you would need to load school data, all students data, all students parent data and so on, and then you would need to manually delete all the data.
This seems like a lot of work to do, so you may want to take another approach to this problem - delete all this data using stored procedure on database that is mapped to ObjectContext, this would perform better since you would not need to get all the data into memory.
But this also seems troublesome. The best approach would be to create Cascade delete constrain on database and map it also in entity framework's model. This has two advantages - you would need to only load school data and after it is deleted from model, it would be deleted from database and cascade delete would remove all referencing data. But if you have school and students data already in memory, EF will take care of marking those objects from memory as deleted, which will make your data consistent with database state.
The best resolution to this problem depends on whether you may or may not modify database. If you can - go for cascade delete. If you cannot - I would recommend stored procedure approach as better performing (assuming performance is an issue and there is lots of students, parents etc. in database).

Categories