Two entities with possibly different keys are mapped to the same row - sql

I have Table
Field1 PK int not null
Field2 PK int not null like this
when i want to map this table I get this error
Error 3 Error 3034: Problem in mapping fragments starting at lines 2212, 2218:
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.
And I tried to delete and re-create that table inside model. When i add table it gives another error but at the end i always get this error
How can i handle this problem please help me...

When i investigate the problem, i realised something becuse i read an article before for this data model situation and i understand the why the problem occur. (Problem in mapping fragments in Entity Framework)
Actually problem comes from table mapping because i said that table has many to many relationship so That article says if you put that table in the model design it always gives us this error and finally when we delete that table on design side and add table silently in data model so program can be build. I ‘m telling you this because maybe you can make an idea for this because i learned this and when i add new view in model and vs doesnt give me any error.
This is the old model picture when the delete that table inside red box The program doesn't give any error and continiue to use bottom table

If nothing else works, try deleting and recreating the whole .edmx-file.
I got this problem when I added a couple of many-to-many tables to an existing Entity Framework 6.2 project.
I tried removing and regenerating all tables in the model. Nothing seemed to work. I believe there was some "junk" somewhere in the EF project causing the problem.
When regenerating the edmx, I added all tables in the project, including the many-to-many tables.

solution for error:3034
PK UserId;
PK RoleId
two primary key cannot allow edmx file.so create sno column in that table and make that as primary key . remove the pk of UserId and RoleId.
now in that two column there is no primary key available.
like
Pk sno;
FK UserId;
FK RoleId

Related

Weird behavior with foreign keys and relations in ERD software, is this how SQL works?

I have been tasked with creating a database but first i have to create a design of it as an erd, i am using for this a program named toad data modeler, but i have one problem with the foreign keys and relations in general.
As seen by this picture(the database is in Polish but you should understand what i mean) :
https://i.ibb.co/Qj6r7Dk/436343664.jpg
The row id_pacjenta shows up in entity9 without a reason, i didn't create a relation between pacjenci and entity9, only entity 9 and Wizyta, Why such a behavior occurs? Also i can't delete it because it automatically deletes also the row that i need (id_wizyty) What i suppose happen is that it takes that row that i don't want (id_pacjenta) from the relationship that i made between Pacjenci and Wizyta, but i don't know why and if it should work that way, what steps should i take to fix this? Thanks a lot in advance!
There's nothing weird about it.
entity9 references wizyta and the primary key of wizyta includes a column id_pacjenta, so of course there has to be a corresponding column for wizyta.id_pacjenta in entity9, which is part of the foreign key referencing wizyta.
So the tool is correct to add both columns, when you introduce a relationship between the tables.

Error Creating relationship in SSAS Tabular

We are having a very frustrating problem with SSAS. we are trying to create a fairly simple Tabular model have had no problem with a few dimensions and a single fact table. when we recently added a new dimension table, and a second fact table we can not get it to stop telling us that
The relationship cannot be created because each column contains duplicate values.
we have deleted and reimported these tables many times. at times we can attach the Dim table to an existing fact without problem, which tells me that the key is unique, but then it errors when attaching to a second fact. at other times it just errors right away.
The key and Foreign Key values are Int in the database. and we have examined the data many times, creating stripped down tables with just a few columns, validating that a
Select Distinct ID from Table
type query returns the same number of rows as a wide open one.
At this point it looks like we simply have a bug in SSAS Tab. I cant see a good reason for this to not work.
any ideas would be greatly appreciated
I have had the same problem, the way I solved is to create the relationship in reverse order meaning: from the 1 to many. This will solve the issue, and the relationship's direction will indeed be from the many to the 1
I do not have any explanation for this...
sometimes SSAS cannot detect that your column is unique so you should do that. you can click on your primary key column and get properties and choose true on the unique property and after that create your relationship

Entity Framework lookup table

I have 3 tables
Brands:
BrandID int
BrandName varchar(30)
Products
ProdID int
ProdName varchar(30)
BrandToProd:
BrandID int => FK Brands.BrandID
ProdID int => FK Products.ProdID
After generating model from existing database EF omits BrandToProd table and creates Many-To-Many relationships between Brands and Products. I would like to have third entity with following fields:
BrandName varchar(30)
ProductsName varchar(30)
This will give me possibility to use scaffolding for this entity. Ideally, when I'll add new pair of Brand and Product, EF should check first if such Brand or Product already exist in database (to avoid duplicates), if no, add to corresponding tables and that add mapping to BrandToProd table. If Brand or Product already exist, EF should you existing BrandID/ProdID value when adding to BrandToProd table. Is there any idea how to do that?
Your BrandToProd table is a pure junction table, i.e. a table with only two foreign keys. It is an EF feature to model such tables into a many to many association without a class in the conceptual model.
The easiest way to include a pure junction table in the model as an entity class is
add a dummy field to the database table temporarily
generate the model
delete the field from the database
update the model from the database
delete the property in the edmx diagram
An alternative way is to edit the edmx manually, but then you really need to know what you're doing. If you don't want to regenerate the model you could generate a second model and investigate the differences in both edmx files by a difference viewer.
However, I wonder if you need to do this. You seem to relate this to duplicate checking. But if you want to add a Brand or Product to the database you'll have to check for duplicates either way. If you want to add a new association (e.g. adding an existing Brand to Product.Brands) you don't have to check whether it exists. If it does, EF just ignores the "new" association.
As extra point to Gert's answer:
when using surrogate keys, there is always the issue of duplicate management. Normally there is 1 or more fields that make a logical key.
you can
a)create a unique index on the Db. Db will complain when the constraint is violated
b)Execute a logical duplicate check before attempting an insert.
I've ended up with just adding dummy ID field to my junction table, as I'm frequently changing DB schema (becsuse site development is in progress and I need from time to time update model from database) and don't want to each time remove/add dummy field to database. Another option I've used - SQL View on two tables and stored procedures mapped to corresponding actions (CRUD) in EF

