Join two related tables into a single entity using Entity Framework - entity-framework

My question relates to the "table joining" function in the ADO.NET Entity Framework.
Imagine a database that contains a table "Product" and another table "ProductPrice". The price table stores a history of all price changes for a product, with a start and end date, where the line containing the current price is indicated by a NULL value in the end date column. This database structure could be useful for statistical purposes, for example the average daily sales volume could be mapped to each change in the product price. However, for the online ordering website, only the current price is required.
Here's the structure of the two tables:
Product
ProductID (PK, int, NOT NULL, auto increment)
Name (varchar 50, NOT NULL)
ProductPrice
ProductPriceID (PK, int, NOT NULL, auto increment)
ProductID (INT, NOT NULL)
StartDate (DATETIME, NOT NULL)
EndDate (DATETIME)
Price (MONEY, NOT NULL)
Here's an example of an SQL statement to retrieve the product plus the current price:
SELECT Product.ProductID, Product.Name, ProductPrice.Price AS CurrentPrice
FROM Product
LEFT JOIN ProductPrice
ON Product.ProductID = ProductPrice.ProductID
AND ProductPrice.EndDate IS NULL
I'd like to use the Entity Framework to join the entities Product and ProductPrice together, so that I can access the current price directly from the Product entity, as in the following example:
var product = (from p in context.Product where p.ProductID == 2 select p).FirstOrDefault();
Console.WriteLine(product.Name);
Console.WriteLine(product.CurrentPrice);
Unfortunately, I'm getting stuck with errors that I can't resolve.
Here are the entities from the storage model:
<EntityType Name="Product">
<Key>
<PropertyRef Name="ProductID" />
</Key>
<Property Name="ProductID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="Name" Type="varchar" Nullable="false" MaxLength="50" />
</EntityType>
<EntityType Name="ProductPrice">
<Key>
<PropertyRef Name="ProductPriceID" />
</Key>
<Property Name="ProductPriceID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="ProductID" Type="int" Nullable="false" />
<Property Name="Price" Type="money" Nullable="false" />
<Property Name="StartDate" Type="datetime" Nullable="false" />
<Property Name="EndDate" Type="datetime" />
</EntityType>
<Association Name="FK_ProductPrice_Product">
<End Role="Product" Type="TestingModel.Store.Product" Multiplicity="1" />
<End Role="ProductPrice" Type="TestingModel.Store.ProductPrice" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Product">
<PropertyRef Name="ProductID" />
</Principal>
<Dependent Role="ProductPrice">
<PropertyRef Name="ProductID" />
</Dependent>
</ReferentialConstraint>
</Association>
And from the conceptual model:
<EntityType Name="Product">
<Key>
<PropertyRef Name="ProductID" />
</Key>
<Property Name="ProductID" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Name="Name" Type="String" Nullable="false" MaxLength="50" Unicode="false" FixedLength="false" />
<Property Name="SKU" Type="String" Nullable="false" MaxLength="50" Unicode="false" FixedLength="false" />
<Property Type="Decimal" Name="CurrentPrice" Nullable="false" Precision="19" Scale="4" />
</EntityType>
And finally the mapping between the two:
<EntitySetMapping Name="Product">
<EntityTypeMapping TypeName="TestingModel.Product">
<MappingFragment StoreEntitySet="Product">
<ScalarProperty Name="ProductID" ColumnName="ProductID" />
<ScalarProperty Name="Name" ColumnName="Name" />
<ScalarProperty Name="SKU" ColumnName="SKU" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="IsTypeOf(TestingModel.Product)">
<MappingFragment StoreEntitySet="ProductPrice">
<ScalarProperty Name="CurrentPrice" ColumnName="Price" />
<Condition ColumnName="EndDate" IsNull="true" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
And here are the error messages that I'm currently struggling with:
Error 1 Error 3024: Problem in mapping fragments starting at line 76:Must specify mapping for all key properties (Product.ProductID) of the EntitySet Product.
Error 2 Error 3025: Problem in mapping fragments starting at line 76:Must specify mapping for all key properties (ProductPrice.ProductPriceID) of table ProductPrice.
I'm not sure if this is even possible with the Entity Framework, maybe I should just do the join manually myself in LINQ.
Any suggestions would be greatly appreciated.

You do not meet the conditions for entity splitting:
You should only map an entity type to multiple tables if the following conditions are true:
The tables to which you are mapping share a common key.
The entity type that is being mapped has entries in each underlying table. In other words, the entity type represents data that has a one-to-one correspondence between the two tables; the entity type represents an inner join of the two tables.
You are smarter than EF. You know that the condition EndDate == null yields one record. EF has no clue to know that and thus never knows that it can create one object from the two tables. In terms of the exception message: PriceId and EndDate == null should somehow deliver all key properties of your ProductPrice records, which is impossible, obviously.
Alternatives:
A. You can create a one-to-many association between the two and query:
products.Where(p => p.ProductId == 2)
.Select(p => new
{
Product = p,
CurrentPrice = p.ProductPrices.Where(pp => pp.EndDate == null)
.FirstOrDefault()
})
Not as convenient as you'd like, but maybe not too bad when wrapped in a repository.
B. Create a database view and use separate paths for viewing and updating (which is not an uncommon thing to do).

You need to do a foreign key between the two tables (i.e. on column ProductID)
Then to retrieve all the products together with their prices you need to do the following:
var x = from p in context.Product where p.ProductID == 2 && p.ProductPrice.EndDate == null select new { p.ProductID, p.Name, p.ProductPrice.Price }.FirstOrDefault();

Related

SAPUI5 - How to bind fields of child entity in same panel as fields of parent entity in SAP Friori Sample "Approve PO" app?

In the SAP Sample "Approve Purchase Order" application, that comes with the SAP Web IDE, how do I bind fields of "Supplier" entity (child) in the same "Simple Form" UI container control as the fields of "PurchaseOrder" (parent) entity. In this sample, there are 3 separate mock data files, one each for "Purchase Order", "Purchase Order Items" and "Supplier". The relationship between Purchase Order and Supplier is 1:1 defined in the metadata.xml using association.
a) PurchaseOrder (relevant portion only)
<EntityType Name="PurchaseOrder" sap:content-version="1" sap:is-thing-type="true">
<Key>
<PropertyRef Name="POId"/>
</Key>
<Property MaxLength="10" Name="POId" Nullable="false" Type="Edm.String" sap:creatable="false" sap:filterable="false"
sap:label="Purchase Order ID" sap:updatable="false"/>
<Property MaxLength="10" Name="OrderedById" Nullable="false" Type="Edm.String" sap:creatable="false" sap:filterable="false"
<Property MaxLength="10" Name="SupplierId" Nullable="false" Type="Edm.String" sap:creatable="false" sap:filterable="false" sap:label="ID"
sap:sortable="false" sap:updatable="false"/>
b) Supplier (relevant portion only)
<EntityType Name="Supplier" sap:content-version="1" sap:is-thing-type="true">
<Key>
<PropertyRef Name="Id"/>
</Key>
<Property MaxLength="10" Name="Id" Nullable="false" Type="Edm.String" sap:creatable="false" sap:filterable="false" sap:label="ID"
sap:sortable="false" sap:updatable="false"/>
<Property MaxLength="255" Name="Email" Nullable="false" Type="Edm.String" sap:creatable="false" sap:filterable="false" sap:label="E-Mail"
sap:semantics="email" sap:sortable="false" sap:updatable="false"/>
c) Association
<Association Name="PurchaseOrderSupplier" sap:content-version="1" sap:label="Association: Supplier --> Purchase Order">
<End Multiplicity="1" Role="FromRole_PurchaseOrderSupplier" Type="EPM_REF_APPS_PO_APV_SRV.Supplier"/>
<End Multiplicity="*" Role="ToRole_PurchaseOrderSupplier" Type="EPM_REF_APPS_PO_APV_SRV.PurchaseOrder"/>
<ReferentialConstraint>
<Principal Role="FromRole_PurchaseOrderSupplier">
<PropertyRef Name="Id"/>
</Principal>
<Dependent Role="ToRole_PurchaseOrderSupplier">
<PropertyRef Name="SupplierId"/>
</Dependent>
</ReferentialConstraint>
</Association>
The portion of the view (PurchaseOrderDetails.view.xml) is shown below. All the fields, except Email is from the parent, PurchaseOrder entity.
<form:SimpleForm class="sapUiForceWidthAuto sapUiResponsiveMargin" columnsL="1" columnsM="1" emptySpanL="5" emptySpanM="5" id="poHeaderForm"
labelSpanL="3" labelSpanM="3" layout="ResponsiveGridLayout" maxContainerCols="2" minWidth="1024" title="{i18n>xtit.formTitle}">
<Label id="poIdFormLabel" text="{/#PurchaseOrder/POId/#sap:label}"/>
<Text id="poIdForm" text="{POId}"/>
<Label id="addressFormLabel" text="{/#PurchaseOrder/DeliveryAddress/#sap:label}"/>
<Text id="addressForm" text="{DeliveryAddress}"/>
<Label id="supplierEmailLabel" text="{/#Supplier/Email/#sap:label}"/>
<Text id="supplierEmail" text="{/PurchaseOrder/Id/Email}"/>
</form:SimpleForm>
I have tried many permutations to bind the field, Email, from the Supplier entity viz: a) {/Id/Email}, b) {path: 'Supplier' , parameters: {Select 'Email'}} but the result has been a blank space.
Please show the correct binding syntax for "Email".
Your metadata.xml snippet does not contain a NavigationProperty to the supplier. Therefore, your Association is not recognized... You have to fix your metadata.xml first. After this you can easily do the following:
<Text id="supplierEmail" binding="{Supplier}" text="{Email}"/>
This assumes that the navigation property of your you have named your NavigationProperty "Supplier" inside your PurchaseOrder Entity. In that case I also suggest you to use $expand=Supplier in the binding in order to get the Supplier data in the same request (the one for the PurchaseOrder), i.e. something like this:
items="{
path: '/PurchaseOrderItems',
parameters: {
'expand': 'Supplier'
}
}"
After this you could simply use this without the binding attribute because you have 'expanded':
<Text id="supplierEmail" text="{Supplier/Email}"/>

Must Be Mapped. It has no default value and is not nullable

I'm just starting to learn EF. The problem I'm facing is with TPH. The example below is from the EF recipes by apress. The table is basically this:
CREATE TABLE [Chapter2].[Employee](
[EmployeeId] [int] IDENTITY(1,1) NOT NULL,
[EmployeeType] [int] NULL,
[FirstName] [nvarchar](50) NOT NULL,
[LastName] [nvarchar](50) NOT NULL,
[Salary] [decimal](18, 0) NOT NULL,
[Wage] [decimal](18, 0) NOT NULL,
CONSTRAINT [PK_Employee] PRIMARY KEY CLUSTERED
(
[EmployeeId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
After creating table and importing it into EF. I made two entities, one FullTimeEmployee and HourlyEmployee. I'm setting the condition under employee type to 1 for the full time one and 2 for the hourly one. I'm, of course deleting the property from the main Employee entity.
<edmx:Mappings>
<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
<EntityContainerMapping StorageEntityContainer="EFRecipesModel1StoreContainer" CdmEntityContainer="EFRecipesEntities1">
<EntitySetMapping Name="Employees">
<EntityTypeMapping TypeName="IsTypeOf(EFRecipesModel1.Employee)">
<MappingFragment StoreEntitySet="Employee">
<ScalarProperty Name="EmployeeId" ColumnName="EmployeeId" />
<ScalarProperty Name="LastName" ColumnName="LastName" />
<ScalarProperty Name="FirstName" ColumnName="FirstName" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="IsTypeOf(EFRecipesModel1.FullTimeEmployee)">
<MappingFragment StoreEntitySet="Employee">
<ScalarProperty Name="EmployeeId" ColumnName="EmployeeId" />
<ScalarProperty Name="Salary" ColumnName="Salary" />
<Condition ColumnName="EmployeeType" Value="1" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="IsTypeOf(EFRecipesModel1.HourlyEmployee)">
<MappingFragment StoreEntitySet="Employee">
<ScalarProperty Name="EmployeeId" ColumnName="EmployeeId" />
<ScalarProperty Name="Wage" ColumnName="Wage" />
<Condition ColumnName="EmployeeType" Value="2" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
THe error I'm getting is:
Error3023: Problem in maaping fragments starting at line 51 column: Employee.Salary in table Employee must be mapped. It has no default value and is not nullable.
I read around and saw a suggestino up update the SSDL which in a way didn't make sense to me:
<edmx:StorageModels>
<Schema Namespace="EFRecipesModel1.Store" Provider="System.Data.SqlClient" ProviderManifestToken="2012" Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
<EntityType Name="Employee">
<Key>
<PropertyRef Name="EmployeeId" />
</Key>
<Property Name="EmployeeId" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
<Property Name="EmployeeType" Type="int" Nullable ="false" DefaultValue="1"/>
<Property Name="FirstName" Type="nvarchar" MaxLength="50" Nullable="false" />
<Property Name="LastName" Type="nvarchar" MaxLength="50" Nullable="false" />
<Property Name="Salary" Type="decimal" Precision="18" Scale="0" Nullable="false" />
<Property Name="Wage" Type="decimal" Precision="18" Scale="0" Nullable="false" />
</EntityType>
<EntityContainer Name="EFRecipesModel1StoreContainer">
<EntitySet Name="Employee" EntityType="Self.Employee" Schema="Chapter2" store:Type="Tables" />
</EntityContainer>
</Schema>
</edmx:StorageModels>
note that I added the Nullable and Default value for EmployeeType. This still doesn't solve the problem.
Could I get some help as to why I'm having such a problem mapping properly?
To me it makes more sense to add DefaultValue to Salary and Wage in the store model:
<Property Name="Salary" Type="decimal" Nullable="false" DefaultValue="0"/>
<Property Name="Wage" Type="decimal" Nullable="false" DefaultValue="0"/>
And also in the conceptual model (in the edmx designer).
It would also make sense to add them as default constraints to the fields in the database (although EF does not copy these constraints to the edmx when the model is generated from the database).
Anyhow, the fields must have a default value since they are not nullable in the database, and none of the concrete entities supply values for both fields.
I ran into a similar issue and what I done was;
Right-click the "Main" entity, and select Properties. Set the Abstract attribute to true, which marked the "Main" entity as abstract.
Just thought I would share this as setting a default value for me stopped ef from complaining but the value was not available when needed in my program .

The entity type <class> is not part of the model for the current context

This is a MODEL first approach. I have already researched this extensiely and have not come up with an answer. I have tried all the suggestions at the following links:
This appears to be the same problem but with no resolution
The entity type <classname> is not part of the model for the current context
These are the links I have already researched so please don't answer as duplicate of ----
EF 4.1 Code First error - The entity type SomeType is not part of the model for the current context
The entity type is not part of the model, EF 5
Here is the EDMX (Abbreviated for brevity):
<?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="Insight_Model.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="Insight_ModelStoreContainer">
<EntitySet Name="vwCompanyHeader" EntityType="Insight_Model.Store.vwCompanyHeader" store:Type="Views" store:Schema="dbo" store:Name="vwCompanyHeader">
<DefiningQuery>SELECT
[vwCompanyHeader].[ID] AS [ID],
[vwCompanyHeader].[Name] AS [Name],
[vwCompanyHeader].[Active] AS [Active],
[vwCompanyHeader].[Creator] AS [Creator],
[vwCompanyHeader].[Created] AS [Created],
[vwCompanyHeader].[Modifier] AS [Modifier],
[vwCompanyHeader].[Modified] AS [Modified]
FROM [dbo].[vwCompanyHeader] AS [vwCompanyHeader]</DefiningQuery>
</EntitySet>
<EntitySet Name="vwWorkOrderHeader" EntityType="Insight_Model.Store.vwWorkOrderHeader" store:Type="Views" store:Schema="dbo" store:Name="vwWorkOrderHeader">
<DefiningQuery>SELECT
[vwWorkOrderHeader].[ID] AS [ID],
[vwWorkOrderHeader].[Active] AS [Active],
[vwWorkOrderHeader].[Name] AS [Name],
[vwWorkOrderHeader].[Creator] AS [Creator],
[vwWorkOrderHeader].[Created] AS [Created],
[vwWorkOrderHeader].[Modifier] AS [Modifier],
[vwWorkOrderHeader].[Modified] AS [Modified],
[vwWorkOrderHeader].[Leader] AS [Leader],
[vwWorkOrderHeader].[LeadTakenDate] AS [LeadTakenDate],
[vwWorkOrderHeader].[ProjectID] AS [ProjectID],
[vwWorkOrderHeader].[ProjectName] AS [ProjectName],
[vwWorkOrderHeader].[WorkOrderStatus] AS [WorkOrderStatus],
[vwWorkOrderHeader].[WorkOrderSubStatus] AS [WorkOrderSubStatus],
[vwWorkOrderHeader].[WorkOrderType] AS [WorkOrderType],
[vwWorkOrderHeader].[WorkOrderSubType] AS [WorkOrderSubType],
[vwWorkOrderHeader].[WorkOrderPriority] AS [WorkOrderPriority],
[vwWorkOrderHeader].[RefNumber] AS [RefNumber],
[vwWorkOrderHeader].[Request] AS [Request]
FROM [dbo].[vwWorkOrderHeader] AS [vwWorkOrderHeader]</DefiningQuery>
</EntitySet>
</EntityContainer>
<EntityType Name="vwCompanyHeader">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Name="ID" Type="int" Nullable="false" />
<Property Name="Name" Type="varchar" Nullable="false" MaxLength="50" />
<Property Name="Active" Type="bit" Nullable="false" />
<Property Name="Creator" Type="varchar" Nullable="false" MaxLength="51" />
<Property Name="Created" Type="date" Nullable="false" />
<Property Name="Modifier" Type="varchar" Nullable="false" MaxLength="51" />
<Property Name="Modified" Type="date" Nullable="false" />
</EntityType>
<EntityType Name="vwWorkOrderHeader">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Name="ID" Type="int" Nullable="false" />
<Property Name="Active" Type="bit" Nullable="false" />
<Property Name="Name" Type="varchar" Nullable="false" MaxLength="15" />
<Property Name="Creator" Type="varchar" Nullable="false" MaxLength="51" />
<Property Name="Created" Type="date" Nullable="false" />
<Property Name="Modifier" Type="varchar" Nullable="false" MaxLength="51" />
<Property Name="Modified" Type="date" Nullable="false" />
<Property Name="Leader" Type="varchar" Nullable="false" MaxLength="51" />
<Property Name="LeadTakenDate" Type="date" Nullable="false" />
<Property Name="ProjectID" Type="int" Nullable="false" />
<Property Name="ProjectName" Type="varchar" Nullable="false" MaxLength="15" />
<Property Name="WorkOrderStatus" Type="varchar" Nullable="false" MaxLength="35" />
<Property Name="WorkOrderSubStatus" Type="varchar" Nullable="false" MaxLength="35" />
<Property Name="WorkOrderType" Type="varchar" Nullable="false" MaxLength="35" />
<Property Name="WorkOrderSubType" Type="varchar" Nullable="false" MaxLength="35" />
<Property Name="WorkOrderPriority" Type="varchar" Nullable="false" MaxLength="35" />
<Property Name="RefNumber" Type="varchar" Nullable="false" MaxLength="25" />
<Property Name="Request" Type="varchar" Nullable="false" MaxLength="1500" />
</EntityType>
</Schema></edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="Insight_Model" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityContainer Name="Insight_Entities" annotation:LazyLoadingEnabled="false">
<EntitySet Name="CompanyHeaders" EntityType="Insight_Model.CompanyHeader" />
<EntitySet Name="WorkOrderHeaders" EntityType="Insight_Model.WorkOrderHeader" />
</EntityContainer>
<EntityType Name="CompanyHeader">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Type="String" Name="Name" MaxLength="50" FixedLength="false" Unicode="false" Nullable="false" />
<Property Type="Boolean" Name="Active" Nullable="false" />
<Property Type="String" Name="Creator" Nullable="false" MaxLength="51" FixedLength="false" Unicode="false" />
<Property Type="DateTime" Name="Created" Nullable="false" />
<Property Type="String" Name="Modifier" Nullable="false" MaxLength="51" FixedLength="false" Unicode="false" />
<Property Type="DateTime" Name="Modified" Nullable="false" />
<Property Type="Int32" Name="ID" Nullable="false" />
</EntityType>
<EntityType Name="WorkOrderHeader">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Type="Int32" Name="ID" Nullable="false" />
<Property Type="Boolean" Name="Active" Nullable="false" />
<Property Type="String" Name="Name" Nullable="false" MaxLength="15" FixedLength="false" Unicode="false" />
<Property Type="String" Name="Creator" Nullable="false" MaxLength="51" FixedLength="false" Unicode="false" />
<Property Type="DateTime" Name="Created" Nullable="false" />
<Property Type="String" Name="Modifier" Nullable="false" MaxLength="51" FixedLength="false" Unicode="false" />
<Property Type="DateTime" Name="Modified" Nullable="false" />
<Property Type="String" Name="Leader" Nullable="false" MaxLength="51" FixedLength="false" Unicode="false" />
<Property Type="DateTime" Name="LeadTakenDate" Nullable="false" />
<Property Type="Int32" Name="ProjectID" Nullable="false" />
<Property Type="String" Name="ProjectName" Nullable="false" MaxLength="15" FixedLength="false" Unicode="false" />
<Property Type="String" Name="WorkOrderStatus" Nullable="false" MaxLength="35" FixedLength="false" Unicode="false" />
<Property Type="String" Name="WorkOrderSubStatus" Nullable="false" MaxLength="35" FixedLength="false" Unicode="false" />
<Property Type="String" Name="WorkOrderType" Nullable="false" MaxLength="35" FixedLength="false" Unicode="false" />
<Property Type="String" Name="WorkOrderSubType" Nullable="false" MaxLength="35" FixedLength="false" Unicode="false" />
<Property Type="String" Name="WorkOrderPriority" Nullable="false" MaxLength="35" FixedLength="false" Unicode="false" />
<Property Type="String" Name="RefNumber" MaxLength="25" Nullable="false" FixedLength="false" Unicode="false" />
<Property Type="String" Name="Request" Nullable="false" MaxLength="1500" FixedLength="false" Unicode="false" />
</EntityType>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs">
<EntityContainerMapping StorageEntityContainer="Insight_ModelStoreContainer" CdmEntityContainer="Insight_Entities">
<EntitySetMapping Name="CompanyHeaders">
<EntityTypeMapping TypeName="Insight_Model.CompanyHeader">
<MappingFragment StoreEntitySet="vwCompanyHeader">
<ScalarProperty Name="ID" ColumnName="ID" />
<ScalarProperty Name="Modified" ColumnName="Modified" />
<ScalarProperty Name="Modifier" ColumnName="Modifier" />
<ScalarProperty Name="Created" ColumnName="Created" />
<ScalarProperty Name="Creator" ColumnName="Creator" />
<ScalarProperty Name="Active" ColumnName="Active" />
<ScalarProperty Name="Name" ColumnName="Name" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
<EntitySetMapping Name="WorkOrderHeaders">
<EntityTypeMapping TypeName="Insight_Model.WorkOrderHeader">
<MappingFragment StoreEntitySet="vwWorkOrderHeader">
<ScalarProperty Name="Request" ColumnName="Request" />
<ScalarProperty Name="RefNumber" ColumnName="RefNumber" />
<ScalarProperty Name="WorkOrderPriority" ColumnName="WorkOrderPriority" />
<ScalarProperty Name="WorkOrderSubType" ColumnName="WorkOrderSubType" />
<ScalarProperty Name="WorkOrderType" ColumnName="WorkOrderType" />
<ScalarProperty Name="WorkOrderSubStatus" ColumnName="WorkOrderSubStatus" />
<ScalarProperty Name="WorkOrderStatus" ColumnName="WorkOrderStatus" />
<ScalarProperty Name="ProjectName" ColumnName="ProjectName" />
<ScalarProperty Name="ProjectID" ColumnName="ProjectID" />
<ScalarProperty Name="LeadTakenDate" ColumnName="LeadTakenDate" />
<ScalarProperty Name="Leader" ColumnName="Leader" />
<ScalarProperty Name="Modified" ColumnName="Modified" />
<ScalarProperty Name="Modifier" ColumnName="Modifier" />
<ScalarProperty Name="Created" ColumnName="Created" />
<ScalarProperty Name="Creator" ColumnName="Creator" />
<ScalarProperty Name="Name" ColumnName="Name" />
<ScalarProperty Name="Active" ColumnName="Active" />
<ScalarProperty Name="ID" ColumnName="ID" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
The repository code:
Imports System.Linq.Expressions
Imports System.Data.Entity.Infrastructure
Imports System.Data.Entity
Public Class Repository(Of T As {Class, IEntity})
Implements IRepository(Of T)
#Region "Attributes"
Private objectContext As IDatabaseContext
Protected objectSet As IDbSet(Of T)
#End Region
Public Sub New(objContext As IDatabaseContext)
objectContext = objContext
objectSet = objectContext.Set(Of T)()
End Sub
Public Function FindAll() As IQueryable(Of T) Implements IRepository(Of T).FindAll
Return objectSet
End Function
Public Function FindWhere(predicate As Expressions.Expression(Of System.Func(Of T, Boolean))) As System.Linq.IQueryable(Of T) Implements Interfaces.IRepository(Of T).FindWhere
Return objectSet.Where(predicate)
End Function
Public Function FindById(id As Integer) As T Implements IRepository(Of T).FindById
Return objectSet.Single(Function(o) o.ID = id)
End Function
Public Sub Add(newEntity As T) Implements IRepository(Of T).Add
objectSet.Add(newEntity)
End Sub
Public Sub Remove(entity As T) Implements IRepository(Of T).Remove
objectSet.Remove(entity)
End Sub
Public Function Count(Optional predicate As Expression(Of Func(Of T, Boolean)) = Nothing) As Integer Implements IRepository(Of T).Count
Return If((predicate Is Nothing), objectSet.Count, objectSet.Where(predicate).Count)
End Function
Public Function Exist(Optional predicate As Expression(Of Func(Of T, Boolean)) = Nothing) As Boolean Implements IRepository(Of T).Exist
Return If((predicate Is Nothing), objectSet.Any, objectSet.Where(predicate).Any)
End Function
End Class
The DatabaseContext code:
Imports System.Data.Entity
Imports Insight.DataLayer.Interfaces
Imports System.Data.Objects
Imports System.Configuration
Public Class DatabaseContext
Inherits DbContext
Implements IDatabaseContext
Const defaultConnectStringName = "Insight_Entities"
Public Sub New()
Me.New(ConfigurationManager.ConnectionStrings(defaultConnectStringName).ConnectionString)
End Sub
Public Sub New(connectionString As String)
MyBase.New(connectionString)
MyBase.Configuration.LazyLoadingEnabled = False
MyBase.Configuration.ProxyCreationEnabled = False
End Sub
Public Overloads Sub SaveChanges() Implements Interfaces.IDatabaseContext.SaveChanges
MyBase.SaveChanges()
End Sub
Public Overloads Function [Set](Of T As Class)() As System.Data.Entity.IDbSet(Of T) Implements Interfaces.IDatabaseContext.Set
Return MyBase.Set(Of T)()
End Function
End Class
And the 2 class files:
WorkOrderHeader:
Imports System.ComponentModel.DataAnnotations
Imports System.Collections.ObjectModel
Imports System.Runtime.Serialization
Imports System.Data.Services.Common
Partial Public Class WorkOrderHeader
Inherits DataTransferObjectHeaderBase
Public Sub New()
End Sub
Public Sub New(iID As Integer, sName As String, bActive As Boolean, sCreator As String, dtCreated As DateTime, sModifier As String, dtModified As DateTime, sLeader As String, dtLeadTaken As Date, iProjectID As Integer, sProjectName As String, sWorkOrderStatus As String, sWorkOrderSubStatus As String, sWorkOrderType As String, sWorkOrderSubType As String, sWorkOrderPriority As String, sRequest As String)
Me.New(iID, sName, bActive, sCreator, dtCreated, sModifier, dtModified, sLeader, dtLeadTaken, iProjectID, sProjectName, sWorkOrderStatus, sWorkOrderSubStatus, sWorkOrderType, sWorkOrderSubType, sWorkOrderPriority, "", sRequest)
End Sub
Public Sub New(iID As Integer, sName As String, bActive As Boolean, sCreator As String, dtCreated As DateTime, sModifier As String, dtModified As DateTime, sLeader As String, dtLeadTaken As Date, iProjectID As Integer, sProjectName As String, sWorkOrderStatus As String, sWorkOrderSubStatus As String, sWorkOrderType As String, sWorkOrderSubType As String, sWorkOrderPriority As String, sRefNumber As String, sRequest As String)
ID = iID
Name = sName
Active = bActive
Creator = sCreator
Created = dtCreated
Modifier = sModifier
Modified = dtModified
Leader = sLeader
LeadTaken = dtLeadTaken
ProjectID = iProjectID
ProjectName = sProjectName
WorkOrderStatus = sWorkOrderStatus
WorkOrderSubStatus = sWorkOrderSubStatus
WorkOrderType = sWorkOrderType
WorkOrderSubType = sWorkOrderSubType
WorkOrderPriority = sWorkOrderPriority
RefNumber = sRefNumber
Request = sRequest
End Sub
<DataMember()> _
Public Property Leader() As String
Get
Return _leader
End Get
Set(value As String)
_leader = value
End Set
End Property
Private _leader As String
<DataMember()> _
Public Property LeadTaken() As Date
Get
Return _leadTaken
End Get
Set(value As Date)
_leadTaken = value
End Set
End Property
Private _leadTaken As Date
<DataMember()> _
Public Property ProjectID() As Int32
Get
Return _projectID
End Get
Set(value As Int32)
If (_projectID <> value) Then
_projectID = value
End If
End Set
End Property
Private _projectID As Int32
<DataMember()> _
Public Property ProjectName() As String
Get
Return _projectName
End Get
Set(value As String)
If (_projectName <> value) Then
_projectName = value
End If
End Set
End Property
Private _projectName As String
<DataMember()> _
Public Property WorkOrderStatus() As String
Get
Return _workOrderStatus
End Get
Set(value As String)
_workOrderStatus = value
End Set
End Property
Private _workOrderStatus As String
<DataMember()> _
Public Property WorkOrderSubStatus() As String
Get
Return _workOrderSubStatus
End Get
Set(value As String)
_workOrderSubStatus = value
End Set
End Property
Private _workOrderSubStatus As String
<DataMember()> _
Public Property WorkOrderType() As String
Get
Return _workOrderType
End Get
Set(value As String)
_workOrderType = value
End Set
End Property
Private _workOrderType As String
<DataMember()> _
Public Property WorkOrderSubType() As String
Get
Return _workOrderSubType
End Get
Set(value As String)
_workOrderSubType = value
End Set
End Property
Private _workOrderSubType As String
<DataMember()> _
Public Property WorkOrderPriority() As String
Get
Return _workOrderPriority
End Get
Set(value As String)
_workOrderPriority = value
End Set
End Property
Private _workOrderPriority As String
<DataMember()> _
Public Property RefNumber() As String
Get
Return _refNumber
End Get
Set(value As String)
_refNumber = value
End Set
End Property
Private _refNumber As String
<DataMember()> _
Public Property Request() As String
Get
Return _request
End Get
Set(value As String)
_request = value
End Set
End Property
Private _request As String
End Class
#End Region
CompanyHeader:
Imports System.ComponentModel.DataAnnotations
Imports System.Collections.ObjectModel
Imports System.Runtime.Serialization
Imports System.Data.Services.Common
<DataServiceKey("ID")> _
<MetadataTypeAttribute(GetType(CompanyHeader.CompanyHeaderMetadata))> _
Partial Public Class CompanyHeader
Inherits DataTransferObjectHeaderBase
Friend NotInheritable Class CompanyHeaderMetadata
'Metadata classes are not meant to be instantiated.
Private Sub New()
MyBase.New()
End Sub
<DataMember()> _
Public Property Active As Boolean
<DataMember()> _
Public Property ID As Integer
<DataMember()> _
Public Property Created As DateTime
<DataMember()> _
Public Property Creator As String
<DataMember()> _
Public Property Modified As DateTime
<DataMember()> _
Public Property Modifier As String
<DataMember()> _
Public Property Name As String
End Class
Public Sub New()
End Sub
Public Sub New(iID As Integer, sName As String, bActive As Boolean, sCreator As String, dtCreated As DateTime, sModifier As String, dtModified As DateTime)
ID = iID
Name = sName
Active = bActive
Creator = sCreator
Created = dtCreated
Modifier = sModifier
Modified = dtModified
End Sub
End Class
Unit Test Code:
<TestMethod()> _
Public Sub WorkOrderServiceTests_TestConnectivityThruService5()
Dim woService As New DatabaseContext
Dim woHeaders = New Repository(Of WorkOrderHeader)(woService)
Dim woTests = New Repository(Of WorkOrderTest)(woService)
Dim coHeaders = New Repository(Of CompanyHeader)(woService)
Assert.IsTrue(woHeaders.Count <> 0)
End Sub
Things I have tried to resolve the issue:
1) Checked database - Both queries return data correctly.
select * from vwWorkOrderHeader
select * from vwCompanyHeader
2) Deleted WorkOrderHeader from the EDMX then recreated it from database.
3) MOQed out the DBContext to make sure that it isn't the service query doing something funny. This correctly returns the object, so it means that it is something at the context level or below.
4) Stepped through both tests to ensure that they use the same method of building the context - Thy do.
4) Stepped through both tests to ensure that they use the same method of building the repository - Thy do.
5) The only difference is during the following Function of DatabaseContext:
Public Overloads Function [Set](Of T As Class)() As System.Data.Entity.IDbSet(Of T) Implements Interfaces.IDatabaseContext.Set
Return MyBase.Set(Of T)()
End Function
The WorkOrderHeader code has the following in the watch for MyBase.Set(Of T)()
{System.Data.Entity.DbSet(Of Insight.Model.WorkOrderHeader)} System.Data.Entity.DbSet(Of Insight.Model.WorkOrderHeader)
The CompanyHeader code has the following in the watch for MyBase.Set(Of T)()
{SELECT [Extent1].[Name] AS [Name], [Extent1].[Active] AS [Active], [Extent1].[Creator] AS [Creator], [Extent1].[Created] AS [Created], [Extent1].[Modifier] AS [Modifier], [Extent1].[Modified] AS [Modified], [Extent1].[ID] AS [ID] FROM (SELECT [vwCompanyHeader].[ID] AS [ID], [vwCompanyHeader].[Name] AS [Name], [vwCompanyHeader].[Active] AS [Active], [vwCompanyHeader].[Creator] AS [Creator], [vwCompanyHeader].[Created] AS [Created], [vwCompanyHeader].[Modifier] AS [Modifier], [vwCompanyHeader].[Modified] AS [Modified] FROM [dbo].[vwCompanyHeader] AS [vwCompanyHeader]) AS [Extent1]} System.Data.Entity.DbSet(Of Insight.Model.CompanyHeader)
I am at a complete loss on this one and am entering my 3rd day of working on the same problem. I am hoping that some fresh eyes on the problem might see an error that I have made or point me in a direction of testing the problem that I have not thought of.
UPDATE:
I removed the service code to ensure it was something in the DBContext. I have isolated the service out of the equation so that only the repositories are being used and they are all using the same DatabaseContext, so it can't be anything related to the connection string. I verified that in the Model Browser under Insight_Model/EntityContainer:Insight_Entities/Entity Sets/WorkOrderHeaders exists and that under Insight_Model/Entity Types/WorkOrderHeader exists. It almost appears as though the DatabaseContext isn't even attempting to query the conceptual model for the entity set.
After 3 (4) days (depending on how you view days, I separate days by when I sleep and not necessarily when the sun goes down/comes up), I found the answer. In the EDMX file above, I have a column named LeadTakenDate, in the WorkOrderHeader class, I used the field name LeadTaken. This was a stupid error on my part.
How can this benefit you possibly? Here is how I resolved the problem:
First I isolated the problem to exactly one area by MOQing out parts to see how far down the rabbit hole the problem was. Once I figured out the problem was in the DatabaseContext, I wrote a test case that created a working and a non working use case. This isolated the problem and allowed me to test theories to fix it quicker. I tried a thousand different things before trying to use the EF 5.x persistance ignorant poco generator. I commented out all of my code in the WorkOrderHeader class and copied the generated code in. Reran my tests and they worked, so I started uncommenting my properties and commenting out the generated code then rerunning my test until I hit the appropriate property.
The error message is cryptic enough that is can be very difficult to trouble shoot.

Entity Framework with stored procedure using defining query

I wrote a defining query
<EntitySet Name="EntityFramework" EntityType="SEOAnalysisModel.Store.EntityFramework">
<DefiningQuery>
SELECT Keyword, ResultHead ,Year from SeoAnalysis where Year = 2005
</DefiningQuery>
</EntitySet>
And entity type for custom entity
<EntityType Name="EntityFramework">
<Key>
<PropertyRef Name="Year" />
</Key>
<Property Name="Year" Nullable="false" Type="int" />
<Property Name="Keyword" Nullable="false" MaxLength="1000" Type="varchar" />
<Property Name="ResultHead" Nullable="false" MaxLength="2000" Type="varchar" />
</EntityType>
But when I call a stored procedure, it returns only 1 value of the column
// Stored procedure
public void SelectValue() {
using (MyConnection ctx = new MyConnection()) {
foreach (var p in ctx.EntityFramework(2005)) {
Response.Write(p.Keyword);
}
}
}
And this column value is repetitive.
Now how can I get all column value?
Actually I am getting same keyword repetitive.
If I have keyword like Apple, then this keyword is repeating until loop is running.
You have marked Year as key for the entity. Key must be unique among all records in your defining query otherwise it is not a key. If the key is not unique, EF will do exactly what you got - it will materialize only the first record into entity and use it for all other records as well. EF uses key to identify the entity - if you get two records with the same key value, EF believes that it is the same entity!

ef error:{"A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'ShippingCardID'."}

I work in EF.Xml definition table is ...
<EntityType Name="ShippingCards">
<Key>
<PropertyRef Name="ShippingCardID" />
</Key>
<Property Name="ShippingCardID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="ProducedID" Type="int" />
<Property Name="User" Type="int" />
<Property Name="Count" Type="int" />
</EntityType>
methode is definition
public void addProduct(Product product)
{
ShippingCard sc = new ShippingCard();
sc.ProducedID = product.ProductID;
Add(sc);
context.SaveChanges();
}
When you try to put in the product in the cart breaks my mistake
{"A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'ShippingCardID'."}
InnerError:
{"A dependent property in a ReferentialConstraint is mapped to a store-generated column. Column: 'ShippingCardID'."}
You have incorrectly defined relations between entities. This error says that ShippingCardID is used as foreign key (= dependent property) in some relation which is not allowed because only properties with StoreGeneratedPattern.None can be used as foreign keys.