utilizar try tipos que propiedades personalizadas para manejo manejar las lanzar existen excepciones excepcion errores error ejemplos como catch capturar cachar c# entity-framework orm

try - propiedades de las excepciones en c#



ADO EF-Errores asociando asociaciones entre tipos derivados en TPH (3)

Fondo

Estoy escribiendo una biblioteca de acceso a datos usando ADO Entity Framework en Visual Studio 2008 SP1 usando .NET Framework 3.5 SP1. Intento crear asociaciones entre dos entidades que se derivan de un tipo abstracto. Represento ambas jerarquías de herencia de entidades usando Table Per Hierarchy (TPH), lo que significa que solo hay dos tablas, una para cada jerarquía de herencia de entidades.

NOTA Puede usar Table Per Type (TPT) para evitar este problema, pero viene con sus propios inconvenientes. Consulte aquí y aquí para obtener más detalles al elegir entre modelos de persistencia de herencia.

Aquí hay una captura de pantalla de la vista del Diseñador del Modelo de Entidad:

Vista de diseño del modelo de entidad en Visual Studio 2008 SP1 http://img153.imageshack.us/img153/6488/adoefgraphexamplescreenxz0.th.png

Y aquí hay una captura de pantalla del esquema de la base de datos:

Esquema de la base de datos http://img7.imageshack.us/img7/8607/graphexampledatabaseschik0.th.png

Suposiciones

Cuando crea asociaciones en ADO Entity Framework Designer entre tipos derivados modelados con TPH utilizando Visual Studio 2008 SP1 y .NET Framework 3.5 SP1, es probable que reciba el siguiente mensaje: "Error 3034: dos entidades con claves diferentes se asignan a la misma Asegúrese de que estos dos fragmentos de mapeo no mapeen dos grupos de entidades con claves superpuestas al mismo grupo de filas ".

En función de lo que he leído en línea , para resolver este problema, debe agregar una condición a la asociación en la clave externa, como sigue:

<Condition ColumnName="Endpoint1" IsNull="false" />

Aquí hay una captura de pantalla de esta edición para la asociación PersonPersonToPerson1:

Edición de Visual Studio 2008 SP1 de GraphModel.edmx http://img153.imageshack.us/img153/2504/edmxassociationsetmappiah8.th.png

Restricciones

  • Las clases base de cada jerarquía (es decir, nodo y enlace) deben ser abstractas.
  • Las propiedades de navegación de las asociaciones entre dos tipos derivados deben distinguirse por tipo de enlace (por ejemplo, PersonToPerson y PersonToLocation). Esto significa que no puede crear asociaciones entre las clases base abstractas Enlace y Nodo.

Problema:

Cuando creo el Modelo de Entidad como se describe arriba y agrego las Condiciones a las Asignaciones de Asociación como se describe en las Suposiciones anteriores, recibo un "Error 3023" cuando construyo / valido el modelo.

Error 1 Error 3023: Problem in Mapping Fragments starting at lines 146, 153, 159, 186, 195, 204, 213: Column Link.Endpoint1 has no default value and is not nullable. A column value is required to store entity data. An Entity with Key (PK) will not round-trip when: ((PK is NOT in ''LinkSet'' EntitySet OR PK does NOT play Role ''PersonToPerson'' in AssociationSet ''PersonPersonToPerson1'') AND (PK is in ''LinkSet'' EntitySet OR PK plays Role ''PersonToPerson'' in AssociationSet ''PersonPersonToPerson1'' OR PK plays Role ''PersonToPerson'' in AssociationSet ''PersonPersonToPerson'')) C:/Documents and Settings/Demo/My Documents/Visual Studio 2008/Projects/GraphExample2.BusinessEntities/GraphExample2.BusinessEntities/GraphModel.edmx 147 15 GraphExample2.BusinessEntities

Lo que el Entity Framework se está colgando en el escenario anterior es que hay dos propiedades que se asignan a las mismas claves foráneas. Por ejemplo, la columna y la clave externa para Endpoint1 se asignan a la propiedad Person en el tipo derivado PersonToLocation y se asignan a la propiedad Leader en el tipo derivado PersonToPerson.

No entiendo por qué esto es un problema. Dado que las propiedades Leader / Follower solo están en el tipo derivado PersonToPerson - no en ningún otro tipo derivado o tipo base - y lo mismo es cierto para la propiedad Person / Location, ¿por qué el campo TypeDiscriminator no es suficiente para que el EF determine qué establecer una fila dada pertenece?

Para mí, parece que si estás tratando con un objeto donde TypeDiscriminator = 1, colocas Endpoint1 en Leader y Endpoint2 en Follower. Del mismo modo, si está tratando con un objeto donde TypeDiscriminator = 2, coloca Endpoint1 en Person y Endpoint2 en Location.

Pregunta:

¿Cómo se resuelve el error 3023 para permitir que ocurran estas asociaciones?

O

¿Cómo se crea el tipo de asociaciones en ADO Entity Framework que he descrito anteriormente?

Referencias

Código

SQL:

