Check alle échte Black Friday-deals Ook zo moe van nepaanbiedingen? Wij laten alleen échte deals zien

C# Entity Framework probleem met toevoegen aan database

Pagina: 1
Acties:

  • Kayshin
  • Registratie: Juni 2004
  • Laatst online: 09-03-2018

Kayshin

Bl@@T @@P!!!

Topicstarter
Ik ben bezig met een applicatie waar ik gebruik maak van Entity Framework. Ik heb een model, waar ik een object (robot) heb die een ander object (coordinaat) als child moet hebben:
Afbeeldingslocatie: http://oi39.tinypic.com/21aijr9.jpg

Een robot heeft 1 property welke het coordinaat moet zijn. Dit is dus een 1 op 1 relatie. Een coordinaat hoeft niet te weten bij welke robot hij hoort.

Bij het modelleren van deze relatie wordt dit dus:
Afbeeldingslocatie: http://oi41.tinypic.com/2u58eqg.jpg
Afbeeldingslocatie: http://oi40.tinypic.com/hx0is0.jpg

Op basis hiervan bouw ik mijn database (Generate database from model) en voer ik de query die hij maakt uit op mijn database:

SQL:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
SET QUOTED_IDENTIFIER OFF;
GO
USE [Südselekt.Robotics.Configuration];
GO
IF SCHEMA_ID(N'dbo') IS NULL EXECUTE(N'CREATE SCHEMA [dbo]');
GO

-- --------------------------------------------------
-- Dropping existing FOREIGN KEY constraints
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[FK_ToolOffset]', 'F') IS NOT NULL
    ALTER TABLE [dbo].[RobotSet] DROP CONSTRAINT [FK_ToolOffset];
GO

-- --------------------------------------------------
-- Dropping existing tables
-- --------------------------------------------------

IF OBJECT_ID(N'[dbo].[RobotSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[RobotSet];
GO
IF OBJECT_ID(N'[dbo].[CoordinateSet]', 'U') IS NOT NULL
    DROP TABLE [dbo].[CoordinateSet];
GO

-- --------------------------------------------------
-- Creating all tables
-- --------------------------------------------------

-- Creating table 'RobotSet'
CREATE TABLE [dbo].[RobotSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [ToolOffset_Id] int  NOT NULL
);
GO

-- Creating table 'CoordinateSet'
CREATE TABLE [dbo].[CoordinateSet] (
    [Id] int IDENTITY(1,1) NOT NULL,
    [X] float  NOT NULL,
    [Y] float  NOT NULL,
    [Z] float  NOT NULL,
    [RX] float  NOT NULL,
    [RY] float  NOT NULL,
    [RZ] float  NOT NULL
);
GO

-- --------------------------------------------------
-- Creating all PRIMARY KEY constraints
-- --------------------------------------------------

-- Creating primary key on [Id] in table 'RobotSet'
ALTER TABLE [dbo].[RobotSet]
ADD CONSTRAINT [PK_RobotSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- Creating primary key on [Id] in table 'CoordinateSet'
ALTER TABLE [dbo].[CoordinateSet]
ADD CONSTRAINT [PK_CoordinateSet]
    PRIMARY KEY CLUSTERED ([Id] ASC);
GO

-- --------------------------------------------------
-- Creating all FOREIGN KEY constraints
-- --------------------------------------------------

-- Creating foreign key on [ToolOffset_Id] in table 'RobotSet'
ALTER TABLE [dbo].[RobotSet]
ADD CONSTRAINT [FK_ToolOffset]
    FOREIGN KEY ([ToolOffset_Id])
    REFERENCES [dbo].[CoordinateSet]
        ([Id])
    ON DELETE NO ACTION ON UPDATE NO ACTION;

-- Creating non-clustered index for FOREIGN KEY 'FK_ToolOffset'
CREATE INDEX [IX_FK_ToolOffset]
ON [dbo].[RobotSet]
    ([ToolOffset_Id]);
GO


Voor het gebruik hiervan voeg ik vervolgens een referentie naar de service toe in mijn andere project en wil ik data gaan toevoegen in mijn database. Ik doe dit op een zelfde manier in een andere applicatie met een iets andere datastructuur (configurationservice is de referentie naar mijn service):
C#:
1
2
3
4
5
6
configurationservice.Coordinate coord = new configurationservice.Coordinate();
unitOfWork.AddToCoordinateSet(coord);
configurationservice.Robot robot = new configurationservice.Robot();
unitOfWork.AddToRobotSet(robot);
unitOfWork.SetLink(robot, "ToolOffset", coord);
unitOfWork.SaveChanges();


Echter krijg ik dan de error bij savechanges:
Entities in 'ConfigurationModelContainer.CoordinateSet' participate in the 'ToolOffset' relationship. 0 related 'Robot' were found. 1 'Robot' is expected.

Ik heb deze error bij mijn andere applicatie niet gehad en ik krijg het niet voor elkaar om dit werkend te krijgen. Bij een ander soort relatie werkt dit wel (heb de entities van mijn andere project gekopieerd en dezelfde functies daarop gebruikt) maar bij dit soort relatie dus niet. Volgens mij is mijn concept voor mijn model wel correct.

My personal videoteek: -Clique-; -NMe- is een snol!