ADO.NET reports an entity association error - c#

I got two tables named Personnel and Department.
**Personnel Table ;**
PERSONNELID int , PK not null
NAME varchar
SURNAME varchar
DepartmentID integer
Department Table ;
DepartmentID int , PK not null
DEPARTMENTNAME varchar
(ONE TO MANY Relation)
I want to create an association between these tables, but I can't do it with EDMX Model Designer. I am getting errors:
Association END KEY property PERSONNELID is not mapped.
Association END KEY property NAME is not mapped.
I am new at this framework. Is there any easy way of do this job in the Entity Framework?
I am using Firebird 2.1 DB and Firebird.Client.Data 2.5.0.0 for provider.

For a one-to-one relationship to work each table must have the same primary (unique) key.
In your case you probably have a one to many relationship:
One department can have many persons.

Related

How to map table name to a column on many to many relationship using EF6 Code First

I have a database table that does not contain true foreign keys. It has two columns that reference the table name and the id of the record in that table. If it were a true foreign key then I could just create a many to many relationship and be on my way but this one has me stumped because of the table name dicriminator. Any help on how I can do this in the fluent API would be great!

Create Association Model First With Ignoring Certain PKs

I have the following Table Structure
Facility
PK Facility ID
AccountID
Accounts
PK NameID
PK AccountID
I can't touch the DB so my changes need to be in Entity Framework. Essentially the AccountIDs are linked so I want to create an association between them. So when I create an association I map the AccountIDs together, however I can't map FacilityID to anything and NameID to anything so when I save Visual Studio complains that the mapping is not set correctly.
My main question is how do I ignore the mappings for FacilityID and NameID? I've tried added [NotMapped] to both FacilityID and NameID but that does not work. I've also tried creating a scalar property for Facility and Accounts and used the Referntial Constraint to map them however when I try to map the columns under Table Mapping, the columns I added do not show up which causes VS to complain as well.
Here is my table, I removed most of the fields because they are unnecessary
Assuming Account.AccountID is unique (ie no two rows in Account actually have the same AccountID), just declare that as the only Key Property on the Account entity.
The Key of an entity does not have to be declared as the PK in the database. But you can only have one Key per entity (the Key can, of course, have multiple columns, and EF Core does support alternate keys). The entity Key should be unique, and should have a unique index in the database on the corresponding columns, but that's not enforced by EF.

Disable associations when a foreign key do not exist

I've created an edmx for my database. In it Entity framework removed a table and instead created an association between two tables because it matches a column name with the primary key in the other table.
I do not want that as there is no real association between those tables. How can I remove that association and get a class for the middle table instead?
Example:
SomeTable
Id int pk
MiddleTable
SomeTableId int fk
SomeCode int
OtherTable
SomeCode int pk
It's the MiddleTable which do not get a class.
Remove one table from the edmx, e.g. OtherTable.
Update model from database and add MiddleTable.
Update model from database and add OtherTable.
When I do this with a similar model I end up with an association between SomeTable and MiddleTable and an unassociated OtherTable. Now you can add/remove associations manually as you wish.
It's normal EF behavior not to create a class for the middle table. This is a so-called many to many association between SomeTable and OtherTable which can be modelled by two collection properties:
SomeTable.OtherTables
OtherTable.SomeTables
The middle table, the junction table, is not really necessary.
It's a bit surprising to me that you say that there is no association between the two tables although, apparently, in the database there are foreign keys. Technically, it is a many to many association.

Splitting an entity into 2 tables with Entity Framework

I have 2 tables in my database (Oracle) which I would like to merge into one entity.
I'm using Entity Framework 4.1 code first approach (with existing database) so everything works fine.
The tables structure:
Table Document_Metadata:
Item NUMBER(2) PK
Version NUMBER(2) PK
DocDate DATE PK
RepType NUMBER(2) PK
ValueType NUMBER(2) PK
DocId NUMBER(5) NOT NULL FK -> Document table
User VARCHAR2(5) NOT NULL
RowVersion DATE NOT NULL
Table Document:
ID NUMBER(5) PK
Doc LONGRAW
User VARCHAR2(5) NOT NULL
RowVersion DATE NOT NULL
Is there a way to accomplish that, and if so how would I do that?
Thanks in advanced.
I don't think you can do that. If this is "one to one" relation, maybe you could go with table per type inheritance, and inherit Document_Metadata class from Document class. So you would have one entity object for both tables. But for that to work DocID would have to be PK in Document_Metadata and I think that primary key column names for both tables would have to be same (rename ID to DocID in Document table). But that requires changes in your database model.

Manual Entity Framework association

I have the following tables:
Table Group (
id INT PK,
year INT PK,
name VARCHAR
)
Table Person (
id PK,
GroupID INT,
name VARCHAR
)
The database does not have foreign keys defined so I want to create a manual association from the Person tables GroupID to the Group tables id.
To do this I right click Person and Add an association. I create a Many to One association and everything works. The problem is when I go to add the mapping. Because the Group table has two primary keys entity framework was something from the Person table to map to the year key.
What do I need to do to create the association?
You cannot create such association because EF follows same rules as database. All PK columns from principal entity must exists as FK columns in dependent entity.
The only way can be some database view selecting distinct Groups with Id and Name and mapped as read only entity and build navigation between those two. I didn't try it but I guess it should work. It will have its own disadvantages because you will have two completely unrelated entities for group and the entity related to person will not accept any modification (without mapping custom SQL commands or stored procedures to insert, updated and delete operations).

Categories