USE [GraphExample2] GO /****** Object: Table [dbo].[Node] Script Date: 02/17/2009 14:36:13 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Node]( [NodeID] [int] NOT NULL, [NodeTypeDiscriminator] [int] NOT NULL, [Name] [varchar](255) NOT NULL, [Description] [varchar](1023) NULL, CONSTRAINT [PK_Node] PRIMARY KEY CLUSTERED ( [NodeID] 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 /****** Object: Table [dbo].[Link] Script Date: 02/17/2009 14:36:12 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Link]( [LinkID] [int] NOT NULL, [LinkTypeDiscriminator] [int] NOT NULL, [Endpoint1] [int] NOT NULL, [Endpoint2] [int] NOT NULL, [Name] [varchar](255) NULL, [Description] [varchar](1023) NULL, CONSTRAINT [PK_Link] PRIMARY KEY CLUSTERED ( [LinkID] 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 /****** Object: ForeignKey [FK_Link_Node_Endpoint1] Script Date: 02/17/2009 14:36:12 ******/ ALTER TABLE [dbo].[Link] WITH CHECK ADD CONSTRAINT [FK_Link_Node_Endpoint1] FOREIGN KEY([Endpoint1]) REFERENCES [dbo].[Node] ([NodeID]) GO ALTER TABLE [dbo].[Link] CHECK CONSTRAINT [FK_Link_Node_Endpoint1] GO /****** Object: ForeignKey [FK_Link_Node_Endpoint2] Script Date: 02/17/2009 14:36:12 ******/ ALTER TABLE [dbo].[Link] WITH CHECK ADD CONSTRAINT [FK_Link_Node_Endpoint2] FOREIGN KEY([Endpoint2]) REFERENCES [dbo].[Node] ([NodeID]) GO ALTER TABLE [dbo].[Link] CHECK CONSTRAINT [FK_Link_Node_Endpoint2] GO

EDMX:

<?xml version="1.0" encoding="utf-8"?> <edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx"> <!-- EF Runtime content --> <edmx:Runtime> <!-- SSDL content --> <edmx:StorageModels> <Schema Namespace="GraphModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl"> <EntityContainer Name="GraphModelStoreContainer"> <EntitySet Name="Link" EntityType="GraphModel.Store.Link" store:Type="Tables" Schema="dbo" /> <EntitySet Name="Node" EntityType="GraphModel.Store.Node" store:Type="Tables" Schema="dbo" /> <AssociationSet Name="FK_Link_Node_Endpoint1" Association="GraphModel.Store.FK_Link_Node_Endpoint1"> <End Role="Node" EntitySet="Node" /> <End Role="Link" EntitySet="Link" /> </AssociationSet> <AssociationSet Name="FK_Link_Node_Endpoint2" Association="GraphModel.Store.FK_Link_Node_Endpoint2"> <End Role="Node" EntitySet="Node" /> <End Role="Link" EntitySet="Link" /> </AssociationSet> </EntityContainer> <EntityType Name="Link"> <Key> <PropertyRef Name="LinkID" /> </Key> <Property Name="LinkID" Type="int" Nullable="false" /> <Property Name="LinkTypeDiscriminator" Type="int" Nullable="false" /> <Property Name="Endpoint1" Type="int" Nullable="false" /> <Property Name="Endpoint2" Type="int" Nullable="false" /> <Property Name="Name" Type="varchar" MaxLength="255" /> <Property Name="Description" Type="varchar" MaxLength="1023" /> </EntityType> <EntityType Name="Node"> <Key> <PropertyRef Name="NodeID" /> </Key> <Property Name="NodeID" Type="int" Nullable="false" /> <Property Name="NodeTypeDiscriminator" Type="int" Nullable="false" /> <Property Name="Name" Type="varchar" Nullable="false" MaxLength="255" /> <Property Name="Description" Type="varchar" MaxLength="1023" /> </EntityType> <Association Name="FK_Link_Node_Endpoint1"> <End Role="Node" Type="GraphModel.Store.Node" Multiplicity="1" /> <End Role="Link" Type="GraphModel.Store.Link" Multiplicity="*" /> <ReferentialConstraint> <Principal Role="Node"> <PropertyRef Name="NodeID" /> </Principal> <Dependent Role="Link"> <PropertyRef Name="Endpoint1" /> </Dependent> </ReferentialConstraint> </Association> <Association Name="FK_Link_Node_Endpoint2"> <End Role="Node" Type="GraphModel.Store.Node" Multiplicity="1" /> <End Role="Link" Type="GraphModel.Store.Link" Multiplicity="*" /> <ReferentialConstraint> <Principal Role="Node"> <PropertyRef Name="NodeID" /> </Principal> <Dependent Role="Link"> <PropertyRef Name="Endpoint2" /> </Dependent> </ReferentialConstraint> </Association> </Schema> </edmx:StorageModels> <!-- CSDL content --> <edmx:ConceptualModels> <Schema xmlns="http://schemas.microsoft.com/ado/2006/04/edm" Namespace="GraphModel" Alias="Self"> <EntityContainer Name="GraphModelContainer" > <EntitySet Name="NodeSet" EntityType="GraphModel.Node" /> <EntitySet Name="LinkSet" EntityType="GraphModel.Link" /> <AssociationSet Name="PersonPersonToPerson" Association="GraphModel.PersonPersonToPerson"> <End Role="Person" EntitySet="NodeSet" /> <End Role="PersonToPerson" EntitySet="LinkSet" /> </AssociationSet> <AssociationSet Name="PersonPersonToPerson1" Association="GraphModel.PersonPersonToPerson1"> <End Role="Person" EntitySet="NodeSet" /> <End Role="PersonToPerson" EntitySet="LinkSet" /> </AssociationSet> <AssociationSet Name="Person_PersonToLocation" Association="GraphModel.Person_PersonToLocation"> <End Role="Person" EntitySet="NodeSet" /> <End Role="PersonToLocation" EntitySet="LinkSet" /> </AssociationSet> <AssociationSet Name="Location_PersonToLocation" Association="GraphModel.Location_PersonToLocation"> <End Role="Location" EntitySet="NodeSet" /> <End Role="PersonToLocation" EntitySet="LinkSet" /> </AssociationSet> </EntityContainer> <EntityType Name="Node" Abstract="true"> <Key> <PropertyRef Name="NodeId" /> </Key> <Property Name="NodeId" Type="Int32" Nullable="false" /> <Property Name="Name" Type="String" Nullable="false" /> <Property Name="Description" Type="String" Nullable="true" /> </EntityType> <EntityType Name="Person" BaseType="GraphModel.Node" > <NavigationProperty Name="Leaders" Relationship="GraphModel.PersonPersonToPerson" FromRole="Person" ToRole="PersonToPerson" /> <NavigationProperty Name="Followers" Relationship="GraphModel.PersonPersonToPerson1" FromRole="Person" ToRole="PersonToPerson" /> <NavigationProperty Name="Locations" Relationship="GraphModel.Person_PersonToLocation" FromRole="Person" ToRole="PersonToLocation" /> </EntityType> <EntityType Name="Location" BaseType="GraphModel.Node" > <NavigationProperty Name="Visitors" Relationship="GraphModel.Location_PersonToLocation" FromRole="Location" ToRole="PersonToLocation" /> </EntityType> <EntityType Name="Link" Abstract="true"> <Key> <PropertyRef Name="LinkId" /> </Key> <Property Name="LinkId" Type="Int32" Nullable="false" /> <Property Name="Name" Type="String" Nullable="true" /> <Property Name="Description" Type="String" Nullable="true" /> </EntityType> <EntityType Name="PersonToPerson" BaseType="GraphModel.Link" > <NavigationProperty Name="Leader" Relationship="GraphModel.PersonPersonToPerson" FromRole="PersonToPerson" ToRole="Person" /> <NavigationProperty Name="Follower" Relationship="GraphModel.PersonPersonToPerson1" FromRole="PersonToPerson" ToRole="Person" /> </EntityType> <EntityType Name="PersonToLocation" BaseType="GraphModel.Link" > <NavigationProperty Name="Person" Relationship="GraphModel.Person_PersonToLocation" FromRole="PersonToLocation" ToRole="Person" /> <NavigationProperty Name="Location" Relationship="GraphModel.Location_PersonToLocation" FromRole="PersonToLocation" ToRole="Location" /> </EntityType> <Association Name="PersonPersonToPerson"> <End Type="GraphModel.Person" Role="Person" Multiplicity="1" /> <End Type="GraphModel.PersonToPerson" Role="PersonToPerson" Multiplicity="*" /> </Association> <Association Name="PersonPersonToPerson1"> <End Type="GraphModel.Person" Role="Person" Multiplicity="1" /> <End Type="GraphModel.PersonToPerson" Role="PersonToPerson" Multiplicity="*" /> </Association> <Association Name="Person_PersonToLocation"> <End Type="GraphModel.Person" Role="Person" Multiplicity="1" /> <End Type="GraphModel.PersonToLocation" Role="PersonToLocation" Multiplicity="*" /> </Association> <Association Name="Location_PersonToLocation"> <End Type="GraphModel.Location" Role="Location" Multiplicity="1" /> <End Type="GraphModel.PersonToLocation" Role="PersonToLocation" Multiplicity="*" /> </Association> </Schema> </edmx:ConceptualModels> <!-- C-S mapping content --> <edmx:Mappings> <Mapping xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS" Space="C-S"> <Alias Key="Model" Value="GraphModel" /> <Alias Key="Target" Value="GraphModel.Store" /> <EntityContainerMapping CdmEntityContainer="GraphModelContainer" StorageEntityContainer="GraphModelStoreContainer"> <EntitySetMapping Name="LinkSet"> <EntityTypeMapping TypeName="IsTypeOf(GraphModel.Link)"> <MappingFragment StoreEntitySet="Link"> <ScalarProperty Name="Description" ColumnName="Description" /> <ScalarProperty Name="Name" ColumnName="Name" /> <ScalarProperty Name="LinkId" ColumnName="LinkID" /> </MappingFragment> </EntityTypeMapping> <EntityTypeMapping TypeName="IsTypeOf(GraphModel.PersonToPerson)"> <MappingFragment StoreEntitySet="Link" > <ScalarProperty Name="LinkId" ColumnName="LinkID" /> <Condition ColumnName="LinkTypeDiscriminator" Value="1" /> </MappingFragment> </EntityTypeMapping> <EntityTypeMapping TypeName="IsTypeOf(GraphModel.PersonToLocation)"> <MappingFragment StoreEntitySet="Link" > <ScalarProperty Name="LinkId" ColumnName="LinkID" /> <Condition ColumnName="LinkTypeDiscriminator" Value="2" /> </MappingFragment> </EntityTypeMapping> </EntitySetMapping> <EntitySetMapping Name="NodeSet"> <EntityTypeMapping TypeName="IsTypeOf(GraphModel.Node)"> <MappingFragment StoreEntitySet="Node"> <ScalarProperty Name="Description" ColumnName="Description" /> <ScalarProperty Name="Name" ColumnName="Name" /> <ScalarProperty Name="NodeId" ColumnName="NodeID" /> </MappingFragment> </EntityTypeMapping> <EntityTypeMapping TypeName="IsTypeOf(GraphModel.Person)"> <MappingFragment StoreEntitySet="Node" > <ScalarProperty Name="NodeId" ColumnName="NodeID" /> <Condition ColumnName="NodeTypeDiscriminator" Value="1" /> </MappingFragment> </EntityTypeMapping> <EntityTypeMapping TypeName="IsTypeOf(GraphModel.Location)"> <MappingFragment StoreEntitySet="Node" > <ScalarProperty Name="NodeId" ColumnName="NodeID" /> <Condition ColumnName="NodeTypeDiscriminator" Value="2" /> </MappingFragment> </EntityTypeMapping> </EntitySetMapping> <AssociationSetMapping Name="PersonPersonToPerson1" TypeName="GraphModel.PersonPersonToPerson1" StoreEntitySet="Link"> <EndProperty Name="Person"> <ScalarProperty Name="NodeId" ColumnName="Endpoint1" /> </EndProperty> <EndProperty Name="PersonToPerson"> <ScalarProperty Name="LinkId" ColumnName="LinkID" /> </EndProperty> <Condition ColumnName="Endpoint1" IsNull="false" /> </AssociationSetMapping> <AssociationSetMapping Name="PersonPersonToPerson" TypeName="GraphModel.PersonPersonToPerson" StoreEntitySet="Link"> <EndProperty Name="Person"> <ScalarProperty Name="NodeId" ColumnName="Endpoint2" /> </EndProperty> <EndProperty Name="PersonToPerson"> <ScalarProperty Name="LinkId" ColumnName="LinkID" /> </EndProperty> <Condition ColumnName="Endpoint2" IsNull="false" /> </AssociationSetMapping> <AssociationSetMapping Name="Person_PersonToLocation" TypeName="GraphModel.Person_PersonToLocation" StoreEntitySet="Link"> <EndProperty Name="Person"> <ScalarProperty Name="NodeId" ColumnName="Endpoint1" /> </EndProperty> <EndProperty Name="PersonToLocation"> <ScalarProperty Name="LinkId" ColumnName="LinkID" /> </EndProperty> <Condition ColumnName="Endpoint1" IsNull="false" /> </AssociationSetMapping> <AssociationSetMapping Name="Location_PersonToLocation" TypeName="GraphModel.Location_PersonToLocation" StoreEntitySet="Link"> <EndProperty Name="Location"> <ScalarProperty Name="NodeId" ColumnName="Endpoint2" /> </EndProperty> <EndProperty Name="PersonToLocation"> <ScalarProperty Name="LinkId" ColumnName="LinkID" /> </EndProperty> <Condition ColumnName="Endpoint2" IsNull="false" /> </AssociationSetMapping> </EntityContainerMapping> </Mapping> </edmx:Mappings> </edmx:Runtime> <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) --> <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx"> <edmx:Connection> <DesignerInfoPropertySet> <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" /> </DesignerInfoPropertySet> </edmx:Connection> <edmx:Options> <DesignerInfoPropertySet> <DesignerProperty Name="ValidateOnBuild" Value="true" /> </DesignerInfoPropertySet> </edmx:Options> <!-- Diagram content (shape and connector positions) --> <edmx:Diagrams> <Diagram Name="GraphModel" ZoomLevel="114" > <EntityTypeShape EntityType="GraphModel.Node" Width="1.5" PointX="5.875" PointY="1.375" Height="1.427958984375" /> <EntityTypeShape EntityType="GraphModel.Person" Width="1.5" PointX="5.875" PointY="3.25" Height="1.4279589843749996" /> <EntityTypeShape EntityType="GraphModel.Location" Width="1.5" PointX="7.75" PointY="4.625" Height="1.0992643229166665" /> <InheritanceConnector EntityType="GraphModel.Location"> <ConnectorPoint PointX="7.375" PointY="2.0889794921875" /> <ConnectorPoint PointX="8.5" PointY="2.0889794921875" /> <ConnectorPoint PointX="8.5" PointY="4.625" /> </InheritanceConnector> <EntityTypeShape EntityType="GraphModel.Link" Width="1.5" PointX="2.875" PointY="1.375" Height="1.427958984375" /> <EntityTypeShape EntityType="GraphModel.PersonToPerson" Width="1.75" PointX="2.625" PointY="3.125" Height="0.9349169921875" /> <InheritanceConnector EntityType="GraphModel.PersonToPerson"> <ConnectorPoint PointX="3.625" PointY="2.802958984375" /> <ConnectorPoint PointX="3.625" PointY="3.125" /> </InheritanceConnector> <InheritanceConnector EntityType="GraphModel.Person"> <ConnectorPoint PointX="6.625" PointY="2.802958984375" /> <ConnectorPoint PointX="6.625" PointY="3.25" /> </InheritanceConnector> <EntityTypeShape EntityType="GraphModel.PersonToLocation" Width="1.875" PointX="0.75" PointY="4.625" Height="1.2636116536458326" /> <InheritanceConnector EntityType="GraphModel.PersonToLocation"> <ConnectorPoint PointX="2.875" PointY="2.0889794921875" /> <ConnectorPoint PointX="1.65625" PointY="2.0889794921875" /> <ConnectorPoint PointX="1.65625" PointY="4.625" /> </InheritanceConnector> <AssociationConnector Association="GraphModel.PersonPersonToPerson"> <ConnectorPoint PointX="5.875" PointY="3.8193058268229163" /> <ConnectorPoint PointX="4.375" PointY="3.8193058268229163" /> </AssociationConnector> <AssociationConnector Association="GraphModel.PersonPersonToPerson1"> <ConnectorPoint PointX="5.875" PointY="3.4721529134114579" /> <ConnectorPoint PointX="4.375" PointY="3.4721529134114579" /> </AssociationConnector> <AssociationConnector Association="GraphModel.Person_PersonToLocation"> <ConnectorPoint PointX="6.625" PointY="4.677958984375" /> <ConnectorPoint PointX="6.625" PointY="5.1875" /> <ConnectorPoint PointX="2.625" PointY="5.1875" /> </AssociationConnector> <AssociationConnector Association="GraphModel.Location_PersonToLocation"> <ConnectorPoint PointX="7.75" PointY="5.4791666666666661" /> <ConnectorPoint PointX="2.625" PointY="5.4791666666666661" /> </AssociationConnector> </Diagram> </edmx:Diagrams> </edmx:Designer> </edmx:Edmx>


Posible solución

  1. Cree una columna separada para cada asociación entre tipos derivados y haga que cada una de estas columnas sea nulable
  2. Cree una clave externa entre cada una de estas nuevas columnas y la tabla de clave principal.
  3. Asigne cada asociación en su Modelo de entidad a una columna única y específica y una clave externa para que cada columna y clave externa solo se utilicen una vez.

Problemas

Esta es una solución bastante indeseable porque hace explotar la cantidad de columnas que necesita.

  • Más columnas : al agregar una columna para cada asociación entre los tipos derivados, se produce una explosión en el número de columnas.
  • Columnas vacías En el caso de TPH, significa que tendrá muchas columnas vacías en su tabla.
  • SQL JOIN - Cambiar de TPH a TPT para evitar el número de columnas vacías da como resultado la necesidad de que EF use un JOIN que tendrá que ocurrir con mucha frecuencia (casi cada vez que trabajes con cualquiera de los tipos derivados).
  • Refactorización Si agrega un tipo derivado en el futuro, no solo tiene que actualizar su modelo de Entidad (* .edmx) y su asignación, sino que también deberá cambiar el esquema de la base de datos agregando columnas adicionales.

Ejemplo

Para el ejemplo de Enlace / Nodo anterior, el esquema resultante de la base de datos se vería así:

GraphExample Solución de esquema de base de datos http://img230.imageshack.us/img230/1628/graphexampledatabasewor.th.png

Código

SQL:

USE [GraphExample2] GO /****** Object: Table [dbo].[Node] Script Date: 02/26/2009 15:45:53 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Node]( [NodeID] [int] IDENTITY(1,1) NOT NULL, [NodeTypeDiscriminator] [int] NOT NULL, [Name] [varchar](255) NOT NULL, [Description] [varchar](1023) NULL, CONSTRAINT [PK_Node] PRIMARY KEY CLUSTERED ( [NodeID] 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 /****** Object: Table [dbo].[Link] Script Date: 02/26/2009 15:45:53 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[Link]( [LinkID] [int] IDENTITY(1,1) NOT NULL, [LinkTypeDiscriminator] [int] NOT NULL, [LeaderID] [int] NULL, [FollowerID] [int] NULL, [PersonID] [int] NULL, [LocationID] [int] NULL, [Name] [varchar](255) NULL, [Description] [varchar](1023) NULL, CONSTRAINT [PK_Link] PRIMARY KEY CLUSTERED ( [LinkID] 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 /****** Object: ForeignKey [FK_Link_Node_Follower] Script Date: 02/26/2009 15:45:53 ******/ ALTER TABLE [dbo].[Link] WITH CHECK ADD CONSTRAINT [FK_Link_Node_Follower] FOREIGN KEY([FollowerID]) REFERENCES [dbo].[Node] ([NodeID]) GO ALTER TABLE [dbo].[Link] CHECK CONSTRAINT [FK_Link_Node_Follower] GO /****** Object: ForeignKey [FK_Link_Node_Leader] Script Date: 02/26/2009 15:45:53 ******/ ALTER TABLE [dbo].[Link] WITH CHECK ADD CONSTRAINT [FK_Link_Node_Leader] FOREIGN KEY([LeaderID]) REFERENCES [dbo].[Node] ([NodeID]) GO ALTER TABLE [dbo].[Link] CHECK CONSTRAINT [FK_Link_Node_Leader] GO /****** Object: ForeignKey [FK_Link_Node_Location] Script Date: 02/26/2009 15:45:53 ******/ ALTER TABLE [dbo].[Link] WITH CHECK ADD CONSTRAINT [FK_Link_Node_Location] FOREIGN KEY([LocationID]) REFERENCES [dbo].[Node] ([NodeID]) GO ALTER TABLE [dbo].[Link] CHECK CONSTRAINT [FK_Link_Node_Location] GO /****** Object: ForeignKey [FK_Link_Node_Person] Script Date: 02/26/2009 15:45:53 ******/ ALTER TABLE [dbo].[Link] WITH CHECK ADD CONSTRAINT [FK_Link_Node_Person] FOREIGN KEY([PersonID]) REFERENCES [dbo].[Node] ([NodeID]) GO ALTER TABLE [dbo].[Link] CHECK CONSTRAINT [FK_Link_Node_Person] GO

EDMX:

<?xml version="1.0" encoding="utf-8"?> <edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx"> <!-- EF Runtime content --> <edmx:Runtime> <!-- SSDL content --> <edmx:StorageModels> <Schema Namespace="GraphModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2006/04/edm/ssdl"> <EntityContainer Name="GraphModelStoreContainer"> <EntitySet Name="Link" EntityType="GraphModel.Store.Link" store:Type="Tables" Schema="dbo" /> <EntitySet Name="Node" EntityType="GraphModel.Store.Node" store:Type="Tables" Schema="dbo" /> <AssociationSet Name="FK_Link_Node_Follower" Association="GraphModel.Store.FK_Link_Node_Follower"> <End Role="Node" EntitySet="Node" /> <End Role="Link" EntitySet="Link" /> </AssociationSet> <AssociationSet Name="FK_Link_Node_Leader" Association="GraphModel.Store.FK_Link_Node_Leader"> <End Role="Node" EntitySet="Node" /> <End Role="Link" EntitySet="Link" /> </AssociationSet> <AssociationSet Name="FK_Link_Node_Location" Association="GraphModel.Store.FK_Link_Node_Location"> <End Role="Node" EntitySet="Node" /> <End Role="Link" EntitySet="Link" /> </AssociationSet> <AssociationSet Name="FK_Link_Node_Person" Association="GraphModel.Store.FK_Link_Node_Person"> <End Role="Node" EntitySet="Node" /> <End Role="Link" EntitySet="Link" /> </AssociationSet> </EntityContainer> <EntityType Name="Link"> <Key> <PropertyRef Name="LinkID" /> </Key> <Property Name="LinkID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" /> <Property Name="LinkTypeDiscriminator" Type="int" Nullable="false" /> <Property Name="LeaderID" Type="int" /> <Property Name="FollowerID" Type="int" /> <Property Name="PersonID" Type="int" /> <Property Name="LocationID" Type="int" /> <Property Name="Name" Type="varchar" MaxLength="255" /> <Property Name="Description" Type="varchar" MaxLength="1023" /> </EntityType> <EntityType Name="Node"> <Key> <PropertyRef Name="NodeID" /> </Key> <Property Name="NodeID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" /> <Property Name="NodeTypeDiscriminator" Type="int" Nullable="false" /> <Property Name="Name" Type="varchar" Nullable="false" MaxLength="255" /> <Property Name="Description" Type="varchar" MaxLength="1023" /> </EntityType> <Association Name="FK_Link_Node_Follower"> <End Role="Node" Type="GraphModel.Store.Node" Multiplicity="0..1" /> <End Role="Link" Type="GraphModel.Store.Link" Multiplicity="*" /> <ReferentialConstraint> <Principal Role="Node"> <PropertyRef Name="NodeID" /> </Principal> <Dependent Role="Link"> <PropertyRef Name="FollowerID" /> </Dependent> </ReferentialConstraint> </Association> <Association Name="FK_Link_Node_Leader"> <End Role="Node" Type="GraphModel.Store.Node" Multiplicity="0..1" /> <End Role="Link" Type="GraphModel.Store.Link" Multiplicity="*" /> <ReferentialConstraint> <Principal Role="Node"> <PropertyRef Name="NodeID" /> </Principal> <Dependent Role="Link"> <PropertyRef Name="LeaderID" /> </Dependent> </ReferentialConstraint> </Association> <Association Name="FK_Link_Node_Location"> <End Role="Node" Type="GraphModel.Store.Node" Multiplicity="0..1" /> <End Role="Link" Type="GraphModel.Store.Link" Multiplicity="*" /> <ReferentialConstraint> <Principal Role="Node"> <PropertyRef Name="NodeID" /> </Principal> <Dependent Role="Link"> <PropertyRef Name="LocationID" /> </Dependent> </ReferentialConstraint> </Association> <Association Name="FK_Link_Node_Person"> <End Role="Node" Type="GraphModel.Store.Node" Multiplicity="0..1" /> <End Role="Link" Type="GraphModel.Store.Link" Multiplicity="*" /> <ReferentialConstraint> <Principal Role="Node"> <PropertyRef Name="NodeID" /> </Principal> <Dependent Role="Link"> <PropertyRef Name="PersonID" /> </Dependent> </ReferentialConstraint> </Association> </Schema> </edmx:StorageModels> <!-- CSDL content --> <edmx:ConceptualModels> <Schema xmlns="http://schemas.microsoft.com/ado/2006/04/edm" Namespace="GraphModel" Alias="Self"> <EntityContainer Name="GraphModelContainer" > <EntitySet Name="NodeSet" EntityType="GraphModel.Node" /> <EntitySet Name="LinkSet" EntityType="GraphModel.Link" /> <AssociationSet Name="PersonPersonToPerson_Leader" Association="GraphModel.PersonPersonToPerson_Leader"> <End Role="Person" EntitySet="NodeSet" /> <End Role="PersonToPerson" EntitySet="LinkSet" /> </AssociationSet> <AssociationSet Name="PersonPersonToPerson_Follower" Association="GraphModel.PersonPersonToPerson_Follower"> <End Role="Person" EntitySet="NodeSet" /> <End Role="PersonToPerson" EntitySet="LinkSet" /> </AssociationSet> <AssociationSet Name="Person_PersonToLocation" Association="GraphModel.Person_PersonToLocation"> <End Role="Person" EntitySet="NodeSet" /> <End Role="PersonToLocation" EntitySet="LinkSet" /> </AssociationSet> <AssociationSet Name="Location_PersonToLocation" Association="GraphModel.Location_PersonToLocation"> <End Role="Location" EntitySet="NodeSet" /> <End Role="PersonToLocation" EntitySet="LinkSet" /> </AssociationSet> </EntityContainer> <EntityType Name="Node" Abstract="true"> <Key> <PropertyRef Name="NodeId" /> </Key> <Property Name="NodeId" Type="Int32" Nullable="false" /> <Property Name="Name" Type="String" Nullable="false" /> <Property Name="Description" Type="String" Nullable="true" /> </EntityType> <EntityType Name="Person" BaseType="GraphModel.Node" > <NavigationProperty Name="Leaders" Relationship="GraphModel.PersonPersonToPerson_Leader" FromRole="Person" ToRole="PersonToPerson" /> <NavigationProperty Name="Followers" Relationship="GraphModel.PersonPersonToPerson_Follower" FromRole="Person" ToRole="PersonToPerson" /> <NavigationProperty Name="Locations" Relationship="GraphModel.Person_PersonToLocation" FromRole="Person" ToRole="PersonToLocation" /> </EntityType> <EntityType Name="Location" BaseType="GraphModel.Node" > <NavigationProperty Name="Visitors" Relationship="GraphModel.Location_PersonToLocation" FromRole="Location" ToRole="PersonToLocation" /> </EntityType> <EntityType Name="Link" Abstract="true"> <Key> <PropertyRef Name="LinkId" /> </Key> <Property Name="LinkId" Type="Int32" Nullable="false" /> <Property Name="Name" Type="String" Nullable="true" /> <Property Name="Description" Type="String" Nullable="true" /> </EntityType> <EntityType Name="PersonToPerson" BaseType="GraphModel.Link" > <NavigationProperty Name="Leader" Relationship="GraphModel.PersonPersonToPerson_Leader" FromRole="PersonToPerson" ToRole="Person" /> <NavigationProperty Name="Follower" Relationship="GraphModel.PersonPersonToPerson_Follower" FromRole="PersonToPerson" ToRole="Person" /> </EntityType> <EntityType Name="PersonToLocation" BaseType="GraphModel.Link" > <NavigationProperty Name="Person" Relationship="GraphModel.Person_PersonToLocation" FromRole="PersonToLocation" ToRole="Person" /> <NavigationProperty Name="Location" Relationship="GraphModel.Location_PersonToLocation" FromRole="PersonToLocation" ToRole="Location" /> </EntityType> <Association Name="PersonPersonToPerson_Leader"> <End Type="GraphModel.Person" Role="Person" Multiplicity="1" /> <End Type="GraphModel.PersonToPerson" Role="PersonToPerson" Multiplicity="*" /> </Association> <Association Name="PersonPersonToPerson_Follower"> <End Type="GraphModel.Person" Role="Person" Multiplicity="1" /> <End Type="GraphModel.PersonToPerson" Role="PersonToPerson" Multiplicity="*" /> </Association> <Association Name="Person_PersonToLocation"> <End Type="GraphModel.Person" Role="Person" Multiplicity="1" /> <End Type="GraphModel.PersonToLocation" Role="PersonToLocation" Multiplicity="*" /> </Association> <Association Name="Location_PersonToLocation"> <End Type="GraphModel.Location" Role="Location" Multiplicity="1" /> <End Type="GraphModel.PersonToLocation" Role="PersonToLocation" Multiplicity="*" /> </Association> </Schema> </edmx:ConceptualModels> <!-- C-S mapping content --> <edmx:Mappings> <Mapping xmlns="urn:schemas-microsoft-com:windows:storage:mapping:CS" Space="C-S"> <Alias Key="Model" Value="GraphModel" /> <Alias Key="Target" Value="GraphModel.Store" /> <EntityContainerMapping CdmEntityContainer="GraphModelContainer" StorageEntityContainer="GraphModelStoreContainer"> <EntitySetMapping Name="LinkSet"> <EntityTypeMapping TypeName="IsTypeOf(GraphModel.Link)"> <MappingFragment StoreEntitySet="Link"> <ScalarProperty Name="Description" ColumnName="Description" /> <ScalarProperty Name="Name" ColumnName="Name" /> <ScalarProperty Name="LinkId" ColumnName="LinkID" /> </MappingFragment> </EntityTypeMapping> <EntityTypeMapping TypeName="IsTypeOf(GraphModel.PersonToPerson)"> <MappingFragment StoreEntitySet="Link" > <ScalarProperty Name="LinkId" ColumnName="LinkID" /> <Condition ColumnName="LinkTypeDiscriminator" Value="1" /> </MappingFragment> </EntityTypeMapping> <EntityTypeMapping TypeName="IsTypeOf(GraphModel.PersonToLocation)"> <MappingFragment StoreEntitySet="Link" > <ScalarProperty Name="LinkId" ColumnName="LinkID" /> <Condition ColumnName="LinkTypeDiscriminator" Value="2" /> </MappingFragment> </EntityTypeMapping> </EntitySetMapping> <EntitySetMapping Name="NodeSet"> <EntityTypeMapping TypeName="IsTypeOf(GraphModel.Node)"> <MappingFragment StoreEntitySet="Node"> <ScalarProperty Name="Description" ColumnName="Description" /> <ScalarProperty Name="Name" ColumnName="Name" /> <ScalarProperty Name="NodeId" ColumnName="NodeID" /> </MappingFragment> </EntityTypeMapping> <EntityTypeMapping TypeName="IsTypeOf(GraphModel.Person)"> <MappingFragment StoreEntitySet="Node" > <ScalarProperty Name="NodeId" ColumnName="NodeID" /> <Condition ColumnName="NodeTypeDiscriminator" Value="1" /> </MappingFragment> </EntityTypeMapping> <EntityTypeMapping TypeName="IsTypeOf(GraphModel.Location)"> <MappingFragment StoreEntitySet="Node" > <ScalarProperty Name="NodeId" ColumnName="NodeID" /> <Condition ColumnName="NodeTypeDiscriminator" Value="2" /> </MappingFragment> </EntityTypeMapping> </EntitySetMapping> <AssociationSetMapping Name="PersonPersonToPerson_Follower" TypeName="GraphModel.PersonPersonToPerson_Follower" StoreEntitySet="Link"> <EndProperty Name="Person"> <ScalarProperty Name="NodeId" ColumnName="FollowerID" /> </EndProperty> <EndProperty Name="PersonToPerson"> <ScalarProperty Name="LinkId" ColumnName="LinkID" /> </EndProperty> </AssociationSetMapping> <AssociationSetMapping Name="PersonPersonToPerson_Leader" TypeName="GraphModel.PersonPersonToPerson_Leader" StoreEntitySet="Link"> <EndProperty Name="Person"> <ScalarProperty Name="NodeId" ColumnName="LeaderID" /> </EndProperty> <EndProperty Name="PersonToPerson"> <ScalarProperty Name="LinkId" ColumnName="LinkID" /> </EndProperty> </AssociationSetMapping> <AssociationSetMapping Name="Person_PersonToLocation" TypeName="GraphModel.Person_PersonToLocation" StoreEntitySet="Link"> <EndProperty Name="Person"> <ScalarProperty Name="NodeId" ColumnName="PersonID" /> </EndProperty> <EndProperty Name="PersonToLocation"> <ScalarProperty Name="LinkId" ColumnName="LinkID" /> </EndProperty> </AssociationSetMapping> <AssociationSetMapping Name="Location_PersonToLocation" TypeName="GraphModel.Location_PersonToLocation" StoreEntitySet="Link"> <EndProperty Name="Location"> <ScalarProperty Name="NodeId" ColumnName="LocationID" /> </EndProperty> <EndProperty Name="PersonToLocation"> <ScalarProperty Name="LinkId" ColumnName="LinkID" /> </EndProperty> </AssociationSetMapping> </EntityContainerMapping> </Mapping> </edmx:Mappings> </edmx:Runtime> <!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) --> <edmx:Designer xmlns="http://schemas.microsoft.com/ado/2007/06/edmx"> <edmx:Connection> <DesignerInfoPropertySet> <DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" /> </DesignerInfoPropertySet> </edmx:Connection> <edmx:Options> <DesignerInfoPropertySet> <DesignerProperty Name="ValidateOnBuild" Value="true" /> </DesignerInfoPropertySet> </edmx:Options> <!-- Diagram content (shape and connector positions) --> <edmx:Diagrams> <Diagram Name="GraphModel" ZoomLevel="114" > <EntityTypeShape EntityType="GraphModel.Node" Width="1.5" PointX="5.875" PointY="1.375" Height="1.427958984375" /> <EntityTypeShape EntityType="GraphModel.Person" Width="1.5" PointX="5.875" PointY="3.25" Height="1.4279589843749996" /> <EntityTypeShape EntityType="GraphModel.Location" Width="1.5" PointX="7.75" PointY="4.625" Height="1.0992643229166665" /> <InheritanceConnector EntityType="GraphModel.Location"> <ConnectorPoint PointX="7.375" PointY="2.4176741536458342" /> <ConnectorPoint PointX="8.5" PointY="2.4176741536458342" /> <ConnectorPoint PointX="8.5" PointY="4.625" /> </InheritanceConnector> <EntityTypeShape EntityType="GraphModel.Link" Width="1.5" PointX="2.875" PointY="1.375" Height="1.427958984375" /> <EntityTypeShape EntityType="GraphModel.PersonToPerson" Width="1.75" PointX="2.75" PointY="3.25" Height="1.2636116536458326" /> <InheritanceConnector EntityType="GraphModel.PersonToPerson" ManuallyRouted="false"> <ConnectorPoint PointX="3.625" PointY="2.802958984375" /> <ConnectorPoint PointX="3.625" PointY="3.25" /> </InheritanceConnector> <InheritanceConnector EntityType="GraphModel.Person"> <ConnectorPoint PointX="6.625" PointY="3.4603483072916683" /> <ConnectorPoint PointX="6.625" PointY="3.25" /> </InheritanceConnector> <EntityTypeShape EntityType="GraphModel.PersonToLocation" Width="1.875" PointX="0.75" PointY="4.625" Height="1.2636116536458326" /> <InheritanceConnector EntityType="GraphModel.PersonToLocation"> <ConnectorPoint PointX="2.875" PointY="2.4176741536458342" /> <ConnectorPoint PointX="1.65625" PointY="2.4176741536458342" /> <ConnectorPoint PointX="1.65625" PointY="4.625" /> </InheritanceConnector> <AssociationConnector Association="GraphModel.PersonPersonToPerson_Leader"> <ConnectorPoint PointX="5.875" PointY="3.8818058268229163" /> <ConnectorPoint PointX="4.5" PointY="3.8818058268229163" /> </AssociationConnector> <AssociationConnector Association="GraphModel.PersonPersonToPerson_Follower"> <ConnectorPoint PointX="5.875" PointY="3.5034029134114579" /> <ConnectorPoint PointX="4.5" PointY="3.5034029134114579" /> </AssociationConnector> <AssociationConnector Association="GraphModel.Person_PersonToLocation"> <ConnectorPoint PointX="6.625" PointY="4.677958984375" /> <ConnectorPoint PointX="6.625" PointY="5.0078214863281243" /> <ConnectorPoint PointX="2.625" PointY="5.0078214863281243" /> </AssociationConnector> <AssociationConnector Association="GraphModel.Location_PersonToLocation"> <ConnectorPoint PointX="7.75" PointY="5.40018798828125" /> <ConnectorPoint PointX="2.625" PointY="5.40018798828125" /> </AssociationConnector> </Diagram> </edmx:Diagrams> </edmx:Designer> </edmx:Edmx>


AoA!

EntityModelCodeGenerator crear 0..1 a muchos relación entre dos tablas, que sea 1 a muchos .

En este tipo de casos puede ser una posible solución.

¡Atentamente! Salahuddin


Tenía este error y señaló que una columna que suprimir en la base de datos no había sido eliminado en el modelo. Me sacó del modelo y el error fue. Esa era mi situación, sin embargo. Tal vez pueda ayudar a alguien.