EdmRelationshipAttribute error in Entity Framework - entity-framework

I am experiencing the following error when I try to assign a new reference to a navigation property. So far, all of my searching has not really turned up anything useful, so I was wondering if I might get any help from SO on this.
Metadata information for the
relationship
'CustomerModel.FK_Execution_ClientBlock'
could not be retrieved. Make sure that
the EdmRelationshipAttribute for the
relationship has been defined in the
assembly
What is strange is that all of my code works. This is only occurring during a unit test. I assume it's because I'm using a mocking framework (MOQ) to mock up this particular EF object.
We're using the version of EF that comes with .net 3.5 if that helps.

I have previously had a similar problem, in my case the solution was this: MetadataException when using Entity Framework Entity Connection
Basically you need to add the name of the dll to the metadata connection string

Related

Get Model schema to programmatically create database using a provider that doesn't support CreateDatabase

I'm using the SQLite provider for Entity Framework 5 but it doesn't support CreateDatabase and thus cannot auto create the database. (Code First)
Is there a way I can obtain the Model schema at runtime so that I can create the SQL "CREATE TABLE" command myself?
If not at runtime, some other way to obtain the schema so I know how to create the table properly?
Thanks!
A) As for obtaining the model schema at runtime part
(all are earlier posts of mine)
See this one How I can read EF DbContext metadata programmatically?
And How check by unit test that properties mark as computed in ORM model?
Also this one for a custom initializer Programmatic data transformation in EF5 Code First migration
Having said that...
The problem I see is where and at what point you actually have the data available.
Actually I'm quite sure you won't be able to do that at any time.
Because to be able to extract that info you need to have a DbContext running - so db has to be constructed etc. etc.
In the initializer maybe - but using different ways to get that info - the above is not available.
B)
The other way would be to go the way of implementing a provider, generator etc. (e.g. this post).
That way you should get all that info just at the right time from the EF/CF itself.
However I haven't played with that much.
For more info you can check the EF source code
This is more of a 'gathered info' so far - in case it helps you get anywhere with it. Not really a solution. I'll add some more tomorrow.
EDIT:
To get the real database metadata, look into the other DataSpace, this should get you to the right place...
(note: things tend to get less exact from here - as obviously there isn't the right official support)
var ssSpaceSet = objectContext.MetadataWorkspace.GetItems<EntityContainer>(DataSpace.SSpace).First()
.BaseEntitySets
.First(meta => meta.ElementType.Name == "YourTableName");
If you look up in debugger, Table property should have the real table name.
However, reflection might be required.
How I can read EF DbContext metadata programmatically?
How check by unit test that properties mark as computed in ORM model?
Programmatic data transformation in EF5 Code First migration
Entity Framework MigrationSqlGenerator for SQLite
http://entityframework.codeplex.com/
Entity Framework - Get Table name from the Entity
ef code first: get entity table name without dataannotations
Get Database Table Name from Entity Framework MetaData
http://www.codeproject.com/Articles/350135/Entity-Framework-Get-mapped-table-name-from-an-ent

Entity Framework DbSet<?> - Column names of database unknown at compile time

So I have run into a situation where I need to use the Entity Framework (DbContext) with databases whose columns are not known at compile time. Basically I don't have the luxury of hard coding a customTable class to put into the DbSet.
The only thing I can think of for solving this issue is using Reflection.Emit to create a customTable class at run-time based on information I glean by interrogating a database.
I was not able to find any information of solving this issue, but I am very new to working with the Entity Framework so maybe a solution would be more obvious to someone more experienced.
I feel like there should be a cleaner way than using Reflection.Emit to dynamically create a class to feed DbSet.
If I am way off base for the intended use of the Entity Framework that information would be useful as well.
Hi I'm investigating this problem too and I have found what seems like the solution.
http://romiller.com/2012/03/26/dynamically-building-a-model-with-code-first/
tells you how to make a DbContext into which you add a new DbSet using a type as a parameter. You can create this type using the Dynamic Linq Library:
http://weblogs.asp.net/scottgu/archive/2008/01/07/dynamic-linq-part-1-using-the-linq-dynamic-query-library.aspx
It has a method called CreateClass which will allow you to dynamically build a POCO class definition from a list of field names and load it, you can then create a DbSet using the new type you have created.
You can then get a non-generic DbSet from your updated DbContext using
db.Set(type)
where 'type' is a variable holding your new type. This can be worked on using the linq predicates in the dynamic linq library.
Incidentally, my application is for a CMS where new modules can add fields to the core data table for the CMS, and I don't want to use DI as its too inflexible as no given module will be able to provide a type which has all the fields it needs and all the other unknown modules might also need.
James

Serialzing POCO EF Objects in Workflow Persistance

I am trying to persist a workflow that has a EF POCO in it as a variable. When I try to persist (and workflow is trying to serialize the object), I get an error:
Type 'System.Collections.Generic.ICollection`1[MyObject]' cannot be
serialized. Consider marking it with the DataContractAttribute
attribute, and marking all of its members you want serialized with the
DataMemberAttribute attribute. If the type is a collection, consider
marking it with the CollectionDataContractAttribute. See the
Microsoft .NET Framework documentation for other supported types.
Any ideas how to get around this?
Turning it into a list is probably the easiest way to get around this. The problem is it doesnt know how to serialise your collection
I found the answer here:
Get serialization error when try and submit EF4 entity via WCF
It turns out the FixupCollection (in Entities.tt) does not have the DataContract attribute. Once I added that, the issue was resolved.

Entity Framework 4.0: Adding scaler property gives error (property it not mapped: 11009)?

I wanted to add a new property to one of my model (table). Basically its a property that doesn't exist in the database but i need to add it to my model so that the custom generation tool (self tracking entity generator) will create the the property inside the the custom generated file.
I added a scaler property, its a string and called testme but it gives me the following error, Anybody know how i can fix this?
Error 2538 Error 11009: Property 'testme' is not mapped.
I am confused why do i need to map it to a table... its a field that doesn't exist in the table ...
Any help really appreciated
Thanks
Generally, you add un-mapped properties to a partial class instead of via the model. That said, use discretion; un-mapped properties can be confusing, since they mostly can't be used in LINQ to Entities queries.
I encountered this problem and was able to resolve it by deleting the entity (a view) in tne designer and readding it by refreshing from the database. This occured after a major redesign of the database and rewriting the view.
I know this doesn't address your problem, but Googling for this error returns this question. Hopefully this answer will be useful to others who are new to EF and hit this message, like I did.
I've been generating my DB from my conceptual model. If I modify the model without updating the DB, then I see this error message.
At the moment I don't have any data in my model, so simply regenerating the DB from the changed model makes these errors go away.

How to tell what name RIA Services/EF Model uses for Associations?

I'm working on a C#.NET 3.5 WCF RIA Services app and having an issue with my Entity Framework model.
My entity Foo is mapped to a DB table and has a primary key called FooId. My Bar is mapped to a DB view. I've selectively designed this view to generate a composite key in the EF using two of the columns (by making sure they were non-nullable and the others are all nullable. This was done using NULLIF and ISNULL in the view design.)
I'm able to add this view to the model with no problem but I keep running into an issue when I try to map an association between the two. Foo should contain many Bars but I keep getting the following error when I add the association:
Unable to retrieve AssociationType for
association 'FK_Bar_Foo'
According to this page, it looks like this might work if I can properly name the association (since RIA Services looks for specific names.) I've tried several variants of names that match the pattern of other associations with no success. Does anyone know if there's a place I can look to find out what name it's looking for?
Thanks,
After some research I found a workaround to the issue here. The problem with this solution is that you have to repeat it every time the model is updated, which just won't work for me. It appears the current version of the EF doesn't support this type of relation.
The solution I finally went with was to redesign the client to independently call the service and request an entity collection by passing the primary key (FooId) of my Foo type. It's not the best approach (and requires a lot more manual coding) but it does the job. I hope Entity Framework version 4 solves this limitation.