4

I've suddenly started getting the following error in a .NET 4 MVC project.

The number of members in the conceptual type 'OrnaviModel.Task' does not match with the number of members on the object side type 'Ornavi.Core.Objects.Task'. Make sure the number of members are the same.

The project uses an EDMX file, which is used to generate the required data objects. The project was working fine last week, however now I get this error whenever I add a new field to any entity. Removing the new property immediately fixes the error.

The field has also been added to the underlying database table too, so there shouldn't be a mismatch.

Why has my file suddenly stopped generating correctly? I've tried "Run Custom Tool", to no avail. I've tried on a separate PC, no joy. If I open the EDMX file in XML mode, I can see the new property is there, likewise if I browse to the generated class, the property is there.

Please help.. it's driving me crazy!

EDIT: I've still not got an answer to this, so I'm going to post some screenshots to see if it helps:

I've modified the Tasks table, as follows:

enter image description here

I then click "Generate Database From Model" to refresh the underlying objects. If I expand the DataModel.edmx file, and browse to the relevant class, I can see the new field:

enter image description here

I've added the column to the database too, yet every time I access the page:

The number of members in the conceptual type 'OrnaviModel.Task' does not match with the number of members on the object side type 'Ornavi.Core.Objects.Task'. Make sure the number of members are the same.

Why has this started happening?

I've tried the following: - Run custom tools on the edmx - Make the changes on a different computer - Tried different tables, columns, variable types

Nothing has changed on the laptop - same version of visual studio, no updates have been installed.

Every time, the same issue. If I delete the column, click "Generate Database From Model", it then works fine. Add it back in, same problem.

Please help! The database structure is massive on this project, so I really don't fancy having to recreate the whole thing!

enter image description here

Gavin Coates
  • 1,366
  • 1
  • 20
  • 44
  • What kind of entity types you're using (POCO or EntityObjects)? Somehow you may want to review code generation strategy (especially in navigation properties & association multiplicity) which EF currently using, because your issue clearly indicates EF generated another column properties with duplicate names (but prefixed with underscores instead). – Tetsuya Yamamoto Mar 19 '18 at 04:44
  • @TetsuyaYamamoto - ive added a new photo showing the generation settings. It's not creating duplicates - it's creating public and private properties with getters/setters – Gavin Coates Mar 19 '18 at 15:26
  • *"If I open the EDMX file in XML mode, I can see the new property is there"* It should be in 3 places - under `edmx:StorageModels`, `edmx:ConceptualModels` and `edmx:Mappings`. From error message looks like one (or more) of them is missing. – Ivan Stoev Mar 19 '18 at 15:38
  • @IvanStoev - I've checked the XML, the new field is present in all 3 sections – Gavin Coates Mar 20 '18 at 10:57
  • I have played it a lot and what I have find that when I am adding a property in entity with datatype bool and generate database from model I showing me that entity as datatype of bit. – Nikhil Ghuse Mar 23 '18 at 10:54

6 Answers6

2

It could be that you have two versions of the model in different projects like the resolution mentioned in this comment from a similar issue:

Number of members in conceptual type does not match with number of members on object side type

Jason G.
  • 790
  • 1
  • 6
  • 17
  • This is exactly the problem I was having! There are two models in the project, an older one that is slowly being phased out, with the code being transferred to a DLL containing a newer model. The new column was only added in one of them, causing the error. – Gavin Coates Mar 26 '18 at 13:15
0

Note that The error means that the generated classes representing your model are not in sync with your model definition xml.

Use one of the following approaches:

  • Try regenerating them (either by moving something around in the EF designer or right clicking on the edmx file and selecting Run Custom Tool).
  • Open your model as XML. Remove all references to that type from the CSDL. Save and close, then reopen in the GUI. Now you should be able to update model as usual. If that doesn't work, do the same thing, but remove from MSL as well.
Amirhossein Mehrvarzi
  • 18,024
  • 7
  • 45
  • 70
0

enter image description here

Thanks @Gert Now I got it as it is model-first approach.

So I think issue is mapping of table datatype and your entity datatype try and set datatype as bit in the database

Try to generate database from model if not able to do it set datatype of the Test column in table as BIT.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nikhil Ghuse
  • 1,258
  • 14
  • 31
0

From far as I understand your question, it can be explained briefly

The new column exists in the database table, but it does not currently exist in the data model class. You must update the model to include your new column. In the Models folder, open the ContosoModel.edmx file to display the model diagram. Notice that the Student model does not contain the MiddleName property. Right-click anywhere on the design surface, and select Update Model from Database.

You can find more detail at EF Database First with ASP.NET MVC: Changing the Database.

Hope it help.

Kim C
  • 11
  • 1
0

What I understand is that you are using Model-First approach.

I would like to share one historical issue which I faced in .NET 4 MVC project -> Mine was database first approach, and whenever we added a new column in DB table, we faced the similar issue when we were refreshing the model.

What I feel is, the same bug is present in your Model first approach as well.

In our case, we did resolve it by completing dropping the .edmx model and then regenerating it. (In DB-first it was easy to do).

However, in your case, I would like to suggest some troubleshooting approach ->

  1. Try searching few other already existing properties like "CreatedById" in your complete solution and see in how many places it is present.
  2. Now search for your new "Test" property and match if it is missing somewhere.
  3. If you find it missing it somewhere, then try adding it manually in that file and re-compile your solution.

If this works, then as I already said updating Model<-->DB changes is a little buggy.

Koder101
  • 844
  • 15
  • 28
0

I simply change my datetime on my pc .it ok. problem is: I recently restored Database from backup , before i added some columns in a Tablet & work with module. That Etisiting concertual model columns missed in my new restored Table. I restored correct DB from Backup & it worked to me!