EntityKey properties of type “Geography” not supported. Entity Framework 5, VS 2012 – Database First - visual-studio

Created a SQL Server 2008 database and table with a property called “Coordinates” of type “geography”. In VS 2012 created a project, added references to Microsoft.SqlServer.Types and EF V5. Then, using the ADO.NET Entity Data Model wizard generated the edmx. When I compile in VS 2012 I get the following error:
Error 4 Error 129: The property 'Coordinates' in EntityType 'DBModel.GeoDestination' is not valid. EntityKey properties that are of type 'Geography' are currently not supported.
Many of the samples on the web are “code first” examples that use the type “DbGeography” as a type in their c# code and generate the database from the code resulting in a table with a property of type “geography”.
How do I do this database first and fix up the type? It does not appear that “DBGeography” is in the drop down list of types when using the “Table Mapping” view.
Using EF 5, NET 4.5, VS 2012 SP 1, SQL Server Types

I figured this out. Looks like a bug in the wizard. If the table in SQL does not have a primary key, the wizard automatically generates a key(s) in the resulting EF model. It appears random, sometimes one key is generated and sometimes it makes several properties keys. A Geography data type cannot be a key. It was not a key in the table, but the wizard made it a key.
Hope this response helps others.

Related

entity framework error after attached .mdf file

I attached an .mdf file in SQL Server 2008 and used that database for my entity framework database first project. Below is the error I got
Exception Details: System.Data.MappingException: Schema specified is not valid.
Error 2062: No mapping specified for instances of the EntitySet and AssociationSet in the EntityContainer"
I tried this link: How do I correctly set an association between two objects in the Entity Framework 4 Entitydesigner?
but it did not work for me. can anyone help me what can be the problem.
thanks,
michaeld
This exception usually occures if you have an entity in your model which doesn't mapped to a table (or an object) into the Database.
If you want that your project just starts debugging, Remove all entities from your model, Right-click in model designer and choose Update model from database
If you have an entity which it supposed to be mapped to a table into database, you should create a relative table in database, and map your entity to that table. You also can ghange your approach from db-first to code-first and enable migrations so that EF updates your db according to your model.
If you have an entity and you want to map it to a stored procedure in your db, see here

xtragrid binded to entity framework

my working environment is C#, Visual Studio 12, entity framework 5 DBContext, SQL Server 2008R2.
the generated entities sets are generated as Hash sets.
xtraGrid.DataSource = _order.OrderLines;
when I type data in the new row, data disappears when focus lost.
With using older entity framework, this logic was working.
What type should I choose for my entity set in order to solve my problem.
Thanks.
I changed type from HashSet to BindingList, and it works, God thank you.

Using Entity Framework with Oracle and Mapping Inheritance - Table-per-Type. Trigger updated Id is not being set in subclass table

I am using Oracle with Entity Framework. The database ids are generated using triggers and sequences.
I want to set up Table-per-Type inheritance, as described here. I've set the "CourseId" to be StoreGeneratedPattern of "Identity".
Everything seems to be fine, except when I insert a row. The table representing the base class (in the walkthrough linked above, the Course table) gets a generated ID as expected.
However the table representing the inherited class (in the walkthrough linked above, the OnlineCourse table) has a row inserted with the CourseId set to zero. This violates the foreign key constraint.
I assume it's something to do with the sequence of inserts. I imagine it should work like:
Insert the Course row
Retrieve the newly created CourseId value
Insert the OnlineCourse, using the new CourseId
However, this isn't working because it's just using the default value of the CourseId (zero) to insert into OnlineCourse.
Any ideas how to fix this?
Since #Campinho hasn't written up his suggestion as an answer, I will do it.
This article has some background on a bug in StoreGeneratedPattern in VS2010.
One of very important features in ORM tools is an ability to get data auto-generated by a database during the entity persistence back to your application. The Entity framework supports this feature by setting StoreGeneratedPattern in the configuration of persisted property. The StoreGeneratedPattern setting is available in both SSDL (Store schema definition language) and CSDL (Conceptual schema definition language) parts of the EDMX file. CSDL configuration allows you defining the reloading behavior in the Model-first approach but SSDL part is responsible for generating correct SQL commands which will persist the entity and reload auto-generated properties. Unfortunately for a long time this was the source of all problems.
The feature was very hard to use because of the annoying bug in the Entity designer. When we set the property in the designer, the value was saved only in CSDL part but not in SSDL part of the EDMX file and the feature didn't work until we opened the EDMX file as XML and manually modified SSDL part. This solved the problem but only until we updated our model from the database. The update always deleted whole SSDL part including our manual change so we had to do it again. Any incremental development of our models become a big pain. The workaround was using mapped stored procedures for inserting and updating entities and mapping result sets (returning auto-generated data) from these stored procedures back to the entity. Finally this bug is solved in Visual Studio 2010 SP1 and we can use StoreGeneratedPattern without any problems because the value is correctly set in both CSDL and SSDL parts and it is not overwritten during updating from the database.
If you have this problem install KB2561001.
I installed the hotfix and changed the StoreGeneratedPattern to "None" then back to "Identity" for the relevant Ids. I could clearly see the SSDL section had been updated to indicate the new setting.
Then everything just worked! I'm not sure what's going to be in the patch from Oracle that they think will fix the problem.
I filed a service request with Oracle and it turns out this is a bug in their provider.
BUG 13724992 - EF: ENTITY NOT UPDATED AFTER SAVECHANGES USING STOREGENERATEDPATTERN
A fix for this bug will be released in an upcoming patchset. However, which patchset in will be included in and when that patchset will be released is currently unknown. I will let you know as soon as this fix will be released.

INSERT fails on EF 4.1 Code First Database

I am using Visual Studio 2010 SP1 with MVC3 Tools Update, and EF 4.1 Code First to scaffold my application's database.
Database CRUD operations work fine when testing locally and on a local database. It is not until I migrate the database to web host's SQL Server 2008 R2 and target that hosted database in my connection string that all INSERT operations fail.
Whenever I try to add a new record I get this error: [I]Cannot insert the value NULL into column 'Id', table 'DB_23378_bloomlmsdata.dbo.Courses'; column does not allow nulls. INSERT fails. The statement has been terminated.[/I]
My data models all specify a Primary Identity Key like so:
[Key]
public int Id {get; set;}
...
And in the local database that gets scaffolded, I see a non-nullable Primary Identity Key in each table.
I read somewhere that Entity Framework, when performing an INSERT operation to the database it will first try to insert a record with a null value as the Primary Key. I do not know how to override this behaviour. Also, this does not seem to be a common problem and people usually only run in to this when they use something different than an IDENTITY key. For the record, I had the same problem with EF CodeFirst CTP5 before EF 4.1 was released.
I have been troubleshooting this for 4 weeks.. My web host is up to date with the technology I am using in my application and database. When I try to get help, they are telling me it is a coding issue but my code checks out. Any ideas would be greatly appreciated.
Check the IsIdentity property of the Id column, it should be set to yes. EF doesn't pass a value for the identity column on an insert because it should be auto-generated by the database.

Remove schema from linq datacontexts (dbml)

When I add stored procedures to a linq datacontext, by default visual studio prefixes the stored procedure with the sql schema that it is in. Is there any way to stop this? In our environment, the stored procedures may be moved to other schemas over time, and we will default the schema based on the sql user used to connect. Do I have to do this manually or can I somehow turn off the schema prefixes?
There doesn't appear to be any means of removing the schema from LINQ-to-SQL mapped stored procedures using the designer. All procs are mapped including their schema name, which (to be fair) is probably a good thing for most applications. And as with most things LINQ-to-SQL, change your database at your own peril. Too many database changes required LINQ-to-SQL dbml updates & rebuilds.
Digging deeper, the schema name is stored in the backing dbml XML file, and is then included in the generated designer.cs file which contains the functions / methods which are called.
// dbml
<Function Name="dbo.MyProc" Method="MyProc">
// designer.cs
[global::System.Data.Linq.Mapping.FunctionAttribute(Name="dbo.MyProc")]
public int MyProc()
If you manually edit the dbml XML file to remove the schema & save within Visual Studio, the code will be regenerated like this:
// changed dbml - removed the dbo schema
<Function Name="MyProc" Method="MyProc">
// the resultant generated code in designer.cs
[global::System.Data.Linq.Mapping.FunctionAttribute()]
public int MyProc()
Which worked for my account, logging in with dbo as default schema. I'll leave the testing up to you.
This might be a workable solution - the visual designer still works like this, other items can be added etc without breaking the schema-less function, and the function itself doesn't cause the designer to complain. Best of luck!

Resources