Entity Framework 4.3 How to save a column value Mutually exclusive between records? - entity-framework-4.3

Is there any way i can save a specific column value mutually exclusive between records.
Ex : I have an Address table and i want to save "IsPrimaryAddress " column to true. So when i add the second Address record for the Same Person with IsPrimaryAddress value True, I want to update the other records which has the IsPrimaryAddress value to be null so that only one address will have IsPrimaryAddress value =True

That is your domain logic and you must implement it in your application. EF will not help you with domain logic - it is framework for data access. Database can handle this only if you create an insert/update trigger and that is pretty ugly way to implement your domain logic.

Related

MS Access Form Validation

I have an Access form that I want to use to update records in a table. The table may have many instances of one serial number, but only one can be active at a time. The two fields are IsActive (Boolean) and SerialNumber (Short Text). I want to prompt the user if they try to create a new record when there is already an active record (IsActive=TRUE) for that serial number. I know I need to use a validation rule, I just have no idea of what the syntax should be.
I tried to use the Expression Builder, but couldn't find anything like my issue.

How to use Oracle APEX dynamic action to populate a 'form on a table' field on the basis of other fields and underlying sequence?

I am implementing a 'form on a table' that will allow the end-user to create records in the database. I would like to generate the primary key for the underlying table based on the selections the user makes as well as the underlying sequence.
The basic formula for the primary key should be a concatenation of the 'disposal site code' + 'year of study' + str(sequence). The sequence is should start at and increment by 1 so that the first project created in the database end with -1 and the nth with -n.
For example if the user selects the 'Disposal Site' as 'Seldom', the field 'Disposal Site Code' should be dynamically populated by 'CA-AT-D130' (a 1:1 code alphanumerical code for each site) and after selecting the year, for example 2022, the project number would dynamically be populated as CA-AT-D130-2022-01 (assuming first entry in the table). After creating the new record CA-AT-D130-2022-01 would become the 'project number'.
Can anyone recommend the idiomatic way to do this in APEX? I have a feeling what I am needlessly complicated something that should be relatively straightforward.
There are 2 ways of doing this that are a lot simpler than what you are trying. There is no reason at all to use a dynamic action, this can just be done using apex page processing.
option 1: in the database: Create an trigger on the table that sets the primary key column based on the other column values & sequence
option 2: in apex: Create an after submit computation on the page item to compute the value based on the other page item values & the sequence.
option 3: what I would do instead:
In my personal opinion this isn't a good option for a primary key. The primary key should just be a unique row identifier with no business value. Just use an identity column for that and no additional work is needed in db or apex.
Sounds like you want to capture business information in the key. Well... make that a "pseudo-primary key" and store that information in a column with a unique index in your database. Or, use a virtual column in your table that calculates this value from the primary key column and the other columns you need.

Oracle forms hybrid validation

This is my first post ever and haven't come across any other questions related to this. I am attempting to try and create a hybrid validation type and add it to an existing oracle form. We have a super/subset type of thing going on. When one chooses something from a dropdown, there are 5 options. If 4 of those options are chosen, the data is pulled from one validation table dataset, table A. If the other option is chosen, it comes from a different table's dataset, table B. These (along with others items) are saved in Table C. Table C has a FK constraint regarding these validations. I have added another column to table C to attempt to bypass the FK constraint, but the field still tries to save in the FK column. I can't seem to figure out if I need to add a database trigger, an item level trigger, or a form level trigger to reroute the data to correct columns in the database. Thanks in advance for any help!
If your items are select lists, you would use an item level trigger (when-validate-item) on the superset list item to populate/repopulate the list for the subset item.
Alternatively, you could use a popup LOV on the subset item which has a query which is filtered by the value of the superset item.

entity framework returning only one value but the list size is correct

Entity framework returning only one value but the list size is correct
I have a table that does not have primary id and I need to get or select all the values in it.
What I see is when I do the selection with linq the number of objects is correct but it is the first row over and over.
I am simply doing something like this
List<MyValueType> valuesInDB = myDb.MyValueTypes.ToList();
Problem is I may get thousands of rows (which is correct) but the rows all have the same exact data.
I am using VS 2010 and used the wizard to create my EF object.
The problem is that entity framework is not able to work with entity without a key. So if your table doesn't specify a key, entity framework will infer its own. The key created by EF is composed of all non-nullable non-binary columns.
So if you for example have single non-nullable column in your entity which have only very small set of values (like enum) you will be able to load only single entity "per value". The reason is an inner implementation of the context and the state manager which uses Identity map pattern. When data record is retrieved from database, EF will first check an entity key and tries to find an object with the same key in its internal storage. If an object is found it will use that object instead of data record retrieved (despite of different data). If an object with the key is not found a new object is materialized and added to internal storage.
That is the purpose of Identity map - object with given key should be created only once by each context. Identity map is core pattern in ORM.
I wrote about Identity map also in this question.
I would suggest searching for the word "Warning" in your EDM's designer.cs file. It might tell you if Entity Framework is having any issues with your table.
I really can't comment much in the absence of the table design. I tried replicating your problem but wasn't able to do so. Here is what I did:
Created a table with no primary key but it had a unique key on an ID column. Entity Framework was able to infer a primary key and when I fetched the data, I not only got the correct number of rows but also the corrects data in those rows.
Created a table with no primary key and no unique key. Also there was no column called ID. Entity Framework excluded this table in the EDM that was generated. Consequently I wasn't able to query this table at all.This was displayed as a warning in the EDM designer file.
It would be better if you can share the create script for your table.

Oracle Row Level Security in multi-tenant app / default values for new records

Task
Retrofit an existing application to use a multi-tenant approach. It shall be possible to create tenants and each user's session should reference exactly one active tenant. Each tenant should only be able to see and update his partition of the database schema.
Approach
Create an Oracle application context that contains the tenant id
Add a tenant id column to any table that should be scoped
Create a predicate function that returns "tenant_id = sys_context('tenant_context', 'tenant_id')" for SELECT, INSERT, UPDATE and delete
Add an appropiate policy via dbms_rls to register the predicate function
This works like a charm without touching the existing application for SELECT, UPDATE and DELETE
Question
When inserting the tenant_id column doesn't get set and a security exception comes up. Is there any way that is as sleek as the predicate function to always set security related fields? I'd rather not add triggers to 300+ tables.
Sometimes asking a question provides the answer. I wasn't aware that you may use non-constant expressions in column's default values, so
alter table XXX
add column tenant_id default sys_context('tenant_context', 'tenant_id');
actually solves my problem.

Resources