Trying to verify understanding of foreign keys SQL Server

So I'm working on just a learning project to expose myself to doing some things I do not get to do at work. I'm just making a simple bug and case tracking app (I know there are a million this is just to work with some tools I don't get to). So I was designing my database and realized I've never actually used Foreign Keys before in any of my projects, I've used them before but never actually setting up a column as a FK. So I've designed my database as follows, which I think is close to correct (at least for the initial layout).
Database Image http://drop.io/download/public/uurp1vxej0abpwu7jsee/895663dedff577359b900cda1726f115b24be90c/Asset/28368622/v3/large_thumbnail
However When I try to add the FK's to the linking Tables I get an error saying, "The tables present in the relationship must have the same number of columns". I'm doing this by in SQLSMS by going to the Keys 'folder' and adding a FK. Is there something that I am doing wrong here, I don't understand why the tables would have to have the same number of columns for me to add a FK relationship between the tables?
When defining a foreign key relationship there are four things you need to specify:
The referring table (foreign key table)
The columns from the referring table that will form the foreign key.
The referred table (primary key table)
The columns from the referred table that form the key.
I'm guessing that you have selected one of the above incorrectly - probably you have forgotten that you need to state the column involved in the relationship twice - once for each table.
Here's a screenshot of how it should look - note that the column is specified twice. In my example the columns have slightly different names to demonstrate that they don't always need to have the same name, but in your case the column names will be the same.

Association end is not mapped in ADO entity framework

I am just starting out with ADO.net Entity Framework I have mapped two tables together and receive the following error:
Error 1 Error 11010: Association End 'OperatorAccess' is not mapped. E:\Visual Studio\projects\Brandi II\Brandi II\Hospitals.edmx 390 11 Brandi II
Not sure what it is I am doing wrong.
I believe I can add some more clarity to the issue (learning as I go):
When I look at the Mapping details and look at the association, the column for operatoraccess table (from above) is blank and the drop down only includes field from the linked table.
The Entity Framework designer is terrible - I've had the same problem many times (and your problem too, Craig):
This happens when you have a many-to-one association which is improperly setup. They could very easily fix the designer to make this process simple; but instead, we have to put up with this crap.
To fix:
Click on the association, and go to the mapping details view.
Under association, click on Maps to <tablename>. Choose the table(s) which make up the many side of the relationship (ie. the table(s) which make up the *-side of the association in the designer)
Under Column, choose the table-columns which map to each entity-side Property. You get this error when one of those entries are blank.
I had the exact same problem and this is what I did to fix it.
Make sure you have an Entity Key set in your designer on the tables your making an association with. Also check that StoreGeneratedPattern is set to Identity for that Entity Key.
There's not a lot of information in your question, but, generally speaking, this means that there is an incompletely defined association. It could be that you have tried to map one table with a foreign key to another table, but have not mapped that other table. You can also get this error when you try to do table per type inheritance without carefully following the steps for implementing that feature.
Not sure of the answer, but I've just posted a similar question, which may at least help clarify the issue you are experiencing.
Defining an Entity Framework 1:1 association
I had to go back into the database itself and clarify the foreign key relationship
I had this problem in the case where I was creating both many to 0..1 and 0..1 to 0..1 associations. One entity needed associations to multiple tables, and that entity did not have foreign keys defined for those tables.
I had to do the table mappings step that is given in the accepted answer, but note that it wasn't only for many to many associations; it applied to all the types of associations I added for this entity.
In the Mapping Details view, I had to select the entity with the non-foreign key ID columns to the various tables. This is not always the "many" side of the relationship. Only there was I able to map the related entity property to the appropriate property in the original entity. Selecting the "destination" entity would not allow me to select the properties that I needed to, and the error would still exist.
So in short, I had to map using the table related to the entity that had the "non-foreign key" ID fields corresponding to the various entities' (and their tables') primary keys that I needed to associate.
Entity A
various other properties...
Id
ContactId
OrderId
etc.
Contact entity
Id
FirstName
LastName
etc.
In the mapping details, I selected Entity A's table. It then showed both ends of the association. I mapped its Entity A's Id property to its table's actual ID column (they had different names). I then mapped the Contact entity's Id field to the ContactId field on the A entity.
Simply select the many relationship table (*) from the Association>Edit Mapping & select the appropriate relationship