Exception while parsing metadata in BreezeJS client - entity-framework

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?

Related

SAPUI5 - mockup server

I'm following this Mockup Server tutorial for SAPUI5. But I'm not able to retrieve the data and set it to my model. Here's what I do:
I have slightly changed the data so, so two things have to be changed:
json data - Person.json:
{ "id":1,
"first_name":"Chris",
"last_name":"Johnston", "email":"cjohnston0#dailymotion.com", "gender":"Male", "ip_address":"119.220.205.173" }
emphasized text
``
<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="PersonsData" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityType Name="Person">
<Key>
<PropertyRef Name="id"/>
</Key>
<Property name="id" Type="Edm.Int16" Nullable="false" />
<Property Name="first_name" Type="Edm.String" Nullable="false" MaxLength="40" FixedLength="false" Unicode="true"/>
<Property Name="last_name" Type="Edm.String" Nullable="false" MaxLength="40" FixedLength="false" Unicode="true"/>
<Property Name="email" Type="Edm.String" Nullable="false"/>
<Property Name="gender" Type="Edm.String" Nullable="false" MaxLength="40" FixedLength="false"/>
<Property Name="ip_address" Type="Edm.String" Nullable="false" MaxLength="40" FixedLength="false" Unicode="true"/>
</EntityType>
</Schema>
<Schema Namespace="databinding.PersonsData.Model" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityContainer Name="PersonEntities" m:IsDefaultEntityContainer="true" p6:LazyLoadingEnabled="true"
xmlns:p6="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
<EntitySet Name="Persons" EntityType="PersonsData.Invoice"/>
</EntityContainer>
</Schema>
</edmx:DataServices>
The SAPUI5 Web IDE says that edmx namespace does not exist.
So I went to check edmx namespace and it does not exists.
Chrome developer tools reports the same error:
Could this be the problem?
I checked and mockserver.init() is triggered.
Did Microsoft moved this namespace somewhere? Cause I was not able to find it.
Thanks
what I see is that you messed up your XML structure. As the error says your XML is invalid.
You've closed the surrounding edmx:Edmx tag at the beginning of the document
You've closed the edmx:DataServices tag too early
Furthermore the attribute name of the property id is in lowercase. Try to make an uppercased Name.
Try to use this xml:
<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="PersonsData" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityType Name="Person">
<Key>
<PropertyRef Name="id"/>
</Key>
<Property Name="id" Type="Edm.Int16" Nullable="false"/>
<Property Name="first_name" Type="Edm.String" Nullable="false" MaxLength="40" FixedLength="false" Unicode="true"/>
<Property Name="last_name" Type="Edm.String" Nullable="false" MaxLength="40" FixedLength="false" Unicode="true"/>
<Property Name="email" Type="Edm.String" Nullable="false"/>
<Property Name="gender" Type="Edm.String" Nullable="false" MaxLength="40" FixedLength="false"/>
<Property Name="ip_address" Type="Edm.String" Nullable="false" MaxLength="40" FixedLength="false" Unicode="true"/>
</EntityType>
</Schema>
<Schema Namespace="databinding.PersonsData.Model" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityContainer Name="PersonEntities" m:IsDefaultEntityContainer="true" p6:LazyLoadingEnabled="true"
xmlns:p6="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
<EntitySet Name="Persons" EntityType="PersonsData.Invoice"/>
</EntityContainer>
</Schema>
</edmx:DataServices>

How does odata know which property is the ID?

In the following example russelwhyte is the id, but how does the provider know to map it to the UserName property?
URL: http://services.odata.org/V4/TripPinServiceRW/People?$format=application/json;odata.metadata=full
Response:
{
#odata.context: "http://services.odata.org/V4/(S(ck3fzk3dze0kmzjcruxiz31i))/TripPinServiceRW/$metadata#People",
#odata.nextLink: "http://services.odata.org/V4/TripPinServiceRW/People?%24format=application%2fjson%3bodata.
metadata%3dfull&%24skiptoken=8",
value:
[
{
#odata.type: "#Microsoft.OData.SampleService.Models.TripPin.Person",
#odata.id: "http://services.odata.org/V4/TripPinServiceRW/People('russellwhyte')",
#odata.etag: "W/"08D17DBDFB9CCAAC"",
#odata.editLink: "http://services.odata.org/V4/TripPinServiceRW/People('russellwhyte')",
UserName: "russellwhyte",
FirstName: "Russell",
LastName: "Whyte",
...
Friends#odata.associationLink: "http://services.odata.org/V4/TripPinServiceRW/People('russellwhyte')/Friends/$ref",
Friends#odata.navigationLink: "http://services.odata.org/V4/TripPinServiceRW/People('russellwhyte')/Friends",
...
},
...
]
}
By overriding GetEntityByKey of the base class EntitySetController
Query the metadata document, you can get the model schema for the service, and in which you can find which properties are the keys/IDs.
For example:
GET: http://services.odata.org/V4/(S(ksn5grnrgbebt44osly5z2vr))/TripPinServiceRW/$metadata
<EntityType Name="Person" OpenType="true">
<Key>
<PropertyRef Name="UserName"/>
</Key>
<Property Name="UserName" Type="Edm.String" Nullable="false">
<Annotation Term="Org.OData.Core.V1.Permissions">
<EnumMember>Org.OData.Core.V1.Permission/Read</EnumMember>
</Annotation>
</Property>
<Property Name="FirstName" Type="Edm.String" Nullable="false"/>
<Property Name="LastName" Type="Edm.String" Nullable="false"/>
<Property Name="Emails" Type="Collection(Edm.String)"/>
<Property Name="AddressInfo" Type="Collection(Microsoft.OData.SampleService.Models.TripPin.Location)"/>
<Property Name="Gender" Type="Microsoft.OData.SampleService.Models.TripPin.PersonGender"/>
<Property Name="Concurrency" Type="Edm.Int64" Nullable="false">
<Annotation Term="Org.OData.Core.V1.Computed" Bool="true"/>
</Property>
<NavigationProperty Name="Friends" Type="Collection(Microsoft.OData.SampleService.Models.TripPin.Person)"/>
<NavigationProperty Name="Trips" Type="Collection(Microsoft.OData.SampleService.Models.TripPin.Trip)" ContainsTarget="true"/>
<NavigationProperty Name="Photo" Type="Microsoft.OData.SampleService.Models.TripPin.Photo"/>
</EntityType>

Sakai Hibernate lazy initialize

I'm having some issues building my database. I have this two hbm mappings:
<class name="br.unicamp.iel.model.Module" table="readinweb_modules">
<id name="id" type="java.lang.Long">
<generator class="increment" />
</id>
<many-to-one name="course" class="br.unicamp.iel.model.Course"
column="course_id" fetch="select" />
<property name="position" type="integer" />
<property name="module_grammar" type="text" />
</class>
<class name="br.unicamp.iel.model.Course" table="readinweb_courses">
<id name="id" type="java.lang.Long">
<generator class="increment" />
</id>
<property name="title" length="255" not-null="true" type="string" />
<property name="idiom" length="255" not-null="true" type="string" />
<property name="description" type="text" />
<set name="courseModules" table="readinweb_modules"
inverse="true" lazy="true" fetch="select">
<key column="id" not-null="true" />
<one-to-many class="br.unicamp.iel.model.Module" />
</set>
</class>
and when I try to access data on my logic bean as:
List modules = new ArrayList(dao.findById(Course.class,
course).getCourseModules());
it gives me a
org.hibernate.LazyInitializationException: failed to lazily initialize
a collection of role: br.unicamp.iel.model.Course.courseModules, no
session or session was closed
We need to see the complete code of the
List modules = new ArrayList(dao.findById(Course.class, course).getCourseModules())
Do you open and close a Session (or a EntityManager) inside the dao.findById method? The session must still be open to resolve lazy relationships

Issue in EF, mapping fragment,no default value and is not nullable

I am developing MVC 3 Applicaiton.
I have Model first approch.
I have Company Entity(Abstract).
Lead and Customer is inherited from the company entity.
When I tried to validate the model, Its gives an errror.
Error 41 Error 3023: Problem in mapping fragments starting at line
70:Column Companies.Status in table Companies must be mapped: It has
no default value and is not nullable.
Here is the mapping of tables.
And Here is the EDMX code in HTML View.
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="Model1.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator">
<EntityContainer Name="Model1StoreContainer">
<EntitySet Name="Companies" EntityType="Model1.Store.Companies" store:Type="Tables" Schema="dbo" />
</EntityContainer>
<EntityType Name="Companies">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false" />
<Property Name="Name" Type="nvarchar(max)" Nullable="false" />
<Property Name="Status" Type="nvarchar(max)" Nullable="false" />
<Property Name="__Disc__" Type="nvarchar" MaxLength="Max" Nullable="false" />
</EntityType>
</Schema></edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="Model1" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
<EntityContainer Name="Model1Container" annotation:LazyLoadingEnabled="true">
<EntitySet Name="Companies" EntityType="Model1.Company" />
</EntityContainer>
<EntityType Name="Company" Abstract="true">
<Key>
<PropertyRef Name="Id" />
</Key>
<Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity" />
<Property Type="String" Name="Name" Nullable="false" />
</EntityType>
<EntityType Name="Lead" BaseType="Model1.Company" >
<Property Type="String" Name="Status" Nullable="false" />
</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="Model1StoreContainer" CdmEntityContainer="Model1Container">
<EntitySetMapping Name="Companies">
<EntityTypeMapping TypeName="IsTypeOf(Model1.Company)">
<MappingFragment StoreEntitySet="Companies">
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="Name" ColumnName="Name" />
<Condition ColumnName="__Disc__" Value="Company" />
</MappingFragment>
</EntityTypeMapping>
<EntityTypeMapping TypeName="Model1.Lead">
<MappingFragment StoreEntitySet="Companies">
<ScalarProperty Name="Id" ColumnName="Id" />
<ScalarProperty Name="Name" ColumnName="Name" />
<ScalarProperty Name="Status" ColumnName="Status" />
<Condition ColumnName="__Disc__" Value="Lead" />
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
</EntityContainerMapping>
</Mapping></edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<edmx:Designer xmlns="http://schemas.microsoft.com/ado/2008/10/edmx">
<edmx:Connection>
<DesignerInfoPropertySet>
<DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly" />
</DesignerInfoPropertySet>
</edmx:Connection>
<edmx:Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild" Value="true" />
<DesignerProperty Name="EnablePluralization" Value="True" />
<DesignerProperty Name="DatabaseGenerationWorkflow" Value="$(VSEFTools)\DBGen\Generate T-SQL Via T4 (TPH).xaml" />
</DesignerInfoPropertySet>
</edmx:Options>
<!-- Diagram content (shape and connector positions) -->
<edmx:Diagrams>
<Diagram Name="Model1" >
<EntityTypeShape EntityType="Model1.Company" Width="1.5" PointX="2.375" PointY="0.875" Height="1.2636116536458335" />
<EntityTypeShape EntityType="Model1.Lead" Width="1.5" PointX="3.375" PointY="2.625" Height="1.0992643229166665" />
<InheritanceConnector EntityType="Model1.Lead" >
<ConnectorPoint PointX="3.125" PointY="2.1386116536458335" />
<ConnectorPoint PointX="3.125" PointY="2.325" />
<ConnectorPoint PointX="4.125" PointY="2.325" />
<ConnectorPoint PointX="4.125" PointY="2.625" />
</InheritanceConnector>
</Diagram>
</edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>
Whats is the issue ?
Put the DefaultValue attribute on the SSDL Status property. It would look like this (I used the empty string):
<Property Name="Status" Type="nvarchar(max)" Nullable="false" DefaultValue=""/>
Since the base entity is abstract you won't be able to create entities of this type so this DefaultValue will not really be used but it should make EF stop complaining.
In your storage model, the Companies.Status column doesn't allow null values.
Since all entities that inherit from Company except the Lead entity will not have the Status property set, you need to either allow null in the Companies.Status column or set a default value to use for the other entities.

EF4 POCO (not using T4): Mapping and metadata information could not be found for EntityType

I have a pretty simple console project with an entity model (containing two simple entities), two handmade POCOs and a handmade Context class. The program fires a simple query against the DB and everything including LazyLoading works fine.
The problem: As soon as i add another Entity data model (even if i add an empty one), the calls to CreateObjectSet in Ef2PlaygroundModel_3Container throw the following exception:
Unhandled Exception: System.InvalidOperationException: Mapping and metadata information could not be found for EntityType 'EF2_Playground.Driver'.
at System.Data.Objects.ObjectContext.GetTypeUsage(Type entityCLRType)
at System.Data.Objects.ObjectContext.GetEntitySetFromContainer(EntityContainer container, Type entityCLRType, String exceptionParameterName)
at System.Data.Objects.ObjectContext.GetEntitySetForType(Type entityCLRType, String exceptionParameterName)
at System.Data.Objects.ObjectContext.CreateObjectSet[TEntity]()
at EF2_Playground.Ef2PlaygroundModel_3Container.get_Drivers() in C:\...\Ef2PlaygroundModel_3Pocos.cs:line 64
at EF2_Playground.Program.Main(String[] args) in C:\...\Program.cs:line 15
Does anyone have an idea about what is going wrong here?
That is the working project:
Ef2PlaygroundModel_3.edmx:
Code Generation Strategy is set to "None"
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema Namespace="Ef2PlaygroundModel_3.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2008" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl">
<EntityContainer Name="Ef2PlaygroundModel_3StoreContainer">
<EntitySet Name="Cars" EntityType="Ef2PlaygroundModel_3.Store.Cars" store:Type="Tables" Schema="dbo"/>
<EntitySet Name="Drivers" EntityType="Ef2PlaygroundModel_3.Store.Drivers" store:Type="Tables" Schema="dbo"/>
<EntitySet Name="CarDriver" EntityType="Ef2PlaygroundModel_3.Store.CarDriver" store:Type="Tables" Schema="dbo"/>
<AssociationSet Name="FK_CarDriver_Car" Association="Ef2PlaygroundModel_3.Store.FK_CarDriver_Car">
<End Role="Car" EntitySet="Cars"/>
<End Role="CarDriver" EntitySet="CarDriver"/>
</AssociationSet>
<AssociationSet Name="FK_CarDriver_Driver" Association="Ef2PlaygroundModel_3.Store.FK_CarDriver_Driver">
<End Role="Driver" EntitySet="Drivers"/>
<End Role="CarDriver" EntitySet="CarDriver"/>
</AssociationSet>
</EntityContainer>
<EntityType Name="Cars">
<Key>
<PropertyRef Name="Id"/>
</Key>
<Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false"/>
<Property Name="Brand" Type="nvarchar(max)" Nullable="false"/>
<Property Name="Model" Type="nvarchar(max)" Nullable="false"/>
<Property Name="ReleaseDate" Type="datetime" Nullable="true"/>
</EntityType>
<EntityType Name="Drivers">
<Key>
<PropertyRef Name="Id"/>
</Key>
<Property Name="Id" Type="int" StoreGeneratedPattern="Identity" Nullable="false"/>
<Property Name="Name" Type="nvarchar(max)" Nullable="false"/>
</EntityType>
<EntityType Name="CarDriver">
<Key>
<PropertyRef Name="Cars_Id"/>
<PropertyRef Name="Drivers_Id"/>
</Key>
<Property Name="Cars_Id" Type="int" Nullable="false"/>
<Property Name="Drivers_Id" Type="int" Nullable="false"/>
</EntityType>
<Association Name="FK_CarDriver_Car">
<End Role="Car" Type="Ef2PlaygroundModel_3.Store.Cars" Multiplicity="1"/>
<End Role="CarDriver" Type="Ef2PlaygroundModel_3.Store.CarDriver" Multiplicity="*"/>
<ReferentialConstraint>
<Principal Role="Car">
<PropertyRef Name="Id"/>
</Principal>
<Dependent Role="CarDriver">
<PropertyRef Name="Cars_Id"/>
</Dependent>
</ReferentialConstraint>
</Association>
<Association Name="FK_CarDriver_Driver">
<End Role="CarDriver" Type="Ef2PlaygroundModel_3.Store.CarDriver" Multiplicity="*"/>
<End Role="Driver" Type="Ef2PlaygroundModel_3.Store.Drivers" Multiplicity="1"/>
<ReferentialConstraint>
<Principal Role="Driver">
<PropertyRef Name="Id"/>
</Principal>
<Dependent Role="CarDriver">
<PropertyRef Name="Drivers_Id"/>
</Dependent>
</ReferentialConstraint>
</Association>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="Ef2PlaygroundModel_3" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
<EntityContainer Name="Ef2PlaygroundModel_3Container" annotation:LazyLoadingEnabled="true">
<EntitySet Name="Cars" EntityType="Ef2PlaygroundModel_3.Car"/>
<EntitySet Name="Drivers" EntityType="Ef2PlaygroundModel_3.Driver"/>
<AssociationSet Name="CarDriver" Association="Ef2PlaygroundModel_3.CarDriver">
<End Role="Car" EntitySet="Cars"/>
<End Role="Driver" EntitySet="Drivers"/>
</AssociationSet>
</EntityContainer>
<EntityType Name="Car">
<Key>
<PropertyRef Name="Id"/>
</Key>
<Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity"/>
<Property Type="String" Name="Brand" Nullable="false"/>
<Property Type="String" Name="Model" Nullable="false"/>
<Property Type="DateTime" Name="ReleaseDate" Nullable="true"/>
<NavigationProperty Name="Drivers" Relationship="Ef2PlaygroundModel_3.CarDriver" FromRole="Car" ToRole="Driver"/>
</EntityType>
<EntityType Name="Driver">
<Key>
<PropertyRef Name="Id"/>
</Key>
<Property Type="Int32" Name="Id" Nullable="false" annotation:StoreGeneratedPattern="Identity"/>
<Property Type="String" Name="Name" Nullable="false"/>
<NavigationProperty Name="Cars" Relationship="Ef2PlaygroundModel_3.CarDriver" FromRole="Driver" ToRole="Car"/>
</EntityType>
<Association Name="CarDriver">
<End Type="Ef2PlaygroundModel_3.Car" Role="Car" Multiplicity="*"/>
<End Type="Ef2PlaygroundModel_3.Driver" Role="Driver" Multiplicity="*"/>
</Association>
</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="Ef2PlaygroundModel_3StoreContainer" CdmEntityContainer="Ef2PlaygroundModel_3Container">
<EntitySetMapping Name="Cars">
<EntityTypeMapping TypeName="IsTypeOf(Ef2PlaygroundModel_3.Car)">
<MappingFragment StoreEntitySet="Cars">
<ScalarProperty Name="Id" ColumnName="Id"/>
<ScalarProperty Name="Brand" ColumnName="Brand"/>
<ScalarProperty Name="Model" ColumnName="Model"/>
<ScalarProperty Name="ReleaseDate" ColumnName="ReleaseDate"/>
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
<EntitySetMapping Name="Drivers">
<EntityTypeMapping TypeName="IsTypeOf(Ef2PlaygroundModel_3.Driver)">
<MappingFragment StoreEntitySet="Drivers">
<ScalarProperty Name="Id" ColumnName="Id"/>
<ScalarProperty Name="Name" ColumnName="Name"/>
</MappingFragment>
</EntityTypeMapping>
</EntitySetMapping>
<AssociationSetMapping Name="CarDriver" TypeName="Ef2PlaygroundModel_3.CarDriver" StoreEntitySet="CarDriver">
<EndProperty Name="Car">
<ScalarProperty Name="Id" ColumnName="Cars_Id"/>
</EndProperty>
<EndProperty Name="Driver">
<ScalarProperty Name="Id" ColumnName="Drivers_Id"/>
</EndProperty>
</AssociationSetMapping>
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<edmx:Designer xmlns="http://schemas.microsoft.com/ado/2008/10/edmx">
<edmx:Connection>
<DesignerInfoPropertySet>
<DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly"/>
</DesignerInfoPropertySet>
</edmx:Connection>
<edmx:Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild" Value="true"/>
<DesignerProperty Name="EnablePluralization" Value="False"/>
<DesignerProperty Name="CodeGenerationStrategy" Value="None"/>
</DesignerInfoPropertySet>
</edmx:Options>
<!-- Diagram content (shape and connector positions) -->
<edmx:Diagrams>
<Diagram Name="Ef2PlaygroundModel_3">
<EntityTypeShape EntityType="Ef2PlaygroundModel_3.Car" Width="1.5" PointX="3.25" PointY="1.625" Height="1.787985026041667"/>
<EntityTypeShape EntityType="Ef2PlaygroundModel_3.Driver" Width="1.5" PointX="5.375" PointY="1.625" Height="1.59568359375"/>
<AssociationConnector Association="Ef2PlaygroundModel_3.CarDriver">
<ConnectorPoint PointX="4.75" PointY="2.422841796875"/>
<ConnectorPoint PointX="5.375" PointY="2.422841796875"/>
</AssociationConnector>
</Diagram>
</edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>
app.config:
<configuration>
<connectionStrings>
<add
name="Ef2PlaygroundModel_3Container"
connectionString="metadata=res://*/Ef2PlaygroundModel_3.csdl|res://*/Ef2PlaygroundModel_3.ssdl|res://*/Ef2PlaygroundModel_3.msl;provider=System.Data.SqlClient;provider connection string="Data Source=.\SqlExpress;Initial Catalog=Ef2PlaygroundModel_3;Integrated Security=True;MultipleActiveResultSets=True""
providerName="System.Data.EntityClient"
/>
</connectionStrings>
</configuration>
Ef2PlaygroundModel_3Pocos.cs:
using System;
using System.Collections.Generic;
using System.Data.Objects;
namespace EF2_Playground
{
public class Car
{
public Car()
{
Drivers = new List<Driver>();
}
public int Id { get; set; }
public string Brand { get; set; }
public string Model { get; set; }
public DateTime? ReleaseDate { get; set; }
public virtual List<Driver> Drivers { get; private set; }
}
public class Driver
{
public Driver()
{
Cars = new List<Car>();
}
public int Id { get; set; }
public string Name { get; set; }
public virtual List<Car> Cars { get; private set; }
}
public class Ef2PlaygroundModel_3Container : ObjectContext
{
public Ef2PlaygroundModel_3Container()
: base("name=Ef2PlaygroundModel_3Container")
{
ContextOptions.LazyLoadingEnabled = true;
}
public IObjectSet<Car> Cars
{
get { return CreateObjectSet<Car>(); }
}
public IObjectSet<Driver> Drivers
{
get { return CreateObjectSet<Driver>(); }
}
}
}
Program.cs:
using System;
namespace EF2_Playground
{
class Program
{
static void Main(string[] args)
{
using (var ctx = new Ef2PlaygroundModel_3Container())
{
foreach (var driver in ctx.Drivers)
{
Console.WriteLine(driver.Name);
foreach (var car in driver.Cars)
{
Console.WriteLine(" drives a {0} - {1} (released on {2})", car.Brand, car.Model, car.ReleaseDate);
}
}
}
}
}
}
And finally Model1.edmx that breaks the whole thing as soon as i add it to the project:
<?xml version="1.0" encoding="utf-8"?>
<edmx:Edmx Version="2.0" xmlns:edmx="http://schemas.microsoft.com/ado/2008/10/edmx">
<!-- EF Runtime content -->
<edmx:Runtime>
<!-- SSDL content -->
<edmx:StorageModels>
<Schema xmlns="http://schemas.microsoft.com/ado/2009/02/edm/ssdl" Namespace="Model1.Store" Alias="Self" Provider="System.Data.SqlClient" ProviderManifestToken="2005">
<EntityContainer Name="Model1TargetContainer">
</EntityContainer>
</Schema>
</edmx:StorageModels>
<!-- CSDL content -->
<edmx:ConceptualModels>
<Schema xmlns="http://schemas.microsoft.com/ado/2008/09/edm" xmlns:cg="http://schemas.microsoft.com/ado/2006/04/codegeneration" xmlns:store="http://schemas.microsoft.com/ado/2007/12/edm/EntityStoreSchemaGenerator" Namespace="Model1" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation">
<EntityContainer Name="Model1Container" annotation:LazyLoadingEnabled="true">
</EntityContainer>
</Schema>
</edmx:ConceptualModels>
<!-- C-S mapping content -->
<edmx:Mappings>
<Mapping xmlns="http://schemas.microsoft.com/ado/2008/09/mapping/cs" Space="C-S">
<Alias Key="Model" Value="Model1"/>
<Alias Key="Target" Value="Model1.Store"/>
<EntityContainerMapping CdmEntityContainer="Model1Container" StorageEntityContainer="Model1TargetContainer">
</EntityContainerMapping>
</Mapping>
</edmx:Mappings>
</edmx:Runtime>
<!-- EF Designer content (DO NOT EDIT MANUALLY BELOW HERE) -->
<edmx:Designer xmlns="http://schemas.microsoft.com/ado/2008/10/edmx">
<edmx:Connection>
<DesignerInfoPropertySet>
<DesignerProperty Name="MetadataArtifactProcessing" Value="EmbedInOutputAssembly"/>
</DesignerInfoPropertySet>
</edmx:Connection>
<edmx:Options>
<DesignerInfoPropertySet>
<DesignerProperty Name="ValidateOnBuild" Value="true"/>
<DesignerProperty Name="EnablePluralization" Value="False"/>
</DesignerInfoPropertySet>
</edmx:Options>
<!-- Diagram content (shape and connector positions) -->
<edmx:Diagrams>
<Diagram Name="Model1"/>
</edmx:Diagrams>
</edmx:Designer>
</edmx:Edmx>
Ok, i guess i've got it. I reduced the second model (the one that brakes the project) to the following:
Class1.cs
using System.Data.Objects.DataClasses;
[assembly: EdmSchemaAttribute()]
Bang! The well known exception appears.
As in so many cases, reading the documentation helps:
Mapping POCO entities is not supported if any mapping attributes are applied to custom data classes, including EdmSchemaAttribute at the assembly level.
Sure, i do not literally add mapping attributes to CUSTOM data classes but that doesn't matter for the EdmSchemaAttribute since that one lives on the assembly level.
Adding the second non-POCO model causes code generation resulting in a class that contains (at least) the EdmSchemaAttribute and that is not supported.
What i've learned: Don't mix POCO and non-POCO models in one assembly.
My guess is that ef is lookig at the wrong model. Is there any chance that the models are using the same namespace? You can modify the CSDLs namespace in the property window. What happens if you modify the csdl namespace of the new, empty model?
In the properties of your *.edmx if you are using POCO, then you should set the CodeGenerationStrategy to none.