Entity Framework with stored procedure using defining query - entity-framework

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!

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}"/>

Exception while parsing metadata in BreezeJS client

Prequel and version info:
BreezeJS client version: 1.5.2
Metadata generated on custom server via design-time EntityFramework model (6.1.2)
BreezeLabs EdmBuilder (1.0.5)
Had problems generating metadata also which can be read here:
How to generate valid metadata from EF DBContext via EdmBuilder for OData v3/BreezeJS
Question/Problem:
Now the breeze client is throwing an error while interpreting the metadata.
Unable to process returned metadata: A nonnullable DataProperty cannot have a null defaultValue. Name: MySuperDuperEnum
The exception occurs in breeze.debug.js at line 7110 - function parseCsdlSimpleProperty(parentType, csdlProperty, keyNamesOnServer).
Whats going wrong here? Do I have wrong metadata?
Here is the metadata:
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:DataServices m:DataServiceVersion="3.0" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<Schema Namespace="ODataMetaDataGenerator" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<EntityType Name="MyObject" p5:ClrType="GreatPersistencyNamespace.Model.GreatStuff.MyObject, BreezeEval, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns:p5="http://schemas.microsoft.com/ado/2013/11/edm/customannotation">
<Key>
<PropertyRef Name="ID"/>
</Key>
<Property Name="ID" Nullable="false" Type="Edm.Int32" p7:StoreGeneratedPattern="Identity" xmlns:p7="http://schemas.microsoft.com/ado/2009/02/edm/annotation"/>
<Property FixedLength="false" MaxLength="Max" Name="Name" Type="Edm.String" Unicode="true"/>
<Property Name="IsActive" Nullable="false" Type="Edm.Boolean"/>
<Property Name="IsGreat" Nullable="false" Type="Edm.Boolean"/>
<Property FixedLength="false" MaxLength="Max" Name="Description" Type="Edm.String" Unicode="true"/>
<Property Name="Address" Nullable="false" Type="ODataMetaDataGenerator.Address"/>
<Property Name="BehaviourType" Nullable="false" Type="ODataMetaDataGenerator.BehaviourType"/>
</EntityType>
<EnumType Name="BehaviourType" UnderlyingType="Edm.Byte" p5:ClrType="GreatPersistencyNamespace.Model.GreatStuff.BehaviourType, BreezeEval, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns:p5="http://schemas.microsoft.com/ado/2013/11/edm/customannotation">
<Member Name="RUDE" Value="1"/>
<Member Name="KIND" Value="2"/>
<Member Name="UNKNOWN" Value="3"/>
</EnumType>
<EntityContainer Name="TheGreatModelContainer" p5:UseClrTypes="true" xmlns:p5="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" >
<EntitySet EntityType="ODataMetaDataGenerator.MyObject" Name="MyObjects"/>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
The exact source of the error is in breeze.debug.js at line 8597 - constructor of DataProperty.
There the defaultValue has to be set and in my case it tries to get the default value from the property dataType.
The dataType at this point looks the following:
{
name: "Undefined",
defaultValue: undefined
}
This happens due to a failure in line 7091 - var dataType = DataType.fromEdmDataType(csdlProperty.type);
because csdlProperty looks like:
{
name: "BehaviourType",
nullable: undefined
type: "ODataMetaDataGenerator.BehaviourType"
}
Which of course is not an Edm-Type and thus fails.. Should the underlying type of the enum be used here?

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 .

Join two related tables into a single entity using 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();

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.