Entity Framework: 0..1-to-many foreign key constraint not recognized? - entity-framework

I am using EF4 to map a DB schema to an object model; I initially generated the EDMX from the database but have been editing the XML directly (trying to leave the SSDL the same while changing the CSDL/MSL to approximate the object model I want). The database contains, among many other things, a couple tables with a (0..)1-to-many constraint via foreign key like so:
go
create table Options (
KitNodeID int primary key foreign key references KitNodes (KitNodeID),
[SKUID] int null foreign key (SKUID) references SKUs (SKUID)
)
go
create table Upgrades (
UpgradeID int identity (1, 1) primary key not null,
[Name] nvarchar(50) not null,
DefaultOptionID int null references Options (KitNodeID)
)
The relevant sections in the EDMX look like this:
[...]
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="DModel.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
<EntityContainer Name="DModelStoreContainer">
<AssociationSet Name="FK__Upgrades__Defaul__70DDC3D8" Association="DModel.Store.FK__Upgrades__Defaul__70DDC3D8">
<End Role="Options" EntitySet="Options" />
<End Role="Upgrades" EntitySet="Upgrades" />
</AssociationSet>
</EntityContainer>
<EntityType Name="Upgrades">
<Key>
<PropertyRef Name="UpgradeID" />
</Key>
<Property Name="UpgradeID" Type="int" Nullable="false" StoreGeneratedPattern="Identity" />
<Property Name="Name" Type="nvarchar" Nullable="false" MaxLength="50" />
<Property Name="DefaultOptionID" Type="int" />
</EntityType>
<Association Name="FK__Upgrades__Defaul__70DDC3D8">
<End Role="Options" Type="DModel.Store.Options" Multiplicity="0..1" />
<End Role="Upgrades" Type="DModel.Store.Upgrades" Multiplicity="*" />
<ReferentialConstraint>
<Principal Role="Options">
<PropertyRef Name="KitNodeID" />
</Principal>
<Dependent Role="Upgrades">
<PropertyRef Name="DefaultOptionID" />
</Dependent>
</ReferentialConstraint>
</Association>
</Schema></edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema Namespace="DModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityContainer Name="DEntities" annotation:LazyLoadingEnabled="true">
<EntitySet Name="Upgrades" EntityType="DModel.Upgrade" />
<AssociationSet Name="Upgrade_DefaultOption" Association="DModel.Upgrade_DefaultOption">
<End Role="Options" EntitySet="Options" />
<End Role="Upgrades" EntitySet="Upgrades" />
</AssociationSet>
</EntityContainer>
<Association Name="Upgrade_DefaultOption">
<End Role="Options" Type="DModel.Option" Multiplicity="0..1" />
<End Role="Upgrades" Type="DModel.Upgrade" Multiplicity="*" />
</Association>
<EntityType Name="Upgrade">
<Key>
<PropertyRef Name="UpgradeID" />
</Key>
<Property Name="UpgradeID" Nullable="false" annotation:StoreGeneratedPattern="Identity" Type="Int32" />
<Property Name="Name" Type="String" Nullable="false" MaxLength="50" Unicode="true" FixedLength="false" />
<NavigationProperty Name="DefaultOption" Relationship="DModel.Upgrade_DefaultOption" FromRole="Upgrades" ToRole="Options" />
<NavigationProperty Name="OptInOptions" Relationship="DModel.OptInOptions" FromRole="Upgrades" ToRole="Options" />
</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="DModelStoreContainer" CdmEntityContainer="DEntities">
<EntitySetMapping Name="Upgrades"><EntityTypeMapping TypeName="DModel.Upgrade"><MappingFragment StoreEntitySet="Upgrades">
<ScalarProperty Name="UpgradeID" ColumnName="UpgradeID" />
<ScalarProperty Name="Name" ColumnName="Name" />
</MappingFragment></EntityTypeMapping></EntitySetMapping>
<AssociationSetMapping Name="Upgrade_DefaultOption" TypeName="DModel.Upgrade_DefaultOption" StoreEntitySet="FK__Upgrades__Defaul__70DDC3D8">
<EndProperty Name="Upgrades">
<ScalarProperty Name="UpgradeID" ColumnName="UpgradeID"/>
</EndProperty>
<EndProperty Name="Options">
<ScalarProperty Name="KitNodeID" ColumnName="DefaultOptionID"/>
</EndProperty>
</AssociationSetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
It will generate code, but when I try to use it I get an "Error 2007: The Table 'FK_Upgrades_Defaul__70DDC3D8' specified as part of this MSL does not exist in MetadataWorkspace." It looks like it can't find the underlying foreign key constraint that the SSDL is supposed to use, but I do see one with that name in the database.
Really, I feel I don't have a very good understanding of how this kind of mapping is supposed to work in general - is a foreign key constraint in the database really treated as an "associationset"? - but this is the closest I can work out. I don't know if anyone could diagnose the problem from the info I've given, but any pointers on where to look? I've tried various things like changing the StoreEntitySet to point to Options, etc. but they just result in different errors.

Try changing how you define your foreign key, see: How do I create a foreign key in SQL Server?
You will probably have to delete and recreate you model.

Related

Receive bytea with asp .net api

First of all, sorry for the probable English mistakes
I created a table in my postgresql database using the entity framework.
In edmx in the creation section I passed as bytea, however in the mapping I used binary (primitive type use is necessary).
<EntityType Name="styleexample">
<Key>
<PropertyRef Name="id" />
</Key>
<Property Name="id" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Name="example" Type="String" MaxLength="40" />
<Property Name="example1" Type="Binary" />
<Property Name="example2" Type="DateTime" />
</EntityType>
<EntityType Name="styleexample">
<Key>
<PropertyRef Name="id" />
</Key>
<Property Name="id" Type="int4" StoreGeneratedPattern="Identity" Nullable="false" />
<Property Name="example" Type="varchar" MaxLength="40" />
<Property Name="example1" Type="bytea" />
<Property Name="example2" Type="timestamp" />
</EntityType>
The table was created with the correct typing
In the entity I referenced the column as byte []
In the column I inserted the base64 string of a png image, but in the service when I return the value is different from what I inserted
My response is also byte[]
It works. However, it returns the incorrect value. Is there a different way to do this process?
I solved the issue by adding to the service
var example1 = new StreamReader(new MemoryStream(estilo.example1));
var example1Stream = example1 .ReadToEnd();

Unable to add custom prefix to generated views

I want to be able to add the prefix "cf_" to all stored procedures and views. I got it working with stored procedures but the same approach does not work with views. See screen shot for how I assigned custom prefixes.
Although I'm including the XML file, I prefer to use the property pages as opposed to editing the XML directly.
<cf:project defaultNamespace="DemoCustomPrefix" xmlns:cf="http://www.softfluent.com/codefluent/2005/1" xmlns:cfx="http://www.softfluent.com/codefluent/modeler/2008/1" xmlns:cfps="http://www.softfluent.com/codefluent/producers.sqlserver/2005/1" xmlns:cfom="http://www.softfluent.com/codefluent/producers.model/2005/1" xmlns:cfasp="http://www.softfluent.com/codefluent/producers.aspnet/2011/1" defaultTargetFramework="4.0" defaultConnectionString="Database=DemoCustomPrefix;Server=.\SQLExpress;Integrated Security=true" persistenceSaveProcedureFormat="cf_{0}_Save" persistenceLoadProcedureFormat="cf_{0}_{1}" persistenceSearchProcedureFormat="cf_{0}_{1}" persistenceDeleteProcedureFormat="cf_{0}_{1}" persistenceRawProcedureFormat="cf_{0}_{1}" viewFormat="cf_{0}" procedureFormat="cf_{0}" createDefaultMethodForms="true" createDefaultApplication="false" createDefaultHints="false">
<cf:import path="Default.Surface.cfp" />
<cf:producer name="SQL Server Producer" typeName="CodeFluent.Producers.SqlServer.SqlServerProducer, CodeFluent.Producers.SqlServer">
<cf:configuration connectionString="Database=DemoCustomPrefix;Server=.\SQLExpress;Integrated Security=true" produceViews="true" targetVersion="Sql2008" targetDirectory="..\DemoCustomPrefix.Persistence" cfx:targetProjectLayout="UpdateItems, DontRemove" cfx:targetProject="..\DemoCustomPrefix.Persistence\DemoCustomPrefix.Persistence.dbproj" />
</cf:producer>
<cf:producer name="BOM Producer" typeName="CodeFluent.Producers.CodeDom.CodeDomProducer, CodeFluent.Producers.CodeDom, Version=1.0.0.0, Culture=neutral, PublicKeyToken=1bb6d7cccf1045ec">
<cf:configuration compileWithVisualStudio="true" targetDirectory="..\DemoCustomPrefix" cfx:targetProject="..\DemoCustomPrefix\DemoCustomPrefix.csproj" />
</cf:producer>
<cf:producer name="ASP.NET WebForms Producer" typeName="CodeFluent.Producers.UI.AspNetProducer, CodeFluent.Producers.UI">
<cf:configuration categoryPath="UI\AspNetWebForms" targetDirectory="..\DemoCustomPrefix.Web" cfx:targetProject="..\DemoCustomPrefix.Web\DemoCustomPrefix.Web.csproj" />
</cf:producer>
<cf:entity name="Widget" namespace="DemoCustomPrefix">
<cf:property name="Id" key="true" />
<cf:property name="Name" />
</cf:entity>
</cf:project>
Figured it out. Just had to select "Custom Formats" under for Naming Convention.

jaysvcutil failing with TypeError: Cannot read property 'schemas' of undefined

I am trying to generate the model from an existing OData service through jaysvcutil and am getting:
[...]\node_modules\jaydata-odatajs\lib\odata\odatautils.js:385
throw err;
^
TypeError: Cannot read property 'schemas' of undefined
at Metadata.processMetadata ([...]\node_modules\jaydata-dynamic-metadata\lib\metadata.js:353:35)
at requestData ([...]\node_modules\jaydata-dynamic-metadata\lib\metadataHandler.js:46:42)
at [...]\node_modules\jaydata-odatajs\lib\odata\odatautils.js:382:13
at IncomingMessage.<anonymous> ([...]\node_modules\jaydata-odatajs\lib\odata\net.js:179:21)
at emitNone (events.js:72:20)
at IncomingMessage.emit (events.js:166:7)
at endReadableNT (_stream_readable.js:921:12)
at nextTickCallbackWith2Args (node.js:442:9)
at process._tickCallback (node.js:356:17)
I tried to condense the domain model into something really simple, so my current $metadata looks like this:
<edmx:Edmx xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx" Version="1.0">
<edmx:DataServices xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" m:DataServiceVersion="1.0">
<Schema xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://schemas.microsoft.com/ado/2007/05/edm" Namespace="data">
<EntityType Name="Product">
<Key>
<PropertyRef Name="ProductId"/>
</Key>
<Property Name="ProductId" Type="Edm.Int64" Nullable="false"/>
<Property Name="ProductNo" Type="Edm.String" Nullable="true"/>
<Property Name="LifeTime" Type="Edm.Int64" Nullable="false"/>
<Property Name="Capacity" Type="Edm.Int64" Nullable="false"/>
<Property Name="PackingSize" Type="Edm.Int64" Nullable="false"/>
<Property Name="CreationTime" Type="Edm.DateTime" Nullable="false"/>
<Property Name="ModificationTime" Type="Edm.DateTime" Nullable="false"/>
</EntityType>
</Schema>
<Schema xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://schemas.microsoft.com/ado/2007/05/edm" Namespace="Namespace">
<EntityContainer Name="DomainData" m:IsDefaultEntityContainer="true">
<EntitySet Name="Product" EntityType="data.Product"/>
</EntityContainer>
</Schema>
</edmx:DataServices>
</edmx:Edmx>
This is an existing WCF DataService based OData provider using OData V1 and I am not able to change that - so please do not recommend updating the provider to V4.
Any help would be appreciated.
As per feedback from the JayData team:
JayData supports OData v4, as there have been multiple breaking changes in both OData and DataJS since v1.
and also
JayData 1.3.7 supports v2 and v3, but it does not support TypeScript and Angular 2.
I will therefore be looking at alternatives, presumably breezejs.

SAP UI5 Smart Table Json Data Usage

I've got a problem about usage of json data in smart table object. I'm getting json data from web service and I set that json data any other variable. How can I bind that variable to EntitySet ? In fact I haven't got any json file in my application folders.
Here is the application sample link that I used for my project;
https://sapui5.netweaver.ondemand.com/explored.html#/sample/sap.m.tutorial.walkthrough.27/code
Here is the my metadata.xml codes;
<edmx:Edmx Version="1.0" xmlns:edmx="http://schemas.microsoft.com/ado/2007/06/edmx">
<edmx:DataServices m:DataServiceVersion="1.0" m:MaxDataServiceVersion="3.0"
xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata">
<Schema Namespace="UserModel" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityType Name="Users">
<Key>
<PropertyRef Name="TKID"/>
<PropertyRef Name="TKTX"/>
</Key>
<Property Name="TKTX" Type="Edm.String" Nullable="false" MaxLength="40" FixedLength="false" Unicode="true"/>
<Property Name="TKID" Type="Edm.Int16" Nullable="false"/>
</EntityType>
</Schema>
<Schema Namespace="ODataWebV2.Northwind.Model" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityContainer Name="UserModelEntities" m:IsDefaultEntityContainer="true" p6:LazyLoadingEnabled="true"
xmlns:p6="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
<EntitySet Name="Users" EntityType="UserModel.Users"/>
<!--How can I bind my json data incoming from webservice ?-->
</EntityContainer>
</Schema>
</edmx:DataServices>
Thanks for your help.

AutoFixture EF entity constraints

Is it possbile to configure AutoFixture so that it adheres the entity constraints [from the EDMX file]?
E.g. Consider a snippet from the CSDL section of my EDMX file:
<EntityType Name="RndtAd">
...
<Property Name="AD" Type="Decimal" Precision="12" Scale="0" Nullable="false" />
<Property Name="USERNAME" Type="String" MaxLength="255" FixedLength="false" Unicode="true" />
<Property Name="VERSION" Type="Decimal" Precision="12" Scale="4" Nullable="false" />
<Property Name="EFFECTIVE_FROM" Type="DateTime" Precision="3" />
<Property Name="EFFECTIVE_FROM_TZ" Type="DateTime" Precision="7" />
<Property Name="EFFECTIVE_TILL" Type="DateTime" Precision="3" />
<Property Name="EFFECTIVE_TILL_TZ" Type="DateTime" Precision="7" />
<Property Name="IS_TEMPLATE" Type="String" MaxLength="1" FixedLength="true" Unicode="false" />
<Property Name="IS_USER" Type="String" MaxLength="1" FixedLength="true" Unicode="false" />
<Property Name="STRUCT_CREATED" Type="String" MaxLength="1" FixedLength="true" Unicode="false" />
<Property Name="AD_TP" Type="String" MaxLength="20" FixedLength="false" Unicode="true" />
<Property Name="PERSON" Type="String" MaxLength="40" FixedLength="false" Unicode="true" />
<Property Name="TITLE" Type="String" MaxLength="20" FixedLength="false" Unicode="true" />
<Property Name="FUNCTION_NAME" Type="String" MaxLength="20" FixedLength="false" Unicode="true" />
<Property Name="COMPANY" Type="String" MaxLength="40" FixedLength="false" Unicode="true" />
<Property Name="STREET" Type="String" MaxLength="40" FixedLength="false" Unicode="true" />
...
What I would like if fixture.Create<RndtAd>() generated randomly an entity where all the previous constraints are satisfied.
What options do I have? All suggestions are welcome.
EDIT. I'm not bound to AutoFixture. If there is another tool which does the job, I'm ok with that too.
AutoFixture has no built-in support for Entity Framework, but during the last couple of years, several people have fought their own battles to integrate the two.
Here's what a Google search turned up for me:
Autofixture and Moq to test Entity Framework project
AutoFixture.AutoEntityFramework
Creating a domain model without circular references in Entity Framework
Using autofixture in my data integration tests to create proxies
How to mockup Entity Framework 6 With Moq & Autofixture
Perhaps you can find some inspiration by looking some of those resources over.
As-is, AutoFixture can't be customized through .EDMX files.