Updating entity framework model after changing the data type of column in database table - entity-framework-6

I am working with entity framework 6 with database 1st approach. I changed the datatype of my columns in the table and after updating my model I got the following error:
Error 1 Error 2019: Member Mapping specified is not valid. The type
'Edm.Int32[Nullable=False,DefaultValue=]' of member 'Warranty' in type
'pjModel.Bill' is not compatible with
'SqlServer.nvarchar[Nullable=False,DefaultValue=,MaxLength=50,Unicode=True,FixedLength=False]'
of member 'Warranty' in type 'pjModel.Store.Bills'.
what I understand that there is some compatibility issue between datatypes of framework and sql server.
Also when I opened the .edmx file with XML viewer, it clearly shows that changes are propagated in the model i.e data type has been updated, but in the diagram view when I right click the particular column it still shows the old datatype.
I can provide more information if required.

The issue has been resolved. I manually change the type from the diagram view and then it get mapped with the model class.

Same problem here, using EF6, after changing on a database table a field from int to bit.
It was not enought to delete property from model designer, and create it again, as the table mapping refered to the old type.
I guess this could be useful for similar cases. The only way to solve it (unless you don't mind deleting entity and updating model from database) is to edit your Entity Data Model as text (by default, DataModel.edmx), locate the entity as <Entity> node inside <Schema> node and find the desired property that indicates the old type. As an example:
<Property Name="MyProperty" Type="int" Nullable="false" />
Simply change Type attribute to the desired type, return to Visual Studio, save the edmx file, and rebuild.

Related

How does Breeze handle database column defaults?

I can't find any info about this in the documentation, so I will ask here. How does breeze handle database column defaults? I have required columns in my database, but there are also default static values supplied for these in the database column definitions. Normally, I can insert null into these columns, and the new records will get the default. However, breeze doesn't seem to be aware of database column defaults, and the entities that have null in these columns fail validation on saving.
Thanks,
Mathias
Try editing the edmx xml by adding StoreGeneratedPattern = "Computed" attribute to the column with default value in the DB.
Edit:
Actually, before doing editing the xml, try setting the StoreGeneratedPattern property to Computed in the model editor itself.
Update:
This was fixed in Breeze 1.4.6 ( or later), available now.
Original Post:
There is currently in a bug in Breeze that should be fixed in the next release, out in about week. When this fix gets in then breeze will honor any defaultValues it finds in the EntityFramework data model.
One problem though is while it is easy to get 'defaultValues' into a Model First Entity Framework model via the properties editor, it's actually difficult to get it into a Code First EF model, unless you use fluent configuration. Unfortunately, EF ignores the [DefaultValue] attribute when constructing Code First model metadata.
One workaround that you can use now is to poke the 'defaultValue' directly onto any dataProperty. Something like:
var customerType = myEntityManager.metadataStore.getEntityType("Customer");
var fooProperty = customerType.getProperty("foo");
fooProperty.defaultValue = 123;

Entity framwork better error explanation

I'm using a databasefirst approach with Entity framwork and MVC 4(asp.net) and do now and then get the following error:
The operation failed: The relationship could not be changed because
one or more of the foreign-key properties is non-nullable. When a
change is made to a relationship, the related foreign-key property is
set to a null value. If the foreign-key does not support null values,
a new relationship must be defined, the foreign-key property must be
assigned another non-null value, or the unrelated object must be
deleted.
Most likly I have an unintentional null-reference which comes is due to any part of the conversion step(from view, to viewmodel, to databaseobject through automapper).
What I would like to know is if there are some way to get the information WHERE the problem is, which foreign key that is causing the issue. I have tried tracing the database but it seems like the application knows before trying to save to database that there are issues. Can I somehow debug this? Get more error info?
These type of errors can be frustrating because they can be hard to track down.
I would start with purging all the data from the database (back it up if required).
Then, open the database in Management Studio and drag and drop the tables onto the visual designer. Then you get a visual clue of what the relationships look like. You might get a surprise!
These type of errors may be occurred if you changed your database schema(model) and your database have some data.
two or more tables have relationships using foreign key.
Possible Solution
drop the database and apply migrations again.

Entity Framework 5 Function Import with Spatial Data

I am creating a model from an existing DB using EF5 with a table containing a geography column. The model for the table works perfectly fine. However if I try to map a stored procedure which simply selects the geography column I cannot map this column in a Function Import. In the Column Information the EDM Type is set to "Not Supported". However the model maps the geography type as a parameter without a problem.
IDE is VS2012 Premium, framework .NET4.5
Any idea's?
I ended up creating the complex type without the my geography column through function import and afterwards added the scalar property of type Geography. Though it wouldn't automatically map the column for me. So I looked through EDMX with the XML editor looking for the mapping details for the function and added the mapping information manually. After that everything worked fine.
Oké, so I found one way to answer this myself. To anyone interested: I've manually created a new Scalar Property of the Geography type on the complex type which results from the SP. Now somehow the value maps. Looks to me the editor is not able to create a property, but mapping an existing property is no problem. To me this seems like a bug in the editor.
Also have a look at this thread: Build error when using VS 11, .NET 4.5 and Entity Framework

Error 11007: Entity type 'tableName' is not mapped. Is it because the primary key of tha table is a identity column?

I am using EF 4 to generate my entities from the database. I get the following error while I am trying to compile my Model assembly (the edmx file in a separate assembly). I get the error only on tables, who have there primary keys as identity columns. Is that an issue? or am I just making wrong judgements?
Error 11007: Entity type 'tableName' is not mapped.
Steps
Using Entity data model wizard I created my Model (edmx file).
Right click the edmx file and Add Code Generation Item to create the entities (using Ado.net self tracking entity generator).
Move the entities to a different assembly by moving the x.tt and edit it to point to the edmx file in a different assembly.
Now the entity and model (edmx) are in different assembles, to enabled me to have a multi tier application.
I am unable to build the Model assembly and I get the error
Error 11007: Entity type 'tableName' is not mapped.
I have checked for solutions online. Yes, all my tables have a primary key and association between them is defined with terms of foreign keys. Any pointers on this?
Thanks,
Dolly

UpdateModel() cannot assign new value to navigation property (entity reference)

This happens in ASP.NET MVC 2, .NET 4 (EF 4). My Address entity has a reference to the Post reference. Zip is the primary key of the Post entity. Another property in Post entity is CityName. In my views I allow users to change the CityName for the address which automatically (via jquery) loads up the corresponding Zip and stores it inside a hidden field.
When posted, both values are posted fine and binded to the Address's Post reference. But UpdateModel() fails to update them. It says that the Zip is part of the entity's Entity Key and cannot be changed.
I would gladly load up the Post entity by the new Zip and manually assign it to the existing Address but for all other properties I stall want to rely on UpdateModel().
How can I achieve that? One would think that in EF4 stuff like this has been resolved..
By default the entity framework generated classes put restrictions on changing primary key values. This is good. You shouldn't change a PK for any reason at all. Changing PKs outside of add scenarios has pretty huge ramifications for state tracking and the general health of your system.
To solve this problem you want to tell UpdateModel not to update your primary keys using the exclude parameter.

Resources