codefluent custom stored procedure - codefluent

I have a custom stored procedure with in parameters that return fields of different tables how I can map this custom stored to an entity? I only want to use like a read only values for a report I don't want to save or something like that I try to add the extra fields to the most similar entity but when I execute the method in code the extra fields are null

Solution 1: Using a view
A view allows to aggregate data from different entities.
<Article>
<Id />
<Name />
<Lines typeName="LineCollection" />
<cf:method name="LoadArticlesByCommand" body="load(string commandName) from ArticleByCommand where CommandName = #commandName" />
<cf:view name="ArticleByCommand" autoLightweight="true">
<ArticleName expression="Name"/>
<ArticleQty expression="Lines.Quantity" />
<CommandName expression="Lines.Command.Name" />
</cf:view>
</Article>
<Command>
<Id />
<Name />
<Lines typeName="LineCollection" />
</Command>
<Line setType="List">
<Article typeName="Article" key="true" />
<Command typeName="Command" key="true" />
<Quantity typeName="int" />
</Line>
http://blog.codefluententities.com/2014/04/22/views-auto-lightweight-and-the-modeler/
https://www.softfluent.com/documentation/Views_PersistentViews.html
Solution 2: Using a lightweight entity
Instead of creating a view, you can can create a lightweight entity that contains only the properties used by the stored procedure.
<cf:entity name="Person" lightweight="true">
<cf:property name="FirstName" typeName="string" />
<cf:property name="lastName" typeName="string" />
<cf:method name="ComputeBalance"
body="load () raw"
rawBody="SELECT 'John' AS FirstName, 'Doe' AS LastName" />
</cf:entity>
Solution 3: Custom mapping
For more specific values or types, a custom method can be provided to map the database values to .NET types. This custom method will be called with a DataReader as parameter, meaning that a developer could do whatever he wants.
<cf:entity name="Sample">
<cf:method name="LoadPair" body="raw" rawBody="SELECT 1234,5678"
returnTypeName="CodeFluent.Runtime.Utilities.Pair<System.Int32,System.Int32>"
cfom:methodName="On{0}" />
<cf:snippet>
private static CodeFluent.Runtime.Utilities.Pair<int,int> OnLoadPair(System.Data.IDataReader reader)
{
return new Pair<int, int>(reader.GetInt32(0), reader.GetInt32(1));
}
</cf:snippet>
</cf:entity>
You can also use OnAfterReadRecord or OnBeforeReadRecord rules

If it is not essential that you map the results of the custom stored procedure to an entity than another option is to use the built in support for DataSets.
http://blog.codefluententities.com/2011/06/22/dataset-support-in-codefluent-entities/
<cf:method name="LoadAllCities" body="raw" returnTypeName="System.Data.DataSet">
SELECT $Address::City$ FROM $Address$
</cf:method>
.
DataSet ds = Address.LoadAllCities();
foreach (DataTable table in ds.Tables)
{
foreach (DataRow row in table.Rows)
{
Console.WriteLine("City: " + row[0]);
}
}

Upon re-reading you're question I am providing another answer.
In response to the part where you said "I try to add the extra fields to the most similar entity but when I execute the method in code the extra fields are null". The following steps should be able to solve that problem.
Execute one of the automatically created stored procedure in SQL Management Studio.
Execute the stored procedure you manually created.
Verify that the fieldnames returned by both stored procedures match.
I think the above will solve your immediate problem but I still don't like the solution. The reason is that you said you picked the most similar entity. I think that is going to cause problems in the future especially if the stored procedure is not being mapped to all of the entities properties.
I would recommend either lightweight entity, view or DataSet.

Related

How to add pre-defined values on "Found in Environment" field

AzureDevOps has a a filed called "Found In Environment".
I want to add this to my work items, however I would like to setup a predefined list of values that the user should select.
I can do this by adding a new custom filed, but It would be strange to add custom filed for a filed that AzureDevOps already have.
Question: Is there any way to configure a predefined set of values for already existing filed?
Actually, Azure DevOps just support users to customize the values for any system picklist(except the reason field) such as Severity, Activity, Priority, etc. You can refer to here to get the information about customize system picklist values.
As my test, the Found in Environment field only support one default value, not picklist items.
So according to your question, some fileds can configure set of values for already existing field, but not the Found in Environment field.
Or you can use XML to customize your own process and fields value by your rule. Below is a simple sample about how to definition a work item picklist items in XML.
In addition, only after Migrate from tfs to azure devops, you will have edit xml options. About XML, you can refer to here
<FIELD name="Priority" refname="Microsoft.VSTS.Common.Priority" type="Integer" reportable="dimension">
<HELPTEXT>Business importance. 1=must fix; 4=unimportant.</HELPTEXT>
<ALLOWEDVALUES expanditems="true">
<LISTITEM value="1" />
<LISTITEM value="2" />
<LISTITEM value="3" />
<LISTITEM value="4" />
</ALLOWEDVALUES>
<DEFAULT from="value" value="2" />
</FIELD>

Two entities mapped to the same rows

I'm getting the following error on my database first model in Entity Framework:
Error 3032: Problem in mapping fragments starting at lines 3434,
4312:EntityTypes Model.Docent, Model.Student are being mapped to the
same rows in table Attendee. Mapping conditions can be used to
distinguish the rows that these types are mapped to.
While I already added conditions to these models:
<EntityTypeMapping TypeName="IsTypeOf(Model.Student)">
<MappingFragment StoreEntitySet="Attendee">
<ScalarProperty Name="Id" ColumnName="atnId" />
<Condition ColumnName="atnTypeId" Value="1" />
</MappingFragment>
</EntityTypeMapping>
And
<EntityTypeMapping TypeName="IsTypeOf(Model.Docent)">
<MappingFragment StoreEntitySet="Attendee">
<ScalarProperty Name="AvailabilityApprovedByType" ColumnName="atnAvailabilityApprovedByAttId" />
<ScalarProperty Name="Id" ColumnName="atnId" />
<Condition ColumnName="atnTypeId" Value="2" />
</MappingFragment>
</EntityTypeMapping>
Their is a more complex hierarchy, possibly that's the problem. But I'm unsure how to proceed. This a the hierarchy:
Attendee (Abstract)
-> Facility (Type = 3)
-> AttendeeCollection (Abstract)
-> Team (Type = 4)
-> Group (Type = 5)
-> Person (Abstract)
-> Student (Type = 1)
-> Docent (Type = 2)
Well I figured out the problem. The Person entity had mapped scalar properties and associations. The associations where the problem. Because they could be of two types. I could not write in a condition for them because they could map to two properties. So I removed the scalar properties and the table mapping for the Person class altogether.
After that I implemented private versions of these scalar properties on the Docent en Student class. And exposed them via a partial implementation. Where I added them as abstract to the Person class.
I hope this is clear and helps somebody else. Possibly somebody else can write it down more legible.

CodeFluent tries to update a non-nullable foreign key of type bigint with NULL when deleting related entity

In my model I'm using bigint (ulong) as the type for entity keys.
The value 0 must be used for empty keys.
Columns for foreign keys must not be nullable, because in my methods I only want to check for the value 0 and not for null.
Everything works fine, except for the deletion of related entities that are referenced by other entities.
Here is my model:
<cf:entity name="Customer" cfom:bindingList="false">
<!--persistenceIdentity is true, because the corresponding column for this property must be auto incremented by the database.-->
<cf:property name="Id" typeName="ulong" key="true" persistenceIdentity="true" cfps:hint="CLUSTERED" />
<cf:property name="Name" typeName="string" />
</cf:entity>
<cf:entity name="Order" cfom:bindingList="false">
<!--persistenceIdentity is true, because the corresponding column for this property must be auto incremented by the database.-->
<cf:property name="Id" typeName="ulong" key="true" persistenceIdentity="true" cfps:hint="CLUSTERED" />
<!--persistenceNullable is false, because the column for the foreign key must not be nullable.-->
<cf:property name="Customer" typeName="{0}.Customer" persistenceNullable="false" />
</cf:entity>
Here is my code:
Customer customer = new Customer();
customer.Save();
Order order = new Order();
order.Customer = customer;
order.Save();
customer.Delete();
The last statement gives the following error:
Cannot insert the value NULL into column 'Order_Customer_Id', table 'CodeFluentTest.dbo.Order'; column does not allow nulls. UPDATE fails.
This is because the Customer_Delete stored procedure contains the following update statement:
UPDATE [Order] SET [Order].[Order_Customer_Id] = NULL
WHERE ([Order].[Order_Customer_Id] = #Customer_Id)
Of course this will not work, because the Order_Customer_Id column is not nullable.
How can I instruct CodeFluent to put the value 0 instead of NULL into the Order_Customer_Id column?
CodeFluent does not really support object keys that are not nullable, because the implied semantics would be somehow strange. When you delete an object, from an OO perspective, the object is now null, it's identifier does not exist any more, it's not set to a specific 0 or other value. You will probably run into other problems with tweaking the model like this.
That being said, one way to change this behavior is to use the "persistenceUnlink" attribute on the property involved, directly in the XML file. Unfortunately the graphical modeler does not support this (ancient) attribute and will override it everytime you modify the model and save it back using the GUI.
So, what you can do is use a custom aspect to automatically apply this attribute on properties where you want it. Here is a sample code for such an aspect (note the aspect runs on startup because it's really based on XML, not on the in-memory model contrary to most aspects):
<cf:project xmlns:cf="http://www.softfluent.com/codefluent/2005/1" defaultNamespace="unlink">
<cf:pattern name="Unlink Aspect" namespaceUri="http://www.example.com/unlink" preferredPrefix="ul" step="Start">
<cf:message class="_doc">
Sample aspect that removes auto unlink in delete procedures
</cf:message>
<cf:descriptor name="unlink" targets="Property" defaultValue="true" displayName="Unlink" typeName="boolean" description="Determines if this property will be unlinked during delete" category="Unlink Aspect" />
</cf:pattern>
<?code #namespace name="System" ?>
<?code #namespace name="System.Xml" ?>
<?code #namespace name="CodeFluent.Model" ?>
<?code
// use a special utility method to get all elements
// with the given attribute in a given namespace URI
var properties = Project.Package.RootModelPart.SelectElements("unlink", "http://www.example.com/unlink", false);
foreach(var property in properties)
{
// here we set a special attribute not supported by the GUI designer in Visual Studio
property.SetAttribute("persistenceUnlink", "false");
}
?>
</cf:project>
What you must do is:
save this code as a file, for example "unlink.xml" somewhere around your project files.
in the graphical modeler, right-click on the "Aspects" node of the CodeFluent project, and choose "Add existing aspect", browse to your unlink.xml file (it should display the aspect metadata), and press ok.
go back to the design surface in the graphical modeler, click on the property you want to remove persistence link, go to the Visual Studio property grid, choose the blue "aspects and producers properties" tab, and set "Unlink" to False that should now be displayed (default is true).
rebuild, and the stored procedure code should not contain links for this relation any more.

EF 6 query contains unmapped columns, breaking the query

I have an EDMX (Entity Framework 6.1.3) that I'm using to query two different databases. There are some minor differences between the databases but I only want the common columns. I generated the EDMX from Database A, and removed the columns that were not in Database B from the Diagram and regenerated the code.
If I query database B the query contains the columns I removed, although the final SELECT does not. This means that the query fails.
The table mapping shows the columns, but with nothing on the Value/Property side:
The exception is:
System.Data.Entity.Core.EntityCommandExecutionException : An error occurred while executing the command definition. See the inner exception for details.
----> System.Data.SqlClient.SqlException : Invalid column name 'ValidFromDate'.
Invalid column name 'ValidToDate'.
Invalid column name 'LastPulled'.
Invalid column name 'IsCurrent'.
The query that is being sent to the server is:
SELECT TOP (1)
[c].[FirstName] AS [FirstName],
[c].[LastName] AS [LastName],
[c].[HomePhone] AS [HomePhone],
[c].[WorkPhone] AS [WorkPhone],
[c].[MobilePhone] AS [MobilePhone],
[c].[Email] AS [Email],
[c].[Fax] AS [Fax]
FROM (SELECT
[Person].[FirstName] AS [FirstName],
[Person].[LastName] AS [LastName],
[Person].[HomePhone] AS [HomePhone],
[Person].[WorkPhone] AS [WorkPhone],
[Person].[MobilePhone] AS [MobilePhone],
[Person].[Email] AS [Email],
[Person].[Fax] AS [Fax],
[Person].[ValidFromDate] AS [ValidFromDate],
[Person].[ValidToDate] AS [ValidToDate],
[Person].[LastPulled] AS [LastPulled],
[Person].[IsCurrent] AS [IsCurrent]
FROM [dbo].[Person] AS [Person]) AS [c]
As you can see there is an inner-query which contains the additional columns.
At this point I'm kind of stumped as to why this is happening. How do I remove these columns from both sides of the mapping, or otherwise stop EF from putting unwanted columns in ANY part of the query?
When you use the EDMX designer and simply delete a column from an entity - this does not fully remove the column from the EDMX. Assuming you truly want it gone, you can open the EDMX file with a text editor and remove it by hand. To make sure that your manual changes trigger a rebuild of your auto-generated classes, edit it in Visual Studio and you shouldn't have an issue.
Right-click the EDMX in solution explorer
Open With...
XML (Text) Editor
I would expect if you were to open the EDMX, you would find something that looks like:
<EntityType Name="Person">
<Property Name="FirstName" Type="varchar" />
<Property Name="LastName" Type="varchar" />
<Property Name="HomePhone" Type="varchar" />
<Property Name="WorkPhone" Type="varchar" />
<Property Name="MobilePhone" Type="varchar" />
<Property Name="Email" Type="varchar" />
<Property Name="Fax" Type="varchar" />
<Property Name="ValidFromDate" Type="datetime" />
<Property Name="ValidToDate" Type="datetime" />
<Property Name="LastPulled" Type="datetime" />
<Property Name="IsCurrent" Type="short" />
</EntityType>
And you would just remove the bottom 4 columns, save and close, and rebuild the project.
EDIT: In case anyone in the future references this answer, if you do not first delete the column in the designer (like the OP did in this case), there will be two other instances of the column(s) in the XML that you would also need to remove in order for it to compile.
Thanks to #Borophyll for pointing me in roughly the right direction. Although their answer was not the solution to my issue, it did allow me to see the actual issue.
in the EDMX file there is also a entry that looks like this:
<EntitySet Name="Person" EntityType="Self.Person" store:Type="Tables" store:Schema="dbo">
<DefiningQuery>SELECT
[Person].[FirstName] AS [FirstName],
[Person].[LastName] AS [LastName],
[Person].[HomePhone] AS [HomePhone],
[Person].[WorkPhone] AS [WorkPhone],
[Person].[MobilePhone] AS [MobilePhone],
[Person].[Email] AS [Email],
[Person].[Fax] AS [Fax],
[Person].[ValidFromDate] AS [ValidFromDate],
[Person].[ValidToDate] AS [ValidToDate],
[Person].[LastPulled] AS [LastPulled],
[Person].[IsCurrent] AS [IsCurrent]
FROM [dbo].[Person] AS [Person]</DefiningQuery>
</EntitySet>
And that's where the weird sub-query was coming. I think the reason is that the table has no primary key as it is part of a staging database.
I just deleted from the SELECT statement the last four columns and everything worked.

use _trackXXX columns in the business object model

Is there any way to view the persistence tracking fields (_trackLastWriteTime) as properties in my BOM.
I've seen a similar question in your website forums, but due to the updates you made to it, answers are lacking of "code" sections, so they are useless.
Thanks again!
Josep.
You can just declare the following properties to the entity:
<cf:property name="LastWriteTime" typeName="datetime" persistenceName="_trackLastWriteTime" readOnLoad="true" readOnSave="true" persistent="false"/>
<cf:property name="CreationTime" typeName="datetime" persistenceName="_trackCreationTime" readOnLoad="true" persistent="false" />
<cf:property name="CreationUser" persistenceName="_trackCreationUser" readOnLoad="true" persistent="false" />
<cf:property name="LastWriteUser" persistenceName="_trackLastWriteUser" readOnLoad="true" readOnSave="true" persistent="false" />
persistenceName should match the column name. This is the default column name but it could be different in your context. The property name is not important, only the persistence/column name.
persistent = false means the property is not persistent. This instructs the inference pipeline to not create a column corresponding to this property (because in fact it has already created that column).
readOnLoad = true and/or readOnSave = true means all layers will load and/or save this property, although it's been marked as non persistent.