Problems with Entity Framework - Adds "1" to the name of my table?

I have the following model - the first (is this what she called?) The diagram I made. I use T4 to generate classes.

enter image description here

Now I have a problem that forces the Entity Framework to somehow add "1" to the table name of the DatabaseSupporter object. The database was created from this very model, and nothing was changed.

I am trying to execute the following line:

 _entities.DatabaseSupporters.SingleOrDefault(s => s.Id == myId); 

The error I get when executing this line (along with my internal exception below):

An exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in mscorlib.dll but was not processed in the user code.

Invalid object name 'dbo.DatabaseSupporter1'.

I tried to fix the problem with the following Fluent API code (note the second line in the function, which explicitly points the table to "DatabaseSupporter"), but no luck.

 protected override void OnModelCreating(System.Data.Entity.DbModelBuilder modelBuilder) { modelBuilder .Entity<DatabaseSupporter>() .HasOptional(f => f.DatabaseChatSession) .WithOptionalPrincipal(s => s.DatabaseSupporter); modelBuilder .Entity<DatabaseSupporter>() .Map(m => { m.Property(s => s.Id) .HasColumnName("Id"); m.ToTable("DatabaseSupporter"); }); modelBuilder .Entity<DatabaseSupporter>() .HasMany(s => s.DatabaseGroups) .WithMany(g => g.DatabaseSupporters) .Map(m => { m.ToTable("DatabaseSupporterDatabaseGroup"); m.MapLeftKey("DatabaseGroups_Id"); m.MapRightKey("DatabaseSupporters_Id"); }); modelBuilder .Entity<DatabaseGroup>() .HasRequired(g => g.DatabaseChatProgram) .WithMany(c => c.DatabaseGroups); modelBuilder .Entity<DatabaseGroup>() .HasRequired(g => g.DatabaseOwner) .WithMany(o => o.DatabaseGroups); modelBuilder .Entity<DatabaseOwner>() .HasMany(o => o.DatabaseChatSessions) .WithRequired(o => o.DatabaseOwner); base.OnModelCreating(modelBuilder); } 

It should be noted that the Id property for each entity is actually a Guid .

I am using Entity Framework 6.0.2.

Any ideas?

Edit 1 A generated DatabaseSupporter.cs file was created here, containing my DatabaseSupporter object, as noted in the comments.

 //------------------------------------------------------------------------------ // <auto-generated> // This code was generated from a template. // // Manual changes to this file may cause unexpected behavior in your application. // Manual changes to this file will be overwritten if the code is regenerated. // </auto-generated> //------------------------------------------------------------------------------ namespace Coengage.Data.Entities { using System; using System.Collections.Generic; public partial class DatabaseSupporter { public DatabaseSupporter() { this.DatabaseGroups = new HashSet<DatabaseGroup>(); } public bool IsActive { get; set; } public string Username { get; set; } public System.Guid Id { get; set; } public virtual DatabaseChatSession DatabaseChatSession { get; set; } public virtual ICollection<DatabaseGroup> DatabaseGroups { get; set; } } } 

Edit 2 Errors began after I added a many-to-many link between DatabaseSupporter and DatabaseGroup . No Fluent code is also required before this link.

+9
c # entity-framework ef-model-first
source share
6 answers

This is an incorrect display:

 modelBuilder .Entity<DatabaseSupporter>() .Map(m => { m.Property(s => s.Id) .HasColumnName("Id"); m.ToTable("DatabaseSupporter"); }); 

This is a 50% view for Entity Splitting β€” a mapping in which the properties of one object are stored in two (or even more) separate tables connected to each other by relationships in the database. Since the mapping is not complete, you are not even getting the correct mapping for Entity Splitting. In particular, EF believes that the second table containing other properties (which are not explicitly configured in the display fragment) should be named DatabaseSupporter1 . I could reproduce this using EF 6 (which, incidentally, added the Property method to configure individual properties in the display fragment. In earlier versions, this method did not exist (only the Properties method).) Also, one -to-one is not created correctly in database. In my opinion, EF should throw an exception due to incorrect display here, and not silently display the model without frills.

In any case, you probably do not want to split your entity properties into multiple tables, but map them to a single table. Then you should replace the code block above:

 modelBuilder.Entity<DatabaseSupporter>() .Property(s => s.Id) .HasColumnName("Id"); modelBuilder.Entity<DatabaseSupporter>() .ToTable("DatabaseSupporter"); 

The first mapping seems redundant because the Id property will be displayed by default to a column with the same name. The second display is also possibly redundant (depending on whether the pluralization function of the table name is enabled). You can try it without this mapping. In any case, you no longer need to get an exception that complains about the absence of dbo.DatabaseSupporter1 .

+10
source share

I don't have my dev environment here in front of me, but my immediate thoughts are:

FIRST

You are free to speak normally - but is there a plural s in your ID column correctly? And there is no plural (s) of table names? That would be the opposite of convention.

SECOND

EF will automatically add the number to call the name. See a similar question here: Why does EntityFramework add 1 by default to edmx after database entities?

Do you have something hanging up - the code file has been removed from your solution, but is still in your build path? Have you tried searching the source folder using Windows Explorer, not visual studio?

+2
source share

I replicated your model exactly the way you listed it, and I cannot reproduce your problem in DDL that the EDMX surface emits when creating a database from the model.

Could you provide detailed information on exactly how you are going to add your many-to-many relationships between DatabaseGroup and DatabaseSupporter? You say you are trying to add relationships on the edmx surface and NOT through the code, and it crap for your table name?

I added this many-to-many thing from DatabaseGroup to DatabaseSupporter I added this many-to-many thing from DatabaseSupporter to DatabaseGroup

Can you provide the following:

Give up your code base before adding many-to-many relationships. Make sure your EF Fluent API code is not currently included in your project.

  • Generate DDL from this surface and make sure that it is not generated with the name DatabaseSupporters1 (the tab name that it selects at this stage is published. DatabaseSupporter or DatabaseSupporters)

Now right-click DatabaseGroup | Add New | Association

  • Choose DatabaseGroup for the left and DatabaseSupporter for the correct one. Confirm that the name of the association that the developer selects DatabaseGroupDatabaseSupporter [Do not create]
  • Choose DatabaseSupporter for the left and DatabaseGroup for the correct one. Confirm that the name of the association that the developer selects DatabaseSupporterDatabaseGroup [Create]

On the edmx surface, right-click the newly created many-to-many association and click Show in Model Browser

  • Edit the message to enable the displayed options.

Also right-click on the surface and click "Create Database from Model".

  • Edit the message to include the generated DDL. The table should be called [DatabaseSupporters]

(My first inclination is that it will have something to do with your navigation properties, but not quite sure. I really had the Entity Framework for me the same thing in the toy project I was working on, but I remember that this is trivial to fix, and I don’t remember what the root cause was, I seem to remind that this is something like nav properties)

[edit] Wait .....

If I remove many to many that do not fix my problem. However, returning before I added that many-to-many corrects it. The exact code that throws the exception is already shown. If I delete my free mappings completely, this is not the same exception that is thrown ( he throws something about the group and supporter, and most importantly ). I have not tried to recreate the model in an empty project - it takes a lot of time. I have already tried searching for EDMX in Notepad for links - none were found.

(pay attention to my additional emphasis) So, the DatabaseSupporter1 error appeared after you tried your smooth api patch? Get rid of the patch, add many-to-many and give us a real mistake.

... Also, it took me 5 minutes to build this diagram. I would not qualify this as "a lot of time."

+2
source share
  modelBuilder .Entity<DatabaseSupporter>() .HasMany(s => s.DatabaseGroups) .WithMany(g => g.DatabaseSupporters) .Map(m => { m.ToTable("DatabaseSupporterDatabaseGroup"); m.MapLeftKey("DatabaseGroups_Id"); m.MapRightKey("DatabaseSupporters_Id"); }); 

Many turn to the left and right to many. Try the following:

  modelBuilder .Entity<DatabaseSupporter>() .HasMany(s => s.DatabaseGroups) .WithMany(g => g.DatabaseSupporters) .Map(m => { m.ToTable("DatabaseSupporterDatabaseGroup"); m.MapLeftKey("DatabaseSupporters_Id"); m.MapRightKey("DatabaseGroups_Id"); }); 
+1
source share

I think the DatabaseSupporter class has created two times

single name: DatabaseSupporter

other: DatabaseSupporter1

Changed changes are saved in DatabaseSupporter1 and displayed here.

You need to copy the code of the DatabaseSupporter1 class and go through the code to DatabaseSupporter . then remove this class DatabaseSupporter1 .

+1
source share

This problem arose because of the renaming of tables in the diagram, in particular, changes only in capital letters.

If you rename a table by clicking the title on the chart, I think that it checks the name of the entity set before trying to change it, sees that it exists (even if it is the same entity set), and adds 1.

However, if you right-click and open the Properties panel and first rename the Entity Set Name and then change the Name second, the number will not be added.

0
source share

All Articles