How to write mappings for a stored procedure - c#

There is an excellent post on how to map return values for a stored procedure call here:
http://elegantcode.com/2008/11/23/populating-entities-from-stored-procedures-with-nhibernate/
The mapping in this example has been done through hbm files.
I am trying to use the latest version of Nhibernate (3.2) where we can do mapping through code. I really want to find out the C# code that would create a mapping like below:
<sql-query name="GetProductsByCategoryId">
<return class="Product">
<return-property column="ProductID" name="Id" />
<return-property column="ProductName" name="Name" />
<return-property column="SupplierID" name="Supplier" />
<return-property column="CategoryID" name="Category" />
<return-property column="QuantityPerUnit" name="QuantityPerUnit" />
<return-property column="UnitPrice" name="UnitPrice" />
<return-property column="UnitsInStock" name="UnitsInStock" />
<return-property column="UnitsOnOrder" name="UnitsOnOrder" />
<return-property column="ReorderLevel" name="ReorderLevel" />
<return-property column="Discontinued" name="Discontinued" />
</return>
exec dbo.GetProductsByCategoryId :CategoryId
</sql-query>

To be honest I never tried it, by you should take a look to the extension method AddNamedQuery(..): you call it from you Configuration instance (NHibernate.Cfg namespace)).
Some examples on the NHibernate test project.
By the way, you can mix the new 3.2 mapping-by-code and xml one.
Start to look at this question;

Related

Retrieve "Applicable When" and "Success Criteria" from Dynamics CRM SLA programmatically

Currently developing a console application to check all the SLAs in the Dynamics 365 instance.
How to retrieve the "applicable when" and "success criteria" from Dynamics CRM SLA programmatically?
That information is stored in the applicablewhenxml and successconditionsxml fields of the slaitem entity. You can use the following FetchXML to retrieve them:
<fetch>
<entity name="slaitem" >
<attribute name="applicablewhenxml" />
<attribute name="successconditionsxml" />
</entity>
</fetch>
As you can imagine from the names, the information is stored as XML. For example, for a success condition of Status equals to Active:
<and>
<condition>
<column id="colEntity" value="incident" />
<column id="colAttribute" value="statecode" />
<column id="colOperator" value="eq" />
<column id="colStaticValue" value="0" dataslugs="" />
</condition>
</and>

WCF - OData: How to access an operation

I am using a child class of the System.Data.Services.Client.DataServiceContext gerated by the "adding a Service Reference" in VisualStudio to access data via an OData service. The definition in the $metadata.xml of the service includes a custom operation:
<schema Namespace="dotNetBF.OData" xmlns="http://schemas.microsoft.com/ado/2006/04/edm">
<EntityContainer Name="OData" m:IsDefaultEntityContainer="true">
<EntitySet Name="SewageArea" EntityType="GeoMan.SealingRegister.SewageArea" />
<EntitySet Name="LandParcel" EntityType="GeoMan.SealingRegister.LandParcel" />
...
<AssociationSet Name="GeoMan_SealingRegister_SewageArea_LandParcel_GeoMan_SealingRegister_LandParcel_LandParcelPartnerSet" Association="GeoMan.SealingRegister.GeoMan_SealingRegister_SewageArea_LandParcel_GeoMan_SealingRegister_LandParcel_LandParcelPartner">
<End Role="LandParcelPartner" EntitySet="SewageArea" />
<End Role="LandParcel" EntitySet="LandParcel" />
</AssociationSet>
...
<FunctionImport Name="LandParcelByRegistrationYear" EntitySet="LandParcel" ReturnType="Collection(GeoMan.SealingRegister.LandParcel)">
<Parameter Name="registrationYear" Mode="In" Type="Edm.String" />
</FunctionImport>
</EntityContainer>
</Schema>
How can I access the defined function LandParcelByRegistrationYear through the DataServiceDeviceContext, commit a value for parameter registrationYearand consume the result.
I already imported the $metadata.xml but cannot find a method given the name of the operation.

Is it possible to have multiple controllers for a single entity type?

I have an entity framework model. Let's say we have an object Foo and an object Bar. They are related, so Foo has a navigation property to Bar, and vice versa.
Now, for my OData endpoint, I'd like to have two collections available for Foo, for instance declared like so:
var builder = new ODataConventionModelBuilder { Namespace = "Test" };
builder.EntitySet<Foo>("Fools");
builder.EntitySet<Foo>("Footballs");
builder.EntitySet<Bar>("Bars");
The idea here is that access to Fools would go through the FoolsController and access to Footballs would go through the FootballsController, so that I could return different sets of data in each endpoint.
Trying to do this, however, causes the following NotSupportedException error message:
Cannot automatically bind the navigation property 'FooThing' on entity type 'Foo' for the entity set or singleton 'Bars' because there are two or more matching target entity sets or singletons. The matching entity sets or singletons are: Fools, Footballs.
I sort of understand the issue, but if I know for a fact that only Footballs will have Bars, is there a way for me to help the system understand that Bars will only have Footballs?
The answer is absolutely yes.
There are many fluent APIs that you can call to set the binding manually, then to suppress the convention binding. For example:
HasManyBinding
HasRequiredBinding
HasOptionalBinding
HasSingletonBinding
...
Based on your information, you can call the following to make the binding manually:
builder.EntitySet<Bar>("Bars").HasRequiredBinding(b => b.FooThing, "Fools");
I also create a simple Foo and Bar class model to test. The below result shows the metadata:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="4.0" xmlns:edmx="http://docs.oasis-open.org/odata/ns/edmx">
<edmx:DataServices>
<Schema Namespace="WebApiTest" xmlns="http://docs.oasis-open.org/odata/ns/ed
m">
<EntityType Name="Foo">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
</EntityType>
<EntityType Name="Bar">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Edm.Int32" Nullable="false" />
<NavigationProperty Name="FooThing" Type="WebApiTest.Foo" Nullable="fals
e" />
</EntityType>
</Schema>
<Schema Namespace="Test" xmlns="http://docs.oasis-open.org/odata/ns/edm">
<EntityContainer Name="Container">
<EntitySet Name="Fools" EntityType="WebApiTest.Foo" />
<EntitySet Name="Footballs" EntityType="WebApiTest.Foo" />
<EntitySet Name="Bars" EntityType="WebApiTest.Bar">
<NavigationPropertyBinding Path="FooThing" Target="Fools" />
</EntitySet>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
As you can see, "HasRequiredBinding" can make the navigation property as non-nullable, while, "HasOptionBinding" can make it nullable.
Hope it can help. Thanks.

Invalid cast exception is raised when insert is performed into column of type SYSTIMESTAMP WITH TIME ZONE with default value

Could you please assist with next:
I have a table with column of type SYSTIMESTAMP WITH TIME ZONE and with default value - SYSTIMESTAMP. I use Entity Framework 5 database first. When I try to insert entity in this table I get an exception. The exception message is “A store-generated value of type 'System.DateTime' could not be converted to a value of type 'System.DateTimeOffset' required for member 'INSERT_DATE' of type 'Model.SAMPLE'.”. The inner exception message is: “Invalid cast from 'System.DateTime' to 'System.DateTimeOffset'.”
I use ODAC 12.1012 unmanaged driver.
How should I solve this problem?Sample table script:
CREATE TABLE "TEST"."SAMPLE"
( "ID" NUMBER NOT NULL ENABLE,
"INSERT_DATE" TIMESTAMP (6) WITH TIME ZONE DEFAULT systimestamp,
"TEXT_COLUMN" VARCHAR2(200 BYTE),
CONSTRAINT "SAMPLE_PK" PRIMARY KEY ("ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "SYSTEM" ENABLE
) SEGMENT CREATION IMMEDIATE
PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 65536 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT FLASH_CACHE DEFAULT CELL_FLASH_CACHE DEFAULT)
TABLESPACE "SYSTEM" ;
edmx file content:
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="Model.Store" Provider="Oracle.DataAccess.Client" ProviderManifestToken="11.2" Alias="Self" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm/ssdl">
<EntityType Name="SAMPLE">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Name="ID" Type="number" Precision="38" Scale="0" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="INSERT_DATE" Type="timestamp with time zone" Precision="6" StoreGeneratedPattern="Identity" />
<Property Name="TEXT_COLUMN" Type="varchar2" MaxLength="200" />
</EntityType>
<EntityContainer Name="ModelStoreContainer">
<EntitySet Name="SAMPLE" EntityType="Self.SAMPLE" Schema="TEST" store:Type="Tables" />
</EntityContainer>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="Model" Alias="Self" annotation:UseStrongSpatialTypes="false" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns:customannotation="http://schemas.microsoft.com/ado/2013/11/edm/customannotation" xmlns="http://schemas.microsoft.com/ado/2009/11/edm">
<EntityContainer Name="vm2" annotation:LazyLoadingEnabled="true">
<EntitySet Name="SAMPLE" EntityType="Model.SAMPLE" />
</EntityContainer>
<EntityType Name="SAMPLE">
<Key>
<PropertyRef Name="ID" />
</Key>
<Property Name="ID" Type="Decimal" Nullable="false" Precision="38" Scale="0" annotation:StoreGeneratedPattern="Identity" />
<Property Name="INSERT_DATE" Type="DateTimeOffset" Precision="9" annotation:StoreGeneratedPattern="Identity" />
<Property Name="TEXT_COLUMN" Type="String" MaxLength="200" FixedLength="false" Unicode="false" />
</EntityType>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping Space="C-S" xmlns="http://schemas.microsoft.com/ado/2009/11/mapping/cs">
<EntityContainerMapping StorageEntityContainer="ModelStoreContainer" CdmEntityContainer="vm2">
<EntitySetMapping Name="SAMPLE">
<EntityTypeMapping TypeName="Model.SAMPLE">
<MappingFragment StoreEntitySet="SAMPLE">
<ScalarProperty Name="TEXT_COLUMN" ColumnName="TEXT_COLUMN" />
<ScalarProperty Name="INSERT_DATE" ColumnName="INSERT_DATE" />
<ScalarProperty Name="ID" ColumnName="ID" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
Thanks in advance!

Error in mapping fragments starting at line 1131:Potential runtime violation of table key.. on the conceptual side but they do not for

I'm using EF in my applicaiton
I get the following error:
Error 1 Error 3002: Problem in mapping fragments starting at line
1131:Potential runtime violation of table CTIDS's keys (CTIDS.CTID,
CTIDS.Carrier): Columns (CTIDS.CTID, CTIDS.Carrier) are mapped to
EntitySet CTIDS's properties (CTIDS.CTID1, CTIDS.Carrier) on the
conceptual side but they do not form the EntitySet's key properties
(CTIDS.AppVersion, CTIDS.CTID1, CTIDS.Carrier).
D:\MaM\Server\ClientServices\Dev\ClientService
1.6\Conduit.Mam.ClientService.DAL.EntityFramework\MamModel.edmx 1132 15 Conduit.Mam.ClientService.Common.EntityFramework
double clicking the error leads to this part in the edmx:
<EntitySetMapping Name="CTIDS">
<EntityTypeMapping TypeName="MaMDBModel.CTID">
<MappingFragment StoreEntitySet="CTIDS">
<ScalarProperty Name="Carrier" ColumnName="Carrier" />
<ScalarProperty Name="AppVersion" ColumnName="AppVersion" />
<ScalarProperty Name="CTID1" ColumnName="CTID" />
</MappingFragment>
</EntityTypeMapping>
I have a table with a composite_PK
which is exactly what reflected in my edmx:
<EntityType Name="CTIDS">
<Key>
<PropertyRef Name="CTID" />
<PropertyRef Name="Carrier" />
</Key>
<Property Name="CTID" Type="varchar" Nullable="false" MaxLength="50" />
<Property Name="AppVersion" Type="varchar" Nullable="false" MaxLength="50" />
<Property Name="Carrier" Type="int" Nullable="false" />
</EntityType>
You only posted the entity describing the store but not the conceptual entity which is mapped to the store entity. From the exception message it appears that the conceptual entity has a composite key that consists of 3 properties (CTIDS.AppVersion, CTIDS.CTID1, CTIDS.Carrier) while the store entity (the one you showed) has a composite entity consisting of 2 properties (CTIDS.CTID, CTIDS.Carrier) which is the reason for the exception.

Categories

Resources