Mi relación entre el padre y el hijo es que están conectados por un borde. Es similar a una estructura de gráfico dirigida.Agregar hijo al registro principal existente en el marco de la entidad
DAL:
public void SaveResource(Resource resource)
{
context.AddToResources(resource); //Should also add children.
context.SaveChanges();
}
public Resource GetResource(int resourceId)
{
var resource = (from r in context.Resources
.Include("ToEdges").Include("FromEdges")
where r.ResourceId == resourceId
select r).SingleOrDefault();
return resource;
}
Servicio:
public void AddChildResource(int parentResourceId, Resource childResource)
{
Resource parentResource = repository.GetResource(parentResourceId);
ResourceEdge inEdge = new ResourceEdge();
inEdge.ToResource = childResource;
parentResource.ToEdges.Add(inEdge);
repository.SaveResource(parentResource);
}
de error: Un objeto con la misma clave ya existe en la ObjectStateManager. El objeto existente está en el estado Sin cambios. Un objeto solo se puede agregar al ObjectStateManager nuevamente si está en el estado agregado.
Image:
alt text http://img100.imageshack.us/img100/374/entity.png
Me han dicho que esto es la secuencia en la presentación de un niño a un padre ya existente:
Get padres -> Adjuntar hijo a padre -> Solicitud de los padres .
Esa es la secuencia que utilicé. El código anterior se extrae de una aplicación ASP.NET MVC 2 utilizando el patrón de repositorio.
archivo EDMX:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="XDbModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
<EntityContainer Name="XDbModelStoreContainer">
<EntitySet Name="Bundles" EntityType="XDbModel.Store.Bundles" store:Type="Tables" Schema="dbo" />
<EntitySet Name="CellProviders" EntityType="XDbModel.Store.CellProviders" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Comments" EntityType="XDbModel.Store.Comments" store:Type="Tables" Schema="dbo" />
<EntitySet Name="LocationPoints" EntityType="XDbModel.Store.LocationPoints" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Locations" EntityType="XDbModel.Store.Locations" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Offers" EntityType="XDbModel.Store.Offers" store:Type="Tables" Schema="dbo" />
<EntitySet Name="PostBundleJunction" EntityType="XDbModel.Store.PostBundleJunction" store:Type="Tables" Schema="dbo" />
<EntitySet Name="PostMedia" EntityType="XDbModel.Store.PostMedia" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Posts" EntityType="XDbModel.Store.Posts" store:Type="Tables" Schema="dbo" />
<EntitySet Name="ResourceEdges" EntityType="XDbModel.Store.ResourceEdges" store:Type="Tables" Schema="dbo" />
<EntitySet Name="ResourceNames" EntityType="XDbModel.Store.ResourceNames" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Resources" EntityType="XDbModel.Store.Resources" store:Type="Tables" Schema="dbo" />
<EntitySet Name="sysdiagrams" EntityType="XDbModel.Store.sysdiagrams" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Users" EntityType="XDbModel.Store.Users" store:Type="Tables" Schema="dbo" />
<EntitySet Name="Votes" EntityType="XDbModel.Store.Votes" store:Type="Tables" Schema="dbo" />
<AssociationSet Name="FK_Comments_Offers" Association="XDbModel.Store.FK_Comments_Offers">
<End Role="Offers" EntitySet="Offers" />
<End Role="Comments" EntitySet="Comments" />
</AssociationSet>
<AssociationSet Name="FK_Comments_Users" Association="XDbModel.Store.FK_Comments_Users">
<End Role="Users" EntitySet="Users" />
<End Role="Comments" EntitySet="Comments" />
</AssociationSet>
<AssociationSet Name="FK_LocationPoints_Locations" Association="XDbModel.Store.FK_LocationPoints_Locations">
<End Role="Locations" EntitySet="Locations" />
<End Role="LocationPoints" EntitySet="LocationPoints" />
</AssociationSet>
<AssociationSet Name="FK_PostBundleJunction_Bundles" Association="XDbModel.Store.FK_PostBundleJunction_Bundles">
<End Role="Bundles" EntitySet="Bundles" />
<End Role="PostBundleJunction" EntitySet="PostBundleJunction" />
</AssociationSet>
<AssociationSet Name="FK_PostBundleJunction_Posts" Association="XDbModel.Store.FK_PostBundleJunction_Posts">
<End Role="Posts" EntitySet="Posts" />
<End Role="PostBundleJunction" EntitySet="PostBundleJunction" />
</AssociationSet>
<AssociationSet Name="FK_Posts_Locations" Association="XDbModel.Store.FK_Posts_Locations">
<End Role="Locations" EntitySet="Locations" />
<End Role="Posts" EntitySet="Posts" />
</AssociationSet>
<AssociationSet Name="FK_Posts_ResourceNames" Association="XDbModel.Store.FK_Posts_ResourceNames">
<End Role="ResourceNames" EntitySet="ResourceNames" />
<End Role="Posts" EntitySet="Posts" />
</AssociationSet>
<AssociationSet Name="FK_Posts_Users" Association="XDbModel.Store.FK_Posts_Users">
<End Role="Users" EntitySet="Users" />
<End Role="Posts" EntitySet="Posts" />
</AssociationSet>
<AssociationSet Name="FK_ResourceEdges_Resources" Association="XDbModel.Store.FK_ResourceEdges_Resources">
<End Role="Resources" EntitySet="Resources" />
<End Role="ResourceEdges" EntitySet="ResourceEdges" />
</AssociationSet>
<AssociationSet Name="FK_ResourceEdges_Resources1" Association="XDbModel.Store.FK_ResourceEdges_Resources1">
<End Role="Resources" EntitySet="Resources" />
<End Role="ResourceEdges" EntitySet="ResourceEdges" />
</AssociationSet>
<AssociationSet Name="FK_ResourceNames_Resources" Association="XDbModel.Store.FK_ResourceNames_Resources">
<End Role="Resources" EntitySet="Resources" />
<End Role="ResourceNames" EntitySet="ResourceNames" />
</AssociationSet>
<AssociationSet Name="FK_Users_Locations" Association="XDbModel.Store.FK_Users_Locations">
<End Role="Locations" EntitySet="Locations" />
<End Role="Users" EntitySet="Users" />
</AssociationSet>
<AssociationSet Name="FK_Votes_Posts" Association="XDbModel.Store.FK_Votes_Posts">
<End Role="Posts" EntitySet="Posts" />
<End Role="Votes" EntitySet="Votes" />
</AssociationSet>
</EntityContainer>
<EntityType Name="Bundles">
<Key>
<PropertyRef Name="BundleId" />
</Key>
<Property Name="BundleId" Type="int" Nullable="false" />
</EntityType>
<EntityType Name="CellProviders">
<Key>
<PropertyRef Name="CellProviderID" />
</Key>
<Property Name="CellProviderID" Type="uniqueidentifier" Nullable="false" />
</EntityType>
<EntityType Name="Comments">
<Key>
<PropertyRef Name="CommentID" />
</Key>
<Property Name="CommentID" Type="uniqueidentifier" Nullable="false" />
<Property Name="ParentPostID" Type="uniqueidentifier" />
<Property Name="OfferPostID" Type="uniqueidentifier" />
<Property Name="UserID" Type="uniqueidentifier" Nullable="false" />
</EntityType>
<EntityType Name="LocationPoints">
<Key>
<PropertyRef Name="LocationPointId" />
</Key>
<Property Name="LocationPointId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="Latitude" Type="float" Nullable="false" />
<Property Name="Longitude" Type="float" Nullable="false" />
<Property Name="Altitude" Type="float" Nullable="false" />
<Property Name="Count" Type="int" Nullable="false" />
<Property Name="LocationId" Type="int" Nullable="false" />
</EntityType>
<EntityType Name="Locations">
<Key>
<PropertyRef Name="LocationId" />
</Key>
<Property Name="LocationId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="Address1" Type="nvarchar" Nullable="false" MaxLength="200" />
<Property Name="Address2" Type="nvarchar" MaxLength="200" />
<Property Name="Address3" Type="nvarchar" MaxLength="200" />
<Property Name="State" Type="nvarchar" Nullable="false" MaxLength="200" />
<Property Name="Country" Type="nvarchar" Nullable="false" MaxLength="200" />
</EntityType>
<EntityType Name="Offers">
<Key>
<PropertyRef Name="OfferID" />
</Key>
<Property Name="OfferID" Type="uniqueidentifier" Nullable="false" />
<Property Name="NeedOffer" Type="uniqueidentifier" Nullable="false" />
<Property Name="ProvisionOffer" Type="uniqueidentifier" Nullable="false" />
</EntityType>
<EntityType Name="PostBundleJunction">
<Key>
<PropertyRef Name="BundleId" />
<PropertyRef Name="PostId" />
</Key>
<Property Name="BundleId" Type="int" Nullable="false" />
<Property Name="PostId" Type="int" Nullable="false" />
</EntityType>
<EntityType Name="PostMedia">
<Key>
<PropertyRef Name="MediaId" />
</Key>
<Property Name="MediaId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="MediaExt" Type="nvarchar" Nullable="false" MaxLength="5" />
<Property Name="PostId" Type="int" Nullable="false" />
<Property Name="SynthId" Type="uniqueidentifier" />
</EntityType>
<EntityType Name="Posts">
<Key>
<PropertyRef Name="PostId" />
</Key>
<Property Name="PostId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="PosterID" Type="uniqueidentifier" Nullable="false" />
<Property Name="BundleId" Type="int" Nullable="false" />
<Property Name="LocationId" Type="int" Nullable="false" />
<Property Name="Tags" Type="uniqueidentifier" />
<Property Name="Quanitity" Type="int" Nullable="false" />
<Property Name="Description" Type="text" Nullable="false" />
<Property Name="ResourceNameId" Type="int" Nullable="false" />
<Property Name="Date" Type="datetime" Nullable="false" />
</EntityType>
<EntityType Name="ResourceEdges">
<Key>
<PropertyRef Name="EdgeId" />
</Key>
<Property Name="Rank" Type="int" Nullable="false" />
<Property Name="EdgeId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="ToResourceId" Type="int" Nullable="false" />
<Property Name="FromResourrceId" Type="int" Nullable="false" />
</EntityType>
<EntityType Name="ResourceNames">
<Key>
<PropertyRef Name="ResourceNameId" />
</Key>
<Property Name="ResourceNameId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="100" />
<Property Name="ResourceId" Type="int" Nullable="false" />
</EntityType>
<EntityType Name="Resources">
<Key>
<PropertyRef Name="ResourceId" />
</Key>
<Property Name="ResourceId" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="Description" Type="nvarchar" MaxLength="50" />
</EntityType>
<EntityType Name="sysdiagrams">
<Key>
<PropertyRef Name="diagram_id" />
</Key>
<Property Name="name" Type="nvarchar" Nullable="false" MaxLength="128" />
<Property Name="principal_id" Type="int" Nullable="false" />
<Property Name="diagram_id" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="version" Type="int" />
<Property Name="definition" Type="varbinary(max)" />
</EntityType>
<EntityType Name="Users">
<Key>
<PropertyRef Name="UserID" />
</Key>
<Property Name="UserID" Type="uniqueidentifier" Nullable="false" />
<Property Name="LocationId" Type="int" />
</EntityType>
<EntityType Name="Votes">
<Key>
<PropertyRef Name="VoteId" />
</Key>
<Property Name="VoteId" Type="int" Nullable="false" />
<Property Name="VoterId" Type="uniqueidentifier" Nullable="false" />
<Property Name="VoteContent" Type="int" Nullable="false" />
<Property Name="PostId" Type="int" Nullable="false" />
</EntityType>
<Association Name="FK_Comments_Offers">
<End Role="Offers" Type="XDbModel.Store.Offers" Multiplicity="0..1" />
<End Role="Comments" Type="XDbModel.Store.Comments" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Offers">
<PropertyRef Name="OfferID" />
</Principal>
<Dependent Role="Comments">
<PropertyRef Name="OfferPostID" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_Comments_Users">
<End Role="Users" Type="XDbModel.Store.Users" Multiplicity="1">
<OnDelete Action="Cascade" />
</End>
<End Role="Comments" Type="XDbModel.Store.Comments" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Users">
<PropertyRef Name="UserID" />
</Principal>
<Dependent Role="Comments">
<PropertyRef Name="UserID" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_LocationPoints_Locations">
<End Role="Locations" Type="XDbModel.Store.Locations" Multiplicity="1">
<OnDelete Action="Cascade" />
</End>
<End Role="LocationPoints" Type="XDbModel.Store.LocationPoints" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Locations">
<PropertyRef Name="LocationId" />
</Principal>
<Dependent Role="LocationPoints">
<PropertyRef Name="LocationId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_PostBundleJunction_Bundles">
<End Role="Bundles" Type="XDbModel.Store.Bundles" Multiplicity="1" />
<End Role="PostBundleJunction" Type="XDbModel.Store.PostBundleJunction" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Bundles">
<PropertyRef Name="BundleId" />
</Principal>
<Dependent Role="PostBundleJunction">
<PropertyRef Name="BundleId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_PostBundleJunction_Posts">
<End Role="Posts" Type="XDbModel.Store.Posts" Multiplicity="1" />
<End Role="PostBundleJunction" Type="XDbModel.Store.PostBundleJunction" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Posts">
<PropertyRef Name="PostId" />
</Principal>
<Dependent Role="PostBundleJunction">
<PropertyRef Name="PostId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_Posts_Locations">
<End Role="Locations" Type="XDbModel.Store.Locations" Multiplicity="1" />
<End Role="Posts" Type="XDbModel.Store.Posts" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Locations">
<PropertyRef Name="LocationId" />
</Principal>
<Dependent Role="Posts">
<PropertyRef Name="LocationId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_Posts_ResourceNames">
<End Role="ResourceNames" Type="XDbModel.Store.ResourceNames" Multiplicity="1" />
<End Role="Posts" Type="XDbModel.Store.Posts" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="ResourceNames">
<PropertyRef Name="ResourceNameId" />
</Principal>
<Dependent Role="Posts">
<PropertyRef Name="ResourceNameId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_Posts_Users">
<End Role="Users" Type="XDbModel.Store.Users" Multiplicity="1" />
<End Role="Posts" Type="XDbModel.Store.Posts" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Users">
<PropertyRef Name="UserID" />
</Principal>
<Dependent Role="Posts">
<PropertyRef Name="PosterID" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_ResourceEdges_Resources">
<End Role="Resources" Type="XDbModel.Store.Resources" Multiplicity="1" />
<End Role="ResourceEdges" Type="XDbModel.Store.ResourceEdges" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Resources">
<PropertyRef Name="ResourceId" />
</Principal>
<Dependent Role="ResourceEdges">
<PropertyRef Name="ToResourceId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_ResourceEdges_Resources1">
<End Role="Resources" Type="XDbModel.Store.Resources" Multiplicity="1" />
<End Role="ResourceEdges" Type="XDbModel.Store.ResourceEdges" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Resources">
<PropertyRef Name="ResourceId" />
</Principal>
<Dependent Role="ResourceEdges">
<PropertyRef Name="FromResourrceId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_ResourceNames_Resources">
<End Role="Resources" Type="XDbModel.Store.Resources" Multiplicity="1">
<OnDelete Action="Cascade" />
</End>
<End Role="ResourceNames" Type="XDbModel.Store.ResourceNames" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Resources">
<PropertyRef Name="ResourceId" />
</Principal>
<Dependent Role="ResourceNames">
<PropertyRef Name="ResourceId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_Users_Locations">
<End Role="Locations" Type="XDbModel.Store.Locations" Multiplicity="0..1">
<OnDelete Action="Cascade" />
</End>
<End Role="Users" Type="XDbModel.Store.Users" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Locations">
<PropertyRef Name="LocationId" />
</Principal>
<Dependent Role="Users">
<PropertyRef Name="LocationId" />
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_Votes_Posts">
<End Role="Posts" Type="XDbModel.Store.Posts" Multiplicity="1">
<OnDelete Action="Cascade" />
</End>
<End Role="Votes" Type="XDbModel.Store.Votes" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Posts">
<PropertyRef Name="PostId" />
</Principal>
<Dependent Role="Votes">
<PropertyRef Name="PostId" />
</Dependent>
</ReferentialConstraint>
</Association>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="XDbModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityContainer Name="XDbEntities" annotation:LazyLoadingEnabled="true">
<EntitySet Name="Bundles" EntityType="XDbModel.Bundle" />
<EntitySet Name="CellProviders" EntityType="XDbModel.CellProvider" />
<EntitySet Name="Comments" EntityType="XDbModel.Comment" />
<EntitySet Name="LocationPoints" EntityType="XDbModel.LocationPoint" />
<EntitySet Name="Locations" EntityType="XDbModel.Location" />
<EntitySet Name="Offers" EntityType="XDbModel.Offer" />
<EntitySet Name="PostMedias" EntityType="XDbModel.PostMedia" />
<EntitySet Name="Posts" EntityType="XDbModel.Post" />
<EntitySet Name="ResourceEdges" EntityType="XDbModel.ResourceEdge" />
<EntitySet Name="ResourceNames" EntityType="XDbModel.ResourceName" />
<EntitySet Name="Resources" EntityType="XDbModel.Resource" />
<EntitySet Name="sysdiagrams" EntityType="XDbModel.sysdiagram" />
<EntitySet Name="Users" EntityType="XDbModel.User" />
<EntitySet Name="Votes" EntityType="XDbModel.Vote" />
<AssociationSet Name="FK_Comments_Offers" Association="XDbModel.FK_Comments_Offers">
<End Role="Offers" EntitySet="Offers" />
<End Role="Comments" EntitySet="Comments" />
</AssociationSet>
<AssociationSet Name="FK_Comments_Users" Association="XDbModel.FK_Comments_Users">
<End Role="Users" EntitySet="Users" />
<End Role="Comments" EntitySet="Comments" />
</AssociationSet>
<AssociationSet Name="FK_LocationPoints_Locations" Association="XDbModel.FK_LocationPoints_Locations">
<End Role="Locations" EntitySet="Locations" />
<End Role="LocationPoints" EntitySet="LocationPoints" />
</AssociationSet>
<AssociationSet Name="FK_Posts_Locations" Association="XDbModel.FK_Posts_Locations">
<End Role="Locations" EntitySet="Locations" />
<End Role="Posts" EntitySet="Posts" />
</AssociationSet>
<AssociationSet Name="FK_Users_Locations" Association="XDbModel.FK_Users_Locations">
<End Role="Locations" EntitySet="Locations" />
<End Role="Users" EntitySet="Users" />
</AssociationSet>
<AssociationSet Name="FK_Posts_ResourceNames" Association="XDbModel.FK_Posts_ResourceNames">
<End Role="ResourceNames" EntitySet="ResourceNames" />
<End Role="Posts" EntitySet="Posts" />
</AssociationSet>
<AssociationSet Name="FK_Posts_Users" Association="XDbModel.FK_Posts_Users">
<End Role="Users" EntitySet="Users" />
<End Role="Posts" EntitySet="Posts" />
</AssociationSet>
<AssociationSet Name="FK_Votes_Posts" Association="XDbModel.FK_Votes_Posts">
<End Role="Posts" EntitySet="Posts" />
<End Role="Votes" EntitySet="Votes" />
</AssociationSet>
<AssociationSet Name="FK_ResourceEdges_Resources" Association="XDbModel.FK_ResourceEdges_Resources">
<End Role="Resources" EntitySet="Resources" />
<End Role="ResourceEdges" EntitySet="ResourceEdges" />
</AssociationSet>
<AssociationSet Name="FK_ResourceEdges_Resources1" Association="XDbModel.FK_ResourceEdges_Resources1">
<End Role="Resources" EntitySet="Resources" />
<End Role="ResourceEdges" EntitySet="ResourceEdges" />
</AssociationSet>
<AssociationSet Name="FK_ResourceNames_Resources" Association="XDbModel.FK_ResourceNames_Resources">
<End Role="Resources" EntitySet="Resources" />
<End Role="ResourceNames" EntitySet="ResourceNames" />
</AssociationSet>
<AssociationSet Name="PostBundleJunction" Association="XDbModel.PostBundleJunction">
<End Role="Bundles" EntitySet="Bundles" />
<End Role="Posts" EntitySet="Posts" />
</AssociationSet>
</EntityContainer>
<EntityType Name="Bundle">
<Key>
<PropertyRef Name="BundleId" />
</Key>
<Property Name="BundleId" Type="Int32" Nullable="false" />
<NavigationProperty Name="Posts" Relationship="XDbModel.PostBundleJunction" FromRole="Bundles" ToRole="Posts" />
</EntityType>
<EntityType Name="CellProvider">
<Key>
<PropertyRef Name="CellProviderID" />
</Key>
<Property Name="CellProviderID" Type="Guid" Nullable="false" />
</EntityType>
<EntityType Name="Comment">
<Key>
<PropertyRef Name="CommentID" />
¿Qué versión de entity-framework/VS2010 (?) Estás? –
VS2010 RC1, no estoy seguro de la versión de marco. –
El método SingleOrDefault es Linq2Sql ... Use FirstOrDefault en EF en su lugar. Solo una observación. –