Related Tabled, Identity and ReferentialConstraint
Posted: Fri 07 Mar 2014 07:38
Hello,
I'm creating a simple log database and i've a problem on table linking Identity columns and ReferentialConstraint error :
I 'm using :
- visual studio 2010
- Entity framework 6
- dotconnect Sqlite v5.1.103
Database sample :
1. when i add association between main and related class with an Identity column (majority of cases) entity model editor does not set corresponding MainClass.IdRelatedClass property to Identity. Entity Model cannot generate and throw an error :
2. setting manually MainClass.IdRelatedClass to Identity, trying to insert anything throw an error :
Code :
Error :
Question : where is my error ? and how to have some table with Identity set for Primary Key and linked them to another table in a simple 0..1 <-> many way ?
Thanks in advance for any help/hint.
I'm creating a simple log database and i've a problem on table linking Identity columns and ReferentialConstraint error :
I 'm using :
- visual studio 2010
- Entity framework 6
- dotconnect Sqlite v5.1.103
Database sample :
Code: Select all
-- Script was generated by Devart Entity Developer, Version 5.7.299.0
-- Script date 07/03/2014 08:23:54
-- Target Server: SQLite
-- Server Version: 3.7
--
-- Creating a table RelatedClasses
--
CREATE TABLE RelatedClasses (
IdRelatedClass INTEGER PRIMARY KEY AUTOINCREMENT,
SubPropertyTest1 INT32
);
--
-- Creating a table MainClasses
--
CREATE TABLE MainClasses (
IdMainClass INTEGER PRIMARY KEY AUTOINCREMENT,
PropertyTest1 TEXT,
IdRelatedClass INT32,
CONSTRAINT FK_MainClasses_RelatedClasses FOREIGN KEY (IdRelatedClass) REFERENCES RelatedClasses (IdRelatedClass)
);
If Ids definition must match, why entity model editor does not set it automatically ?The types of all properties in the Dependent Role of a referential constraint must be the same as the corresponding property types in the Principal Role. The type of property 'IdRelatedClass' on entity 'MainModel.Store.MainClasses' does not match the type of property 'IdRelatedClass' on entity 'MainModel.Store.RelatedClasses' in the referential constraint 'FK_MainClasses_RelatedClasses'.
2. setting manually MainClass.IdRelatedClass to Identity, trying to insert anything throw an error :
Code :
Code: Select all
using (MainEntities entities = new MainEntities(ConnectionString))
{
RelatedClass relatedClass = new RelatedClass
{
SubPropertyTest1 = 1
};
entities.RelatedClasses.AddObject(relatedClass);
entities.SaveChanges(); // no pb
MainClass mainClass = new MainClass
{
PropertyTest1 = "test",
RelatedClass = relatedClass
};
entities.MainClasses.AddObject(mainClass);
entities.SaveChanges(); // throw error
}
Error :
A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'IdRelatedClass'.
Question : where is my error ? and how to have some table with Identity set for Primary Key and linked them to another table in a simple 0..1 <-> many way ?
Thanks in advance for any help/hint.