Entity Framework db first: states machine associations - oracle

I want to manage a simple state machine with EF.
I have a registry table of states and I'm supposed to have a table with two fields: an ID of a starting state and an ID of a destination state.
The associations table should have distinct couples of starting and destination ids.
I tried to make both of field as primary key and as foreign key referring to the registry table, but it's bad imported into the edmx model.
The registry state is like this:
ID DESCR
1 A
2 B
3 C
the associations table should be like this:
ID_STATE_START ID_STATE_DEST
1 2
2 3
So, in this case I can say that a process can go from A to B and from B to C.
EDIT:
This is the model VS2017 created for me:
I have two tables in db, but VS imported just the registry with two references on its own.
By the way this is not importan since i just need a right solution.

Related

Entity Framework 6 and Oracle: The table/view does not have a primary key defined. The Entity is read-only

I have an ASP.NET Core application that uses EF6 for dealing with a third-party application's database.
Everything is working as expected, but I'm unable to insert rows into a joining table.
I have two tables, Users and Groups, and a joining table GroupUser that identifies which users are members of which groups. Users has a PK of UserId, and Groups has a PK of GroupId.
GroupUser has only 3 columns: GroupId, UserId and another column (which is irrelevant for this post). The two foreign keys in this table identify a unique record.
Every time I try to insert into GroupUser, I get the inner exception
The table/view does not have a primary key defined. The entity is read-only
The error is correct. There is no PK, but both of the FKs are marked as keys in the model. Shouldn't VS be able to use those as a PK somehow?
The inserts used to work as some point, but required some manual modification of the .edmx file as XML in order to work. Unfortunately, our version control records containing this modification have been lost (and I wasn't the one originally working on this).
I've looked at and tried about a dozen articles around this, but they generally have to do with views instead of tables, so don't seem applicable to my case. The ones that did seem applicable didn't solve the issue.
The only other clue I have for a solution is this comment I found in the code:
// Important note: If you have updated the edmx file in the [redacted]
// project and suddenly start having problems, the edmx file may need to be
// edited as an xml file so that you can make changes necessary to make
// VS believe that the GroupUser table has a primary key. See revision #[redacted]
I'm able to insert into User and Group tables just fine, and as I've said, I don't have access to the revision log mentioned.
Edit: The database is for a third-party application, and unfortunately, it's not as simple as just modifying the table to add a PK. I wish it was. Problem would be solved. But I've been advised by the vendor not to make this change, as it may have unexpected consequences, and would void our support.
How can I 'trick' EF into thinking the table has a key? I'm also open to other workarounds. Modifying the DB structure is currently out of the question.

Unable to add a table from an existing database to EDMX using Database First Entity Framework

I am using Database First EF to generate model from the existing database. When I first generated the models, it ignores only one of the table, the entity was not added to EDMX, no model file is created for the table and no context is created for the entity.
When I tried to explicitly add the table to EDMX (when generating the model, selected the specific table first and then updated the model with all the other tables from the database), it complained with the following error.
Two entities with possibly different keys are mapped to the same row. Ensure these two mapping fragments map both ends of the AssociationSet to the corresponding columns.
This specific table has two columns which are primary keys of some other tables and both the columns are specified as Primary keys for the table.
Am I doing something wrong or should I handle this table differently since it has two columns defined as Primary Keys? Any suggestions greatly appreciated!
You are not doing anything wrong. Your table is junction table for many-to-many relation. You don't need that table in the model because EF (in contrast to database) can handle many-to-many relation directly without any intermediate. The table is actually mapped on behind of the many-to-many relation - you will see that in mapping details window.
Btw. you are not using code first. Code first = no EDMX.

Entity Framework, odata syntax

As indicated in the Entity Framework image below, I have 3 tables, tblModel, tblModelFetish and tblFetish. A record in tblModel can have multiple records in the tblModelFetish table linked by the modelid column. The tblModelFetish table links to the tblFetish table via the fetishID column to get the fetish description stored in the fetish column. This Entity Model was generated with VS 2010 from an existing database including foreign keys.
a Entity Data Model http://spreadthenudes.com/efmodel.jpg
Using the odata syntax, I'm able to access all models (http://localhost:51157/WcfDataService.svc/tblModels) or a specific model (http://localhost:51157/WcfDataService.svc/tblModels(11)) successfully. I'm having trouble accessing the related tables data via odata, I've tried many permutations including expand etc.
What I want is a result set of all the columns in the tblModel and the related tblFetish records including the fetish column from the tblFetish table. In other words, Mary (a modelname in tableModel) has 3 fetishes (3 records in tblModelFetish) named beach, travel and coffee (stored in tblFetish, fetish column).
What is the odata syntax to acquire this?
thanks for reading! Bob
Try either:
http://localhost:51157/WcfDataService.svc/tblModels(11)?$expand=tblModelFetishes/tblFetish
or
http://localhost:51157/WcfDataService.svc/tblModels?$filter=id eq 11&$expand=tblModelFetishes/tblFetish
Just to make it clear you will not get one huge result set with all columns but the entity graph consisted of your entities.

Linq to SQL .DBML Child Property Problem

I have a 1:1 relationship between table 'A' and 'B' in my .DBML. The FK in the database is in place and the .DBML diagram shows an association line between 'A' and 'B'. However, I cannot get the code generator to create a child property in the 'A' entity. All I have is the FK column. In the Association properties, I have ChildProperty set to true. However, the code generator will not create the child property. I have dropped and added the two tables several times.
Anyone have any ideas?
The O/R designer will refuse to create an association property if a primary key is missing on one of the associated tables. Make sure all of your associated tables have a primary key.
Not sure, but I think what you call 1:1 is actually seen by the DBML as 1:* because the list can "have" many of your fk-table, e.g. one empley oyee can have one city, but each city can "have" many employees.
AFAIK a primary key in each table is a prerequisite without which the DBML will not "work". An error is issued when saving it. Your project will compile, but you'll see the errors later. HTH

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.

Resources