I'm using VS2008 to connect to a SQL server database in order to populate it in C#. It's going pretty well, I'm able to query, insert, and update all tables in my database successfully, except one. Anytime I try to query or insert into one table I get the following error:
Message = "Invalid object name 'DB_NewModelStoreContainer.DATATYPE'."
the query that generated this error is:
var test3 = (from o in context.DATATYPE
where o.DATETYPE_NAME == "Single"
select o).First();
(yes I know it should be DATATYPE, but that is not the problem =) )
Whenever I added the database to my project, for some reason it marked every attribute in the DATATYPE table as a primary key. I went into the xml of the .edmx and fixed this but I still get this error and I cannot find out why ><. Any help at all would be very greatly appreciated! Thanks in advance.
In order to work with EntityFramework ,there should be clearly mentioned PrimaryKey and Foreign Key if any exists.
If you don't have any primary Key ,make the unique column as primary key ,It should definately work. and I am wondering how could you are able to insert record without primary key
in edmx designer you might have misssed something or may be some mapping got screwed up after changes.so,modify the tables in your database to asign the primary key to every table and then try to generate the edmx from scratch.
Related
I'm trying to import tables and views from an Oracle database using ODAC 12c Release 4 and Visual Studio 2015 with EF 6 into an .edmx model.
I can import most tables and views just fine, but some of them contains errors and I can't figure out how to fix these. Specifically there are two types of errors I'm having trouble with:
Foreign keys with wrong data type. Usually a NUMBER foreign key column connected to a NUMBER(9,0) column. These will be translated to decimal and int32, causing errors.
Views without primary keys.
Previously I have used ODAC 11 with EF 5 where I could solve these errors in the following way:
The import would add the problematic tables to the diagram and point out the error. To fix it all I had to do was to change the datatypes in the model.
To get a primary key I added ROW_NUMBER() AS ID as a column and set it as a primary key with a disabled constraint CONSTRAINT ID_PK PRIMARY KEY (ID) DISABLE. This would let me import the view but I'd still get an error about primary keys being nullable so I created a script that would add Nullable=False for all primary keys. After this everything would work fine.
Trying to import tables and views with these problems using the new software is much more problematic. Instead of importing first and pointing out the errors afterwards, I'm not allowed to import at all. This is where I get stuck for the two problems:
Trying to add tables with this problem will fail without explanation. Nothing will be added to neither diagram, model nor model.store. Adding one table at a time lets me add either end of the problematic foreign key connection but trying to add the other table will just give this error then do nothing:"The model was generated with warnings or errors. Model.edmx. Please see the Error List for more details. These issues must be fixed before running your application."But the error list will be empty. I can't even see what table is causing the problem to help me fix the issue in the database.
Before adding row number as a primary key the view will be added to model.store but commented out with this error:
"Error 6013: The table/view '[ViewName]' does not have a primary key defined and no valid primary key could be inferred. This table/view has been excluded. To use the entity, you will need to review your schema, add the correct keys, and uncomment it."
After adding row number as a primary key I get this error instead:
"Error 13101: Key part 'ID' for type '[ViewName]' is not valid. All parts of the key must be non-nullable."
The view won't be imported at all, giving me no way to fix the problem since Oracle doesn't allow Nullable=False on views. So I can't fix it before importing but I can't import it without fixing it first...
How am I supposed to deal with these problems using ODAC 12c Release 4 and EF 6?
Having to go back to ODAC 11 and Visual Studio 2012 with EF 5 every time I want to import tables and views from the database is getting annoying.
Searching for others with the same problem only gives a few hits and no answers.
EDIT:
I found a work around for the problem with views.
I created a Row_Number table with Row_Number as the only column and joined the view with that. Since the Row_Number from the table is a primary key it will be marked as Nullable=False in the resulting view and can be imported.
Some code to help anyone else with the same problem:
CREATE TABLE "ROW_NUMBER"
( "ROW_NUMBER" NUMBER(9,0) NOT NULL ENABLE,
CONSTRAINT "ROW_NUMBER_PK" PRIMARY KEY ("ROW_NUMBER"));
CREATE OR REPLACE PROCEDURE CREATE_ROW_NUMBER IS
LOOP_ROW_NUMBER NUMBER := 1;
BEGIN
LOOP
INSERT INTO ROW_NUMBER(ROW_NUMBER)
VALUES (LOOP_ROW_NUMBER);
LOOP_ROW_NUMBER := LOOP_ROW_NUMBER + 1;
IF LOOP_ROW_NUMBER > 1000000 THEN
EXIT;
END IF;
END LOOP;
COMMIT;
END CREATE_ROW_NUMBER;
begin
CREATE_ROW_NUMBER;
end;
CREATE OR REPLACE FORCE VIEW New_View (“ID”, [COLUMNS]) AS
SELECT Row_Number.Row_Number, [COLUMNS]
FROM Row_Number INNER JOIN (
SELECT ROWNUM Row_Number, [COLUMNS]
FROM(
SELECT * FROM Table1
UNION
SELECT * FROM Table2
) Original_View ON Row_Number.Row_Number = Original_View.Row_Number;
Nice solution. works perfectly. Here I want to share 3 additions.
a ")" is missing
you can use an already existing view as original view
add "_1" in the alias view name
Query:
CREATE OR REPLACE FORCE VIEW New_View (“ID”, [COLUMNS]) AS
SELECT Row_Number.Row_Number, [COLUMNS]
FROM Row_Number INNER JOIN (
SELECT ROWNUM Row_Number, [COLUMNS]
FROM (SELECT [COLUMNS] FROM Original_View)
) Original_View_1
ON Row_Number.Row_Number = Original_View_1.Row_Number;
I'm studying Linq, I have a table with a relationship between processos.idProcesso and ProcessosDetalhe.id_ProcessosDetalhe, but, when I try to add the informations in DataBase, I get one error:
I tried to use the Add function, but, I can't use the add function in datacontex
So, how I can solve that error ??
or, what I need to make in SQL to correct that error ?
The error mean that in your table ProcessosDetalhes, it has a foreign key reference to another table. The FK cannot have a value in that column that is not also in the primary key column of the referenced table.
I need to implement an import export module for three database tables which are internally related with primary key/foreign key constraints.
I can do both part easily using c#/linq to xml/linq to sql. Problem is, when i am importing the exported data, in database, primary key tables' id are regenerating without taking/assign the id from xml file(its natural for auto increment). For this, other tables data can't be imported/showing error that primary key constraint violates.
My goal is to import all data and keep the primary/foreign key intact.
Now, can anyone please suggest me what I can do to solve this issue please? Thanks in advance.
You can use identity insert to specify a value for identity columns.
If you specify explicit values for identity columns, you yourself are responsible for resolving conflicts. If the database already has a row with id = 1, you have to decide what to do if the import also contains id = 1: SQL Server will just throw an error.
We always used BCP operations for such tasks.
I would suggest to not make your autoincrement the primary key but to have a separate primary key which can be defined somehow else... then you won't have the problem with the autoincrement
If you don't need the ids in the XML and SQL to match (only the relationships) you could use SCOPE_IDENTITY() and maintain mapping from XML ids to SQL ids to insert properly foreign key columns in other tables.
I'm a bit of a noob with DAO and SQL Server and I'm running into a problem when I'm trying to insert values into two tables that have a relation. The table Photos has a gpsId field which has a foreign key relation with the id field of the GPSLocations table. I want to create a new Photos entry linked to a new GPSLocation, so the code looks something like this:
gpsRow = dataset.GPSLocations.AddGPSLocationsRow("0.0N", "3.2W");
dataset.Photos.AddPhotosRow(#"c:\path\file.jpg", gpsRow);
tableAdapterManager.UpdateAll(dataset);
However this results in the following error:
A foreign key value cannot be inserted
because a corresponding primary key
value does not exist. [ Foreign key
constraint name = photoToGps ]
I'm using SQL Server CE. Is my understanding correct that the TableAdapterManager should be handling this hierarchical update? I just dragged these tables onto the XSD view and relied on its automatic creation of the wrapper classes. Do I need to change anything about the relation (eg to make it a Foreign Key constraint)? I've noticed that under some circumstances the gps id is positive and sometimes negative, is that relevant?
EDIT:
I've also ensured that the update property is set to CASCADE, which results in the same error. Hierarchical updates are set to true and there is a foreign key constraint between the two tables in the designer.
It's just the configuration of your data set. Doubleclick the relation beween the tables in the Visual Studio's dataset designer, choose Both Relation And Foreigh Key Constraint option and in the Update Rule field choose Cascade option and that must be it.
Some information about the subject is in MSDN, you can look here http://msdn.microsoft.com/en-us/library/bb629317.aspx and go to the related topics.
I've managed to track down the source of this problem, which boils down to a limitation of SQL Server CE compared with the full SQL Server. It turns out the major hint that something wasn't right was because the ids were negative. The ids are negative in the DataSet before the row is inserted into the database, at which point it gets resolved to a positive index. The fact that it wasn't becoming a positive index happened because the TableAdapterManager normally does a batch statement of INSERT followed by a SELECT to update the id. However, SQL Server CE doesn't support batch statements, so this requires extra code to be written so that we simulate the SELECT step by responding to the RowUpdated event. This MSDN article explains the steps.
Did you enable Hierarchical Updates as described here?
Is there a foreign key constraint between the two tables (there should be a line on the XSD designer connecting them)? Since your fields are named differently it might not have been automatically added when you dragged the tables to the design surface.
Since the column photoToGps (foreign key) depends on the primary key (id), you cannot add a photoToGps unless there is a corresponding id present. So what you need to is individual updates, instead of doing an UpdateAll. First update the GPSLocations table, and then the other table. That way, you will have an id existing before you add a photoToGPS for it.
I don't know what's wrong with this code , this code compile successfully but doesn't update database . do i miss some thing ?
DataClassesDataContext db = new DataClassesDataContext();
var query = from p in db.AllPatiences
select p;
int newID = 1001;
foreach (AllPatience patient in query)
{
patient.Id = newID.ToString();
newID++;
}
db.SubmitChanges();
My guess is that you're working with a file-based database (.mdf) in your project, and you're trying to look at the table data in that database. When you build your project, the database is cloned into the bin/debug/ or bin/release/ directory, where your running program accesses it. If you take a look at this version of the file, rather than the one you have loaded into your VS project, you should see the changes.
If this is the case, you should set the database file property to "Copy only if newer" or "Do not copy", to avoid it cloning the DB each build.
is patient.Id generated in the database itself? Possibly as an SQL IDENTITY field?
Also, check the SyncOn property of the field in your dbml file.
I found the problem , For Update like that , It's necessary to have primary key . The reason my table didn't have primary key was : I imported those data from an Excel file , So i didn't have Primary key .
Guess, is "Id" your tables primary key? If so, I don't think that L2S supports an update of the primary key (what's very correct in my opinion). You should never attend to change the value of the primary key.
If Id is not your primary key, I have no clue :-P.