Mapping Exsisting DataBase Tables to Entity Framework is causing many problems - entity-framework-4.1

I have already existing DataBase inside SQL server 2008 around (300 tables) so I faced the following problems when i try to create an entity framework .edmx file:
If I specify to map all the 300 tables at once, the visual Studio will hang.
So I decide to include only the tables which I currently need, then the mapping will work fine. But if after that I add a new table to them, a Foreign key error will occur. So I have to delete the existing models and add them again with the new table. So the FK error will be removed.
So can anyone advice on how to overcomes these problems?
The error I usually got when adding a new table to the .edmx file will look something similar to.
Problem in mapping fragments starting at lines 2186, 2265:Foreign key constraint 'SDOrgPostalAddr_FK2' from table SDOrgPostalAddr (POSTALADDR_ID) to table AaaPostalAddress (POSTALADDR_ID):: Insufficient mapping: Foreign key must be mapped to some AssociationSet or EntitySets participating in a foreign key association on the conceptual side.
while if I added the table from the beginning then no error will be displayed.
Regards

Related

Error when adding foreign key to newly created table (mySQL)

I am trying to alter table product to add a constraint of type foreign key for field petCat_ID so that it references table petCategory(ID). I just created table petCat_ID and i am getting a "Cannot add or update a child row" error.
This is the commands I performed to get this error:
Alter table product
-> ADD CONSTRAINT FK_petCatID
-> FOREIGN KEY (petCat_ID)
-> REFERENCES productCategory(ID);
Any help or tips would be greatly appreciated! Note: petCat_ID is in table product and productCategory is a different table.
In my comments I've mentioned that I need a clearer idea of what kind of database structure you have, but I have a series of things that will help you work through the problem you're having.
If an ALTER statement isn't working, and you have good syntax, it is because what you are doing conflicts with an already present rule.
Sometimes, doing a DROP TABLE command, followed by creating the table again can fix problems. This can be problematic if there are dependencies that keep you from dropping the table.
When things get dire, try looking at the script you used to make the DB in the first place. Modify it and see if you can get the properties you want. Once you do, make a new database table structure and migrate your table entries over to the new database from the old one.
I made a github repository here wherein I made a third normal form version of what the customer facing Amtrack database would look like, and even wrote scripts to add data to the tables, with examples. There are images showing the ER structure. I included my creation script, broken into each table's creation in specific order. It should be a good reference for how to assign table relationships, and that will give you a good idea of what you can alter. Disclaimer I wrote it for SSMS, but I don't believe I used anything SSMS specific I THINK that code should work in MySQL.

still error "multiple primary keys defined" when tables/databases are dropped

I want to put my live site (wordpress) on localhost, so I exported the database (turning on the options for dropping database and tables) with phpmyadmin. When importing on localhost with BigDump I get the error message that "Multiple primary keys are defined".
How can I solve this?
All the suggestions I have read so far say to drop the tables and/or database when exporting from live site. I did that but it makes no difference. What else can I do to succesfully import the DB on localhost?
Check your create table statements in exported file. May be somewhere in your create statemate two primary keys are defined.
A table can have only one primary key, which may consist of single or multiple fields. When multiple fields are used as a primary key, they are called a composite key.
If possible cereate database and tables manually and remove table creation statements from expoted file and you can source only data from the file.

JPA tool - Generate entities from tables with foreign keys mapped with non-primary key

I have a MySQL database on which I want to generate entities with Dali JPA Tool built into Eclipse.
The database is not mine, and I prefer not to ask for the change of architecture.
The problem exists when I create entities because a table has a foreign key that is mapped to a column in another table that is not a primary key.
I have noticed that for this reason the entity of this table is not created by the tool (but it's added in persistence.xml).
Is there a way to generate the entity without having to change the database architecture?
Thanks a lot for every possible idea.
PS: I'm not a database expert, but is it ok to create foreign keys that are mapped to columns that are not primary keys?

SQL - Foreign key is part of the composite key in the parent causing error

I'm designing a small database and made the relationship model using the MySQLWorkbench 6.3.
When I try to transfer the relationship to excel by manually creating new relationship, I get this error,
Error message,
I'm guessing that's because my task table has a composite primary key instead of only one. What would be the best way of fixing this issue? thanks a lot,

Insertion conflict LINQ-to-SQL

I get this error when insert a new album:
{System.Data.SqlClient.SqlException:
INSERT statement conflicted with COLUMN FOREIGN KEY
constraint 'FK_ChannelAlbum_Group'.
The conflict occurred in database 'Stamper', table 'Channel', column 'ID'.
I don't know what is going on because sometime I insert the new album to the context the error will occur but Album table has no Channel ID columns only Album_Channel table. This insert does not influence the album_channel and channel table at all. Why is there a conflict.
I realized that after I have created the new album after that i try inserting a album_channel data I have an error so I stop debugger and try to fix the problem. Once i have work that out, I go an insert a new album once more but it gives me this error always. So I close my visual studio and reopen it in order to work.....
I am not sure is it a good way of having singleton style of creating the context e.g.
I have write a context as class and this context only created once, to prevent it to create too many times.
Hard to answer without a diagram. It looks like you have a foreign key in the table you're inserting which is not a valid primary key in the table it is referencing. If you don't believe you have one, then you could try checking your relationships and possibly regenerating your L2S entities. Maybe even try to insert into that table manually and see if you get the same error.
Context wrapped in a singleton is a bad idea for web applications as it isn't thread safe. You could look into the 'unit of work' pattern or try caching the context instance.