TPT entities derived from TPH base classes in Entity Framework 6? - sql-server

I have a project that is using EF6 Database first mapped to a SQL database. This is all new so I control the EF model as well as the database schema.
I currently have a table that I'll call Vehicle for simplicity. I use a discriminator column to get subclass Entities Car and Truck. This all works fine.
Now I need to do a 'soft delete' and move any deleted vehicles to a VehicleHistory table. (After trying this w/ EF i will probably use a SQL transaction). This needs to be reviewable so I need this history table mapped as well, but I would like to keep it within the inheritance hierarchy so its easily reused in other classes.
My idea was to create 'vehiclecurrent' and 'vehiclehistory' tables with FK's to Vehicle for shared columns. i would then use TPT in EF to get 'carcurrent','carhistory', ect... derived from my TPH classes(so e.g. carhistory->car->vehicle). This is not working and I get Error 3034: "Entities w/ different keys are mapped to the same row"
So my question is basically how can I pull this off? Will this approach work and how, or is there another way to accomplish this? Thanks!

Related

Asp.Net core Deleting an entry from table in database: what is the outcome

I have a database with table Photos and table Categories. Each photo is related to one category by categoryId field.
What happens when I delete one category from Categories table? Will the photos with that category be updated with a null value in the categoryID? Or how will entity-framework react to this change?
Another question can I then reset with a mass-change the values of those categories in the photos table? And how can I do that?
hi if you have created relationship between table(using foreign key) then only the deletion of parent table will affect deletion of child table. if you just created table separately and managing relationship with your code then it will not affect the child table. if you are creating using model first approach in entity framework with specifying relation then relationship will be automatically created in backend.
their are four options available in sql on deletion of parent entity
1)No Action
2)Cascade
3)SET NULL
4)SET Default
to know how it will affect check this article
https://www.mssqltips.com/sqlservertip/2365/sql-server-foreign-key-update-and-delete-rules/
That would depend completely on how the ORM that is used defines the database model.
Assuming you use Entity Framework then you can define exactly how EF should react to that situation. In the DbContext you should find a OnModelCreating method in wich you can specify per table what restrictions you want on the table. There you can also define the behaviour of the OnDelete of a foreign key.
If you are not using EF but have your own or a different ORM then again, it depends on how that ORM is configured.
Simple check if you dont know about the used ORM is this: Does the field in the database have a foreign key and how is that configured? Also, is the field categoryID (as defined in the database) nullable? if so, then it apparently doesnt need the relation and shouldnt result in related deletes.

Relationships in MDS

I have a question, with regards to how do you create the relationships between members of two different entities?
For example, in case of 1:1 you can create a domain-based attribute that is referencing the entity.
But in case you have a customer with multiple addresses, and you have an address entity.
When you update the member of the Address attribute of the Customer entity, based on the 1:M how would we be able to surface that relationship and connect the Address entity to the Customer entity?
So in case an Address member in the Customer entity changes, how would that update process work since we don't have a mapped relationship?
I know in M:M you would use a bridge table. And in the MDS database which table would contain that relationship info?
Any suggestions to articles and blog posts/Videos or ideas, would be greatly appreciated.
Thanks,
Andrea
In MDS (Master Data Services), you can use the Hierarchy feature.
- Option 1: create Explicit Hierarchy
If you're using SQL Server 2012 (possibly 2014 too), then an Explicit Hierarchy will solve this.
(see: Explicit Hierarchies (Master Data Services) )
Basically, you will be able to use Customer entity rows as parent nodes in the hierarchy (think of a tree structure) and assign one or more Address entity rows as child of each Customer.
As the name suggests, this needs to be "explicitly" managed.. means either someone will do this manually (drag-drop) using the MDS Hierarchy UI page or you can use an SSIS package to automate it while loading the staging table.
(see: Move Explicit Hierarchy Members by Using the Staging Process (Master Data Services) )
NOTE: Explicit Hierarchies have been Deprecated (starting SQL 2016 ...I think)
- Option 2: create Derived Hierarchy (bridge table- M:M)
This is similar to the bridge table concept that has been suggested earlier for M:M relationship, but in the MDS context.
Create another entity, call it CustomerAddressRelationship (let's say), let's call it CAR for now.
Add 2 Domain Attributes to CAR: one referring to the Customer entity, the other to the Address entity.
Now, if you want, you can create a Derived Hierarchy on this CAR entity in the format CAR > Customer > Address.
Read more about Derived Hierarchies: Many-to-Many (M2M) Relationships
- Option 3: create Derived Hierarchy (1:M)
Don't create CAR. Just add a column CustomerID (let's say) to the Address entity. This way every Address may have 0 or 1 Customer related to it.
Create a Derived Hierarchy for 1:M Relationship
Hope this helps.

How to send data from OLE DB source to Anchor model tables using ETL procedure?

I'm currently solving this task: some data should be sent from AdventureWorks2012 to Anchor model tables on the same server in MsSQL.
This is my Anchor Model
At this point I have a pretty simple Integration Services project in Visual Studio and it looks like this.
Control flow:
For example Load_territories is:
The main requirement is to fill all tables of Anchor model tables in MsSQL but I'm constantly facing a problem: the amount of attributes in tables are different and some of them are repeating
At this picture in the second table basically TR_ID,TR_GRP_TR_ID, TR_TID_TR_ID, TR_TNM_TR_ID contain the same values from dwh_key but it's impossible to create a one-to-many relation between attributes. My tutor has recommended me to use Lookup but I cannot figure out how to implement them in this project
This may be considered as cheating, but if you insert data into the latest view rather than the separate 6NF tables all of those ID fields will be populated by underlying trigger logic. I suspect that this would defeat the purpose of using SSIS though, since you would effectively be loading attributes sequentially rather than in parallel.
Another option is to leave surrogate key management to the ETL tool. This would require that you switch the data type for your identities from integers to GUID:s. SSIS can then generate a GUID and you can then use that very same GUID to populate all the attributes. Note that the anchor would have to be loaded first, or you will get a foreign key violation.
The most common solution though, is to leave surrogate key management to the database (and use integers). You would have a step in which you populate the metadata column in the anchor with the desired number of new identities to be created. Using the metadata number you can then select the newly generated identities and merge them into your data flow. It doesn't matter which number gets assigned to which row. After that all attributes can be populated in parallel, including their ID columns.
Of course, if this is intended to be used for more than an initial load, you would also have to add steps to detect if the data you are loading is already known or not.
I can also recommend watching the video tutorial referenced in this blog post: https://clinthuijbers.wordpress.com/2013/06/14/ssis-anchor-modeling-example-tutorial/

Change Mapped table dynamically in Entity Framework

I am using Entity Framework. In my DB, I have a table (DOCMASTER) which is mapped in my model, and on occasion, has a backup from a different date created (e.g. DOCMASTER_10_01_2015). The mappings are identical, with the exception of the keys/constraints. These are not brought over with the backup.
I have an application with a dropdown that is filled with all of the tables in the DB that are of type "DOCMASTER". The user selects which table they would like to query from, and they search for a client in that particular version of the table.
What I would ideally like to do is remap my model to use the selected table instead of the mapped table, however when I do that using DbModelBuilder, it seems to want to remap all of the tables in that model, not just the one table. I receive the error "CodeFirstNamespace.CUSTOM1: : EntityType 'CUSTOM1' has no key defined. Define the key for this EntityType" wherein 'CUSTOM1' is my table name, but I receive this for all of my tables in the model.
I am debating just using a paramaterized query to query the selected table directly. Does anyone have any thoughts on this?

Displaying fields from related tables in a Silverlight datagrid

I have a Silverlight 4.0 application that has a normalised database. In this database i have tables for Applicants, Licences, LicenceClasses, LicenceTypes and LicenceStatuses amongst others. The last 3 mentioned tables are lookup tables linked to the Licences table through foreign key relationships.I am using RIA services with the Entity Framework for data access. The scenario i am facing is as follows.
When i create a datagrid on my form i get all the appropriate colums with fields from the Licences table. I want to display the names from the lookup tables that are represented by the ID fields in the Licence table. I need to show for instance the LicenceStatus instead of the LicenceStatusID.
I have followed some examples about including the related collections in my domain service and making all the appropriate Include annotations in the Metadata classes. While i can correctly get this to work with one lookup field , i can't seem to find a way to include more than one look up table in my GetLicences query.
public IQueryable<LearnersLicence> GetLearnersLicences()
{
return this.ObjectContext.LearnersLicences.Include("LicenceClass");
}
In the above query i can only include the LicenceClass collection and i have found no way of including the LicenceStatus collection or multiple look up collections that i need to display.
How do i go about accomplishing this
You can include multiple tables by adding a include for each.
public IQueryable<LearnersLicence> GetLearnersLicences()
{
return this.ObjectContext.LearnersLicences.Include("LicenceClass").Include("LicenceTypes");
}

Resources