Entity Framework - Cannot create multiple DbContexts that reference the same table. - Workaround - entity-framework-6

As per the title, the scenario is: one c-sharp project, with two folders, each containing two EDMX contexts...
Myproj\NSOne\ModelOne.edmx, Myproj\NSTwo\ModelTwo.edmx;
Generally this works well, until you share the same table across both contexts. No problem at design time, Entity Classes get generated under different namespaces, it's only at runtime that you get the following error:
Schema specified is not valid. Errors: The mapping of CLR type to EDM
type is ambiguous because multiple CLR types match the EDM type
'TB_MyTable'. Previously found CLR type 'Namespace.NSOne.TB_MyTable',
newly found CLR type 'Namespace.NSTwo.CIXModel.TB_MyTable'.
While searching for answers, I've come across a Github issue that pretty much say "it is too hard / not trivial" we won't fix.
https://github.com/aspnet/EntityFramework6/issues/362
However, a workaround is to edit the EDMX file, as mentioned in that github issue.
<ConceptualModels>
<Schema Namespace="ConsoleApplication33"
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">
<EntityType Name="Person"
customannotation:ClrType="MyApp.Person, MyApp, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="Int32" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Name="Name" Type="String" MaxLength="Max" FixedLength="false" Unicode="true" />
</EntityType>
<EntityContainer Name="Town" customannotation:UseClrTypes="true">
<EntitySet Name="People" EntityType="Self.Person" />
</EntityContainer>
</Schema>
</ConceptualModels>
My problem / question is, in my project the above xml fragment with "ConceptualModels" is nowhere to be found ?!
Where do I even begin to look ? Within my edmx files (thru visual studio), I can only see, for example,
ModelOne.edmx
ModelOne.Context.tt
ModelOne.Designer.csharp
ModelOne.edmx.diagram
ModelOne.tt
EntityFramework.dll, EntityFramework.SqlServer.dll versions are:
File version: 6.2.61023.0
Product version: 6.2.0-61023

Not really an answer, but it appears Microsoft has abandoned EF Database-first (EDMX), so the next option for me two months after asking the question was to get rid of it myself from my current project.
In future projects I'll also use EF Code-first, given that VS still allows the strongly-typed classes to be generated easily.

ConceptualModels is section in EDMX file.
You just need to open it with XML Text Editor.
Right click
Open with
XML Text editor.

Related

Constraining the use of Resources in FHIR

For a given implementation scenario is it acceptable to limit how resources are used within the system in ways not described by resource profiles.
A number of scenarios :
- explicitly prohibit the use of <contained> resources
- explicitly prohibit the use of <modifierExtensions>
- explicitly prohibit the use of the narrative <text>
Validation on the system could clearly implement the above, but is there away of advertising this restriction on say an "implementation conformance profile"?
Profiles don't say anything about which resources are used general, as in "which resource are supported by a FHIR endpoint or client", since that would be for that server or client to decide. And they report such capabilities in their Conformance statement.
Profiles can limit the resources involved when communicating data between partners: For example, an Observation may normally refer to Patient, Group, Device or Location in its 'subject' property. You may limit these to a subset, and by doing this consistently across Resources, you are effectively restricting the set of Resources exchanging trading partners need to "know" about when they use that Profile (and only that profile).
I think your second bullet misses some text, so I can't comment on that one.
The spec says about Narrative:
Resources SHOULD always contain narrative to support human-consumption as a fallback. However, in a strictly managed trading systems where all systems share a common data model and additional text is unnecessary or even a clinical safety risk, the narrative may be omitted."
If you look at the base profile definitions of a given resource (look at http://www.hl7.org/implement/standards/fhir/observation.profile.xml.html) for example, you'll see that Observation.text is defined there with cardinality 0..1, you may profile this to 0..0 to make this explicit in your profile.
Here's an example Profile showing the tools there are, including Lloyd's suggestion to use XPath:
<Profile xmlns="http://hl7.org/fhir" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://hl7.org/fhir ../../schema/profile.xsd">
<!-- stuff removed -->
<structure>
<type value="Observation"/>
<name value="MyConstrainedObservation"/>
<publish value="true"/>
<!-- again, elements left out -->
<element>
<path value="Observation" />
<constraint>
<key value="shorttext" />
<severity value="error" />
<human value="Must be short text" />
<xpath value="string-length(f:text) < 100" />
</constraint>
</element>
<element>
<path value="Observation.modifierExtension"/>
<definition>
<min value="0" />
<max value="0" />
</definition>
</element>
<element>
<path value="Observation.text"/>
<definition>
<short></short>
<formal></formal>
<min value="0" />
<max value="1" />
<condition value="shorttext" />
</definition>
</element>
<!-- elements left out -->
<element>
<path value="Observation.subject"/>
<definition>
<type>
<code value="Resource(Patient)"/>
<aggregation value="bundled" />
<aggregation value="referenced"/>
</type>
</definition>
</element>
<!-- more stuff -->
</structure>
</Profile>
This profile first defines an XPath constraint limiting the length of a text (just as an example), and goes on to limit the cardinality of Observation.modifierExtension to 0..0, effectively forbidding its use. Furthermore, it limits Observation.subject to only reference Patients (thus you might avoid the use of Device etc. in your exchanges) and specifies that those Patients can only be referenced or bundled (in a message, document or transaction), but may not be included using .
Obviously, what I did here can be done with Observation.text and Observation.contained too. You have both structural (cardinality) and executable (xpath) means to limit the content at your disposal.
I believe you would add this to your conformance document
In profiles, it's also possible to constrain any of these elements through the use of Profile/structure/element/definition/constraint. For example, to prohibit narrative on Patient, you would define a Profile with a structure of type "Patient" and on the root "Patient" element, include a constraint with the xpath "not(f:text)"
This would still need to be done on a per-resource basis though.
Another option would be to define an isModifier extension on your Conformance resource which declares that you don't support isModifier or text or whatever on any resources. Though in practice, that's just going to mean most systems won't know how to read your Conformance resource and thus won't know how to talk to you at all.
Some advice:
Keep in mind that any such restrictions are greatly going to limit your ability to interoperate with the broad community, though they could potentially be appropriate in very limited environments.
With isModifier, there's general recognition that most systems will reject instances containing an isModifier they don't recognize, so if you don't recognize any modifier extensions, rejecting instances that contain them doesn't require any special declaration - it's normal behavior.
As for text, it may be better to place a constraint that says that narrative must be generated rather than prohibiting it altogether. Generated narrative can be safely ignored and you're much more in the spirit of FHIR conformance doing that than rejecting instances with any narrative at all.

Ant property with multiple values each on separate line

I have a property that has to contain long list of strings and to improve readability I would like to define each value (that are quite long) in separate line, something like:
<property name="items" separator=",">
<item>A</item>
<item>B</item>
</property>
as equivalent to
<property name="items" value="A,B" />
Or something similar to <path> + <pathconvert> but not expanding paths.
Is it possible ?
Turns out there are string resources and a generic resource container:
<resources id="items">
<string>A</string>
<string>B</string>
</resources>
<pathconvert property="items" refid="items" pathsep="," />
Not supported by standard ANT.
There is a popular ant-contrib plugin that has a "foreach" task which acts upon comma separated properties, but I prefer to embed a proper programming language. groovy stands out due it's excellent Java and ANT integration.
Examples of list handling:
Change values of list in ANT
How can i pass two variable in one Ant target by spliting commas

zendframework 2 - load initial data and database schema

I am looking for a way to initial our application quickly with current database schema which is defined and seed data to the database in zendframework 2. This has been done quite well in the modern web framework like rails, play!, you name it but I don't know if it's existed in zf2. I don't find any official documentation about this :)
Use Phing for that task. Initializing (installing) your app, should not be done by the app itself but by an external agent like Phing.
Here is an example phing target of how you can install your database and populate it with initial data:
<target name="recreate-db">
<echo message="[Data] Drop and recreate database..." />
<pdosqlexec
url="mysql:host=${db.project.host};dbname=${db.project.dbname}"
userid="${db.project.username}"
password="${db.project.password}"
onerror="abort">
DROP DATABASE IF EXISTS `${db.project.dbname}`;
CREATE DATABASE `${db.project.dbname}`;
</pdosqlexec>
<echo message="[Data] Populate database with schema and start-data ..." />
<pdosqlexec
url="mysql:host=${db.project.host};dbname=${db.project.dbname}"
userid="${db.project.username}"
password="${db.project.password}"
onerror="abort">
<transaction src="${basePath}/db-versioning/schema.sql" />
<transaction src="${basePath}/db-versioning/start-data.sql" />
</pdosqlexec>
</target>

concat as resource collection in zip not working?

I'm trying to use <concat> as a resource collection in a <zip> task and, according to the documentation, this should work. I'd like to do this because some of the files I want to include in the zip need to have some properties expanded, so I will also add a <filterchain> to the <concat> to do this. I'd prefer to do it directly rather than copying to a temp location (with property substitution) and including the copy in the zip file.
However, I can't seem to get <zip> to correctly use the <concat> element.
A simplified example of what I have so far:
<zip destfile="target/dist.zip">
<concat>
<fileset file="CHANGES.txt" />
</concat>
</zip>
This creates a zip file containing several directories all named concat (C: (obviously this is on a Windows machine).
What am I missing?
A colleague and I came up with the answer by looking through the <zip> and <concat> source. There are really two answers:
<concat>'s implementation of the ResourceCollection interface is odd, but we understand why.
There's a way around that.
For #1, while <concat> is a ResourceCollection (like FileSet), under the hood it returns as the name of single Resource it contains a hard-coded value:
"concat (" + String.valueOf(c) + ")";
Look familiar?
The name of resources is normally ignored--except by <zip> and its related tasks, which uses the resource name as the ZipEntry name. Since <concat> returns the odd-looking name, that's what we get in the zip file.
I haven't quite figured out why I get multiple entries, but it doesn't matter: the observation leads to a convoluted solution.
Since I know the name of the ZipEntry I want to create, I can use a <mapper> to give the <concat> resource a name. Here's what I came up with in all its glory:
<zip destfile="target/distribution.zip">
<fileset dir=".">
<exclude name="target/**" />
<exclude name="CHANGES.txt" />
</fileset>
<mappedresources>
<concat>
<fileset file="CHANGES.txt" />
<filterchain>
<expandproperties />
</filterchain>
</concat>
<mergemapper to="CHANGES.txt" />
</mappedresources>
</zip>
As my colleague says "In Ant every problem can be solved using a mapper."
This only works for Ant 1.8+ because <mappedresources> was added in that release.
I'm going to post some comments to the Ant mailing list and suggest a couple of enhancements:
Allow a resource name to be specified as an attribute on <concat> when it's being used as a ResourceCollection.
Throw an exception (and don't create a synthetic value) if getName() is called without having a value specified.
Finally, though not directly related, I do wish <expandproperties> could take a <propertyset> so I can control which properties get substituted.
Do you want the final zip to contain a single file or multiple files? As far as I can see, using concat (when done successfully, which isn't done above) would produce a single file, the result of concatenation of all files in the resource collection.
If you want multiple files rather than concatenation, I think intermediate copy is what you'll need.
From Ant manual for the concat task:
Since Apache Ant 1.7.1, this task can
be used as a Resource Collection that
will return exactly one resource.

Combine JAXB and JAXWS for an imported XML Schema

How can I specify a JAXB Binding for an imported XSD within a WSDL when using wsimport?
I tried following binding, which causes the error "XPath evaluation of //xs:element[#name='isFoobar'] results in an empty target node".
<?xml version="1.0" encoding="UTF-8"?>
<jaxws:bindings xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" wsdlLocation="example.wsdl"
xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc" xmlns:jaxws="http://java.sun.com/xml/ns/jaxws">
<jaxws:bindings node="wsdl:definitions">
<jaxws:bindings node="wsdl:types" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb">
<jaxws:bindings
node="//xs:schema[#targetNamespace='http://www.example.org/']">
<jaxb:globalBindings>
<xjc:serializable uid="10000001" />
</jaxb:globalBindings>
<jaxb:bindings
node="//xs:element[#name='isFoobar']">
<jaxb:typesafeEnumClass name="IsFoobar">
<jaxb:typesafeEnumMember value="01" name="TRUE" />
<jaxb:typesafeEnumMember value="02" name="FALSE" />
</jaxb:typesafeEnumClass>
</jaxb:bindings>
</jaxws:bindings>
</jaxws:bindings>
</jaxws:bindings>
</jaxws:bindings>
Any ideas?
Sorry for the necro-threading, I encountered this problem and although this is one of the first answer that showed up on google with various key word combination it didn't hold the answer I ended up using.
For imported schemas, the easiest way to specify a JAXB binding on an imported XSD within a WSDL is... to treat it as a completely different schema !
Short example :
MyXSD.xsd
<xsd:schema targetNamespace="whatever"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xs:complexType name="ThingThatNeedsToBeBound">
<!-- Whatever this is made of -->
</xs:complexType>
</xsd:schema>
No matter where this xsd is imported (wether it's at the root of the of my wsdl or within a nested import), all I need to write to bind my "ThingThatNeedsToBeBound" in my custom binding is :
customBindings.xml
<jaxb:bindings schemaLocation="Path/To/MyXSD.xsd" node="/xs:schema/xs:complexType[#name='ThingThatNeedsToBeBound']">
<!-- your custom binding -->
</jaxb:bindings>
So, it's just like a regular case, except that you specify the schemaLocation, but then you can consider the imported schema as a whole itself instead of a part of something.
I hope this will help others stumbling upon this problem.
Source : http://www.oracle.com/technetwork/articles/grid/jax-ws-jaxb-customization-082750.html
(Note : in the source, the solution seems way more complicated, so my case might have been simpler than what they described, I found my solution using that none the less !)
I did something similar ages ago, I think you need to specify the node to select with XPath as follows:
//xs:element[#name='isFoobar']/xs:complexType
Or replace xs:complexType with whatever kind of type you are using here. Hopefully it will fix your probelm.
My first try at resolving this was trying to somehow use XPath or multiple jxb:binding elements, but that didn't work. As far as I know the XPath just isn't validated properly against imported schemas unless it would all be rewritten and mashed together with DOM.
So the way I resolved this problem was by using inline customizations in the imported XSD. I didn't test this approach with multiple nested imports, but if you got access and time to modify all the imported XSDs this should work out ok. In my opinion this is only necessary if you need to generate the mapping and can be scrubbed/removed from the XSD once the mapping is done.

Resources