0

I'm having problems with entity framework

The problem is pretty simple, I have a database in sql server 2008. When I import the tables for creating the edmx file in a visual studio 2010 project (using ADO.NET Entity Data Model), not all of relationships are replicated in the model, just some of them. I've tried deleting an recreating the model, updating and even in a completely new project the same problem becomes....

for example:

This entity is called "simte_plandeestudio"

The creating script for this table is:

CREATE TABLE [dbo].[simte_PlanDeEstudio](
    [Id] [uniqueidentifier] NOT NULL,
    [Estudiante] [uniqueidentifier] NULL,
    [Curso] [uniqueidentifier] NULL,
    [Duracion] [int] NOT NULL,
    [Meta] [int] NOT NULL,
    [Estado] [int] NOT NULL,
    [FechaFinalizacion] [datetime] NULL,
    [Tutor] [uniqueidentifier] NULL,
    [FechaInicio] [datetime] NOT NULL,
    [MotivoRetiro] [varchar](50) NULL,
    [FechaIngresoTaller] [datetime] NOT NULL,
    [FechaMatricula] [datetime] NOT NULL,
    [TiempoTranscurrido] [int] NOT NULL,
PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

**ALTER TABLE [dbo].[simte_PlanDeEstudio]  WITH CHECK ADD  CONSTRAINT [FK_simte_PlanDeEstudio_simte_Curso] FOREIGN KEY([Curso])
REFERENCES [dbo].[simte_Curso] ([Id])
GO
ALTER TABLE [dbo].[simte_PlanDeEstudio] CHECK CONSTRAINT [FK_simte_PlanDeEstudio_simte_Curso]
GO**

**ALTER TABLE [dbo].[simte_PlanDeEstudio]  WITH CHECK ADD  CONSTRAINT [FK_simte_PlanDeEstudio_simte_Estudiante] FOREIGN KEY([Estudiante])
REFERENCES [dbo].[simte_Estudiante] ([Id])
GO
ALTER TABLE [dbo].[simte_PlanDeEstudio] CHECK CONSTRAINT [FK_simte_PlanDeEstudio_simte_Estudiante]
GO**

ALTER TABLE [dbo].[simte_PlanDeEstudio]  WITH CHECK ADD  CONSTRAINT [FK_simte_PlanDeEstudio_simte_Usuario] FOREIGN KEY([Tutor])
REFERENCES [dbo].[simte_Usuario] ([Id])
GO

ALTER TABLE [dbo].[simte_PlanDeEstudio] CHECK CONSTRAINT [FK_simte_PlanDeEstudio_simte_Usuario]
GO

ALTER TABLE [dbo].[simte_PlanDeEstudio] ADD  CONSTRAINT [DF_simte_PlanDeEstudio_Id]  DEFAULT (newid()) FOR [Id]
GO

The first two constraints ("simte_Curso" and "simte_Estudiante") are seen in the sql management studio diagram, but not passed to the model.

The last one ("simte_Usuario") is in the database diagram and is passed to the model.

The "simte_curso" entity has not foreign keys, and "simte_usuario" has only one foreign key related to another table

I hope it could be more clarifying

the creation scripts for "simte_curso" and for "simte_Usuario" are :

**CREATE TABLE [dbo].[simte_Curso](
    [Id] [uniqueidentifier] NOT NULL,
    [Nombre] [varchar](50) NOT NULL,
    [Orden] [int] NOT NULL,
    [UltimoCurso] [bit] NULL,
PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]**



**CREATE TABLE [dbo].[simte_Usuario](
    [Id] [uniqueidentifier] NOT NULL,
    [UsuarioOrchard] [int] NOT NULL,
    [TipoDeUsuario] [varchar](10) NOT NULL,
    [Nombres] [varchar](50) NOT NULL,
    [Apellidos] [varchar](50) NOT NULL,
    [Documento] [varchar](50) NULL,
    [TelefonoMovil] [varchar](50) NULL,
    [FechaDeNacimento] [datetime] NULL,
    [CorreoElectronico] [varchar](50) NULL,
    [sexo] [bit] NULL,
PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
SET ANSI_PADDING OFF
GO
ALTER TABLE [dbo].[simte_Usuario]  WITH CHECK ADD  CONSTRAINT [FK_simte_Usuario_Orch_Orchard_Users_UserPartRecord] FOREIGN KEY([UsuarioOrchard])
REFERENCES [dbo].[Orch_Orchard_Users_UserPartRecord] ([Id])
GO
ALTER TABLE [dbo].[simte_Usuario] CHECK CONSTRAINT [FK_simte_Usuario_Orch_Orchard_Users_UserPartRecord]
GO**
Miguel
  • 1
  • 2

3 Answers3

0

Usually this is a sign that the foreign key constraints have not been set up in the database. Can you confirm that the relationships are in fact in the database and not just the column names that represent the foreign keys?

Josh
  • 10,352
  • 12
  • 58
  • 109
  • Yes, all the foreign keys are created in database, when I see the database diagram in the managment studio, all the relationships are shown. I also checked the creation script of the database and all the relationships are there – Miguel Apr 18 '12 at 17:05
0

This may also happen if you don't have your Primary keys set up in your related tables. I created an edm with entity framework version 6 in VS 2013 against SQL Server 2008 R2. The relationships were set up in the database but were not showing up in the EDM for a couple tables. Once I set the Primary key fields as Primary Keys in SQL Server, the EDM updated correctly.

Tony L.
  • 17,638
  • 8
  • 69
  • 66
0

You can add a new diagram in yout "Model Browser" and add the Associations, you have to click with the right button of your mouse and select "Add to Diagram". After that the tables and the relationships will be shown in the model.