Answered by:
separated interface Entities and Models

Question
-
I'm trying out Entity Framework 4.0 for a project that I'm working on but I'm having issues trying to implement the Separated Interface pattern. I generated my model from the database and then generated self tracking entities. To achieve the separated interface I moved my generated entities to a separate assembly and updated the namespaces. I can now create a new instance of one of the entities and persist the objects but I cannot read objects from the database, when I try I recieve the errors listed below. I'm assuming it is because the edmx is still pointing to the originally generated files that were using a different namespace and assembly. If anyone would like a better description of my problem let me know. I assume that this is fairly common question but I haven't been able to find the answer.
Test method blah.TaskFlow.Test.Unit.UnitTest1.TestMethod4 threw exception:
System.InvalidOperationException: Object mapping could not be found for Type with identity 'TaskFlowModel.Group'.System.Data.Metadata.Edm.MetadataWorkspace.ImplicitLoadFromEntityType(EntityType type, Assembly callingAssembly)
System.Data.Objects.ObjectContext.GetObjectByKey(EntityKey key)
blah.TaskFlow.Test.Unit.UnitTest1.TestMethod4() in J:\Project\TaskFlow\blah.TaskFlow.Test.Unit\UnitTest1.cs: line 112
Thanks,
Tony
Thursday, August 5, 2010 7:32 PM
Answers
-
Tony, the following walkthrough shows how to separate the entity types from the model.
http://msdn.microsoft.com/en-us/library/ee789839.aspx
Best Regards,
Julia
This posting is provided "AS IS" with no warranties, and confers no rights.- Proposed as answer by Michael Sun [MSFT]Microsoft employee Monday, August 16, 2010 1:43 AM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Wednesday, August 18, 2010 1:12 AM
Tuesday, August 10, 2010 6:35 AM
All replies
-
Hello Tony,
Welcome to EF forum!
Do you also copy the context class to the separate assembly?
EF needs the .edmx file loaded into memory and the .edmx contains three key sections: storage model (SSDL), conceptual model (CSDL) and mapping model (MSL). For detail, please see http://msdn.microsoft.com/en-us/library/bb399604.aspx.
From the exception, it seems that EF cannot find the corresponding type to map. So is the context class and the entity classes under the same namespace and assembly?
Good day!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Friday, August 6, 2010 7:24 AM -
Thanks for the response Lingzhi.
I actually have the context and the entities in separate namespaces. I have the context class in the implementation assembly that contains my DAOs and the edmx, and I have the entities in another assembly that contains my entities and data interfaces.
Looking at my edmx the xml looks like this.
<edmx:ConceptualModels>
<Schema Namespace="TaskFlowModel" Alias="Self" xmlns:annotation="http://schemas.microsoft.com/ado/2009/02/edm/annotation" xmlns="http://schemas.microsoft.com/ado/2008/09/edm">
<EntityContainer Name="TaskFlowEntities" annotation:LazyLoadingEnabled="true">
<EntitySet Name="Emails" EntityType="TaskFlowModel.Email" />
<EntitySet Name="Files" EntityType="TaskFlowModel.File" />The entity types should now be TaskFlowModel.Domain.EntityName rather than TaskFlowModel.EntityName also since the entities are in a different assembly I don't know if there is somewhere I have to set that in the edmx...
Thanks
Friday, August 6, 2010 1:18 PM -
Hi Tony,
I would recommend you put the context and entity classes under one namespace. The namespace in the .edmx file does not determine the class namespaces. So it’s fine that context and entities classes have different .NET namespaces.
If it is convenient, could you please send me a demo for further investigation? Thanks a lot! My mail: v-micsun@microsoft.com.
Good day!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Tuesday, August 10, 2010 1:19 AM -
Tony, the following walkthrough shows how to separate the entity types from the model.
http://msdn.microsoft.com/en-us/library/ee789839.aspx
Best Regards,
Julia
This posting is provided "AS IS" with no warranties, and confers no rights.- Proposed as answer by Michael Sun [MSFT]Microsoft employee Monday, August 16, 2010 1:43 AM
- Marked as answer by Michael Sun [MSFT]Microsoft employee Wednesday, August 18, 2010 1:12 AM
Tuesday, August 10, 2010 6:35 AM -
Hi Tony,
I am writing to check the status of the issue on your side. Would you mind letting us know the result of the suggestions?
If you need further assistance, please feel free to let me know. I will be more than happy to be of assistance.
Have a nice day!
Best Regards,
Lingzhi SunMSDN Subscriber Support in Forum
If you have any feedback on our support, please contact msdnmg@microsoft.com
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.Monday, August 16, 2010 1:43 AM