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

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.
c # entity-framework ef-model-first
Mathias lykkegaard lorenzen
source share