Why am I getting Invalid column name on field names that don't exist in my model? - entity-framework

I am querying an entity with EF 5.0 Code First / DbContext. I am getting the following errors:
Invalid column name 'RetainedEarningsAccount_Key'.
Invalid column name 'PLSummaryAccount_Key'.
However, neither of these fields exist in the model. I assume some default configuration/mapping is happening here, but I don't know what it is. Here is the model class in question:
public GLEntity()
{
this.AccessEntities = new HashSet<AccessEntity>();
this.BankAccountGLAccounts = new HashSet<BankAccountGLAccount>();
this.BatchRestrictionEntities = new HashSet<BatchRestrictionEntity>();
this.BudgetVersions = new HashSet<BudgetVersion>();
this.CalendarCrossRefs = new HashSet<CalendarCrossRef>();
this.ConsolDetails = new HashSet<ConsolDetail>();
this.ConsolHeaders = new HashSet<ConsolHeader>();
this.DefaultBudgetVersions = new HashSet<DefaultBudgetVersion>();
this.Departments = new HashSet<Department>();
this.ExpenseCodeDetails = new HashSet<ExpenseCodeDetail>();
this.GLAccounts = new HashSet<GLAccount>();
this.Journals = new HashSet<Journal>();
this.PostingRules = new HashSet<PostingRule>();
this.Processes = new HashSet<Process>();
this.Properties = new HashSet<Property>();
this.RecurringJournals = new HashSet<RecurringJournal>();
this.RecurringTransactionGLs = new HashSet<RecurringTransactionGL>();
this.EntitiesAlternate = new HashSet<GLEntity>();
this.GLIntercompaniesDestinationEntities = new HashSet<GLIntercompany>();
this.GLIntercompaniesSourceEntities = new HashSet<GLIntercompany>();
this.TransGLs = new HashSet<TransGL>();
}
public System.Guid Key { get; set; }
public string EntityID { get; set; }
public string Description { get; set; }
public Nullable<short> CurrentFiscalYear { get; set; }
public Nullable<short> CurrentPrd { get; set; }
public string EntityType { get; set; }
public string AllowPostingYN { get; set; }
public string NextJournal { get; set; }
public Nullable<System.Guid> CompanyKey { get; set; }
public Nullable<System.Guid> RetainedEarningsAcctKey { get; set; }
public Nullable<System.Guid> PLSummaryAcctKey { get; set; }
public string AccountingType { get; set; }
public string UserCreated { get; set; }
public System.DateTime Created { get; set; }
public string UserEdited { get; set; }
public Nullable<System.DateTime> Edited { get; set; }
public Nullable<System.Guid> AlternateEntityKey { get; set; }
public string TrackJobs { get; set; }
public string TrackUnits { get; set; }
public virtual ICollection<AccessEntity> AccessEntities { get; set; }
public virtual ICollection<BankAccountGLAccount> BankAccountGLAccounts { get; set; }
public virtual ICollection<BatchRestrictionEntity> BatchRestrictionEntities { get; set; }
public virtual ICollection<BudgetVersion> BudgetVersions { get; set; }
public virtual ICollection<CalendarCrossRef> CalendarCrossRefs { get; set; }
public virtual Company Company { get; set; }
public virtual ICollection<ConsolDetail> ConsolDetails { get; set; }
public virtual ICollection<ConsolHeader> ConsolHeaders { get; set; }
public virtual ICollection<DefaultBudgetVersion> DefaultBudgetVersions { get; set; }
public virtual ICollection<Department> Departments { get; set; }
public virtual ICollection<ExpenseCodeDetail> ExpenseCodeDetails { get; set; }
public virtual ICollection<GLAccount> GLAccounts { get; set; }
public virtual ICollection<Journal> Journals { get; set; }
public virtual ICollection<PostingRule> PostingRules { get; set; }
public virtual ICollection<Process> Processes { get; set; }
public virtual ICollection<Property> Properties { get; set; }
public virtual ICollection<RecurringJournal> RecurringJournals { get; set; }
public virtual ICollection<RecurringTransactionGL> RecurringTransactionGLs { get; set; }
public virtual ICollection<GLEntity> EntitiesAlternate { get; set; }
public virtual GLEntity EntityAlternate { get; set; }
public virtual ICollection<GLIntercompany> GLIntercompaniesDestinationEntities { get; set; }
public virtual ICollection<GLIntercompany> GLIntercompaniesSourceEntities { get; set; }
public virtual ICollection<TransGL> TransGLs { get; set; }
public virtual GLAccount RetainedEarningsAccount { get; set; }
public virtual GLAccount PLSummaryAccount { get; set; }
}
And here is the mapping:
public GLEntity_Mapping()
{
this.HasKey(t => t.Key);
this.ToTable("tblEntity");
this.Property(t => t.Key).HasColumnName("KeyGUID");
this.Property(t => t.EntityID).HasColumnName("EntityID").IsUnicode(false).HasMaxLength(10);
this.Property(t => t.Description).HasColumnName("Description").IsUnicode(false).HasMaxLength(50);
this.Property(t => t.CurrentFiscalYear).HasColumnName("CurrentFiscalYear");
this.Property(t => t.CurrentPrd).HasColumnName("CurrentPrd");
this.Property(t => t.EntityType).HasColumnName("EntityType").IsUnicode(false).IsFixedLength().HasMaxLength(1);
this.Property(t => t.AllowPostingYN).HasColumnName("AllowPostingYN").IsUnicode(false).IsFixedLength().HasMaxLength(1);
this.Property(t => t.NextJournal).HasColumnName("NextJournal").IsUnicode(false).HasMaxLength(20);
this.Property(t => t.CompanyKey).HasColumnName("Company");
this.Property(t => t.RetainedEarningsAcctKey).HasColumnName("RetainedEarningsAcct");
this.Property(t => t.PLSummaryAcctKey).HasColumnName("PLSummaryAcct");
this.Property(t => t.AccountingType).HasColumnName("AccountingType").IsUnicode(false).IsFixedLength().HasMaxLength(1);
this.Property(t => t.UserCreated).HasColumnName("UserCreated").IsRequired().IsUnicode(false).HasMaxLength(50);
this.Property(t => t.Created).HasColumnName("Created");
this.Property(t => t.UserEdited).HasColumnName("UserEdited").IsUnicode(false).HasMaxLength(50);
this.Property(t => t.Edited).HasColumnName("Edited");
this.Property(t => t.AlternateEntityKey).HasColumnName("AlternateEntity");
this.Property(t => t.TrackJobs).HasColumnName("TrackJobs").IsUnicode(false).IsFixedLength().HasMaxLength(1);
this.Property(t => t.TrackUnits).HasColumnName("TrackUnits").IsUnicode(false).IsFixedLength().HasMaxLength(1);
this.HasOptional(t => t.Company).WithMany(t => t.Entities).HasForeignKey(d => d.CompanyKey);
this.HasOptional(t => t.EntityAlternate).WithMany(t => t.EntitiesAlternate).HasForeignKey(d => d.AlternateEntityKey);
}
Why am I getting an error on non-existent properties?

Because of the abbreviation Acct instead of Account in these properties...
public Nullable<System.Guid> RetainedEarningsAcctKey { get; set; }
public Nullable<System.Guid> PLSummaryAcctKey { get; set; }
...EF conventions do not recognize the properties as foreign keys for
public virtual GLAccount RetainedEarningsAccount { get; set; }
public virtual GLAccount PLSummaryAccount { get; set; }
You can either rename the FK properties to RetainedEarningsAccountKey and PLSummaryAccountKey. EF should recognize them correctly then according to the rule "FK property name = Navigation property name + primary key name of target entity".
Or define the properties as FKs with Fluent API in your GLEntity_Mapping:
this.HasOptional(t => t.RetainedEarningsAccount)
.WithMany(a => a.SomeInverseCollection1)
.HasForeignKey(t => t.RetainedEarningsAcctKey);
this.HasOptional(t => t.PLSummaryAccount)
.WithMany(a => a.SomeInverseCollection2)
.HasForeignKey(t => t.PLSummaryAcctKey);
SomeInverseCollection1/2 are the related collections in GLAccount or use WithMany() without lambda parameter if there are no inverse collections in that class.

Related

Entity Framework 1 to 1 / 1 to *

I have this setup of classes:
public class User
{
public int Id { get; set; }
public string LoginName { get; set; }
public int CurrentPasswordId { get; set; }
public virtual ArchivedPassword CurrentPassword {get;set;}
public virtual ICollection<ArchivedPassword> UsedPasswords { get; set; }
}
public class ArchivedPassword
{
public int Id { get; set; }
public string Hash { get; set; }
public int UserId { get; set; }
public virtual User User { get; set; }
public virtual ICollection<User> Users { get; set; }
}
ModelBuilder.Entity<ArchivedPassword>()
.HasMany(e => e.Users)
.WithRequired(e => e.CurrentPassword)
.HasForeignKey(e => e.CurrentPasswordId)
.WillCascadeOnDelete(false);
ModelBuilder.Entity<User>()
.HasMany(e => e.UsedPasswords)
.WithRequired(e => e.User)
.HasForeignKey(e => e.UserId)
.WillCascadeOnDelete(false);
When i try to add a user and a password, ef canĀ“t figure out the update order.
Is this a mapping Problem?
I am not expert on Database .But i think you can not make two different Foreign Keys for same tables.But you can success what you want as below code
public class UserContext : DbContext
{
public DbSet<User> Users { get; set; }
public DbSet<Password> Paswords { get; set; }
}
public class User
{
public int Id { get; set; }
public string Name { get; set; }
[NotMapped]
public Password Password
{
get
{
return Passwords.ElementAt(Passwords.Count - 1);
}
}
public virtual ICollection<Password> Passwords { get; set; }
}
public class Password
{
public int Id { get; set; }
public string Hash { get; set; }
public virtual User User { get; set; }
}

Entity Framework many to many fluent API

I'm working with an existing database whose schema is fixed.
There are 3 many-to-many relations, Contact-Group, Contact-Department and Contact-Team.
There is a common table, ContactRelation that acts as the middle table for all 3 relations.
public class Contact
{
[Column("CtcID")]
public int Id { get; set; }
[Column("CtcFirstName")]
public string FirstName { get; set; }
[Column("CtcFamilyName")]
public string LastName { get; set; }
public virtual ICollection<ContactRelation> GroupRelations { get; set; }
public virtual ICollection<ContactRelation> DepartmentRelations { get; set; }
public virtual ICollection<ContactRelation> TeamRelations { get; set; }
}
public class Group
{
[Key, Column("GRID")]
public int Id { get; set; }
[Column("GRName")]
public string Name { get; set; }
public virtual ICollection<ContactRelation> ContactRelations { get; set; }
}
public class Department
{
[Key, Column("DEPID")]
public int Id { get; set; }
[Column("DEPName")]
public string Name { get; set; }
public virtual ICollection<ContactRelation> ContactRelations { get; set; }
}
public class Team
{
[Key, Column("TMID")]
public int Id { get; set; }
[Column("TransCode")]
public string TransCode { get; set; }
public virtual ICollection<ContactRelation> ContactRelations { get; set; }
}
public class ContactRelation
{
[Key]
[Column("CtcRelnID")]
public int Id { get; set; }
[Column("GRID")]
public int GroupId { get; set; }
[Column("DEPID")]
public int DepartmentId { get; set; }
[Column("TMID")]
public int TeamId { get; set; }
[Column("CUCtcID")]
public int ContactId { get; set; }
[Column("RCode")]
public string RoleCode { get; set; }
}
In my mapping, I have the following code:
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<Group>()
.HasMany(g => g.ContactRelations)
.WithRequired()
.HasForeignKey(r => r.GroupId);
modelBuilder.Entity<Department>()
.HasMany(c => c.ContactRelations)
.WithRequired()
.HasForeignKey(r => r.DepartmentId);
modelBuilder.Entity<Team>()
.HasMany(s => s.ContactRelations)
.WithRequired()
.HasForeignKey(r => r.TeamId);
modelBuilder.Entity<Contact>()
.HasMany(c => c.GroupRelations)
.WithRequired()
.HasForeignKey(r => r.ContactId);
modelBuilder.Entity<Contact>()
.HasMany(c => c.DepartmentRelations)
.WithRequired()
.HasForeignKey(r => r.ContactId);
modelBuilder.Entity<Contact>()
.HasMany(c => c.TeamRelations)
.WithRequired()
.HasForeignKey(r => r.ContactId);
}
I then try to execute the following query:
var ctc = repo.Contacts
.Include("GroupRelations")
.Include("DepartmentRelations")
.FirstOrDefault(c => c.FirstName.ToLower() == "jason");
and I keep getting error:
System.Data.SqlClient.SqlException:
Invalid column name 'Contact_Id1'.
Invalid column name 'Contact_Id'.
Invalid column name 'Contact_Id1'.
Invalid column name 'Contact_Id2'.
I read somewhere that a table cannot participate in more than one many-to-many relations. Is that true? Is it because the ContactRelation table is used more than once that I'm getting this error?
If so, what's the correct way to map these relations, without modifying the database schema.?
PS: I'm working with EF6.1
Thanks for your help.
Remove this mapping
modelBuilder.Entity<Contact>()
.HasMany(c => c.TeamRelations)
.WithRequired()
.HasForeignKey(r => r.ContactId);
and then try executing your query.

EF Code First Approach: Confused in EF Foreign Key constraint by fluent syntax

I am trying to create a foreign key relation ship with fluent syntax using EF code first approach.
My entities are as follows,
public partial class Defect
{
public int DefectID { get; set; }
public decimal ReleaseNo { get; set; }
public int BuildNo { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string StepsToReproduce { get; set; }
public int ApplicationModuleID { get; set; }
public int SeverityLevel { get; set; }
public string LoggedBy { get; set; }
public Nullable<System.DateTime> LoggedOn { get; set; }
public string LastModifiedBy { get; set; }
public Nullable<System.DateTime> LastModifiedOn { get; set; }
public string AssignedTo { get; set; }
public string Status { get; set; }
public string ResolutionNote { get; set; }
public Nullable<System.DateTime> ResolvedOn { get; set; }
public int ProjectID { get; set; }
public virtual SeverityIndex SeverityIndex { get; set; }
public virtual User LoggedByUser { get; set; }
public virtual User LastModifiedUser { get; set; }
public virtual User AssignedToUser { get; set; }
public virtual Project Project { get; set; }
}
public class DefectMap:EntityTypeConfiguration<Defect>
{
public DefectMap()
{
this.HasKey(d => d.DefectID);
this.ToTable("Defect");
this.Property(d => d.DefectID)
.IsRequired()
.HasColumnName("DefectID")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
this.Property(d => d.Description)
.IsRequired()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(2000)
.HasColumnName("Description")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(d => d.StepsToReproduce)
.IsOptional()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(4000)
.HasColumnName("StepsToReproduce")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(d => d.LastModifiedBy)
.IsOptional()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(10)
.HasColumnName("LastModifiedBy")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(d => d.AssignedTo)
.IsOptional()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(10)
.HasColumnName("AssignedTo")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(d => d.Status)
.IsOptional()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(50)
.HasColumnName("Status")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.Property(d => d.ResolutionNote)
.IsOptional()
.IsUnicode()
.IsVariableLength()
.HasMaxLength(4000)
.HasColumnName("ResolutionNote")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.HasRequired(p => p.Project).WithMany(p => p.DefectList).HasForeignKey(p => p.ProjectID);
this.HasRequired(s => s.SeverityIndex).WithMany(s => s.DefectList).HasForeignKey(s => s.SeverityLevel).WillCascadeOnDelete();
this.HasOptional(u => u.AssignedToUser).WithMany(u => u.AssignedToUserList).HasForeignKey(u => u.AssignedTo).WillCascadeOnDelete();
this.HasOptional(u => u.LastModifiedUser).WithMany(u => u.ModifiedByUserList).HasForeignKey(u => u.LastModifiedBy);
this.HasRequired(u => u.LoggedByUser).WithMany(u => u.LoggedByUserList).HasForeignKey(u => u.LoggedBy);
}
public partial class Project
{
public Project()
{
ApplicationModuleList = new List<ApplicationModule>();
DefectList = new List<Defect>();
UserList = new List<User>();
}
public int ID { get; set; }
public string ProjectName { get; set; }
public string ProjectManager { get; set; }
public Nullable<System.DateTime> ProjectStartDate { get; set; }
public Nullable<System.DateTime> ProjectEstimatedEndDate { get; set; }
public Nullable<System.DateTime> ProjectActualEndDate { get; set; }
public Nullable<int> ProjectBillingModel { get; set; }
public Nullable<decimal> ProjectEstimatedBudget { get; set; }
public Nullable<decimal> ProjectActualBudget { get; set; }
public Nullable<int> ProjectPortfolio { get; set; }
public Nullable<decimal> ProjectBillingRate { get; set; }
public Nullable<int> ProjectEstimatedManHours { get; set; }
public Nullable<int> ProjectActualManHours { get; set; }
public Nullable<int> ProjectIsApproved { get; set; }
public virtual ICollection<ApplicationModule> ApplicationModuleList { get; set; }
public virtual ICollection<Defect> DefectList { get; set; }
public virtual ICollection<User> UserList { get; set; }
public virtual BillingModel BillingModel { get; set; }
public virtual Portfolio Portfolio { get; set; }
public virtual ApprovalStatus ApprovalStatus { get; set; }
}
public class ProjectMap:EntityTypeConfiguration<Project>
{
public ProjectMap()
{
this.HasKey(p => p.ID);
this.ToTable("Projects");
this.Property(p => p.ID)
.HasColumnName("ID")
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity)
.IsRequired();
this.Property(p => p.ProjectName)
.HasColumnName("ProjectName")
.HasMaxLength(200)
.IsRequired()
.IsVariableLength()
.IsUnicode()
.HasDatabaseGeneratedOption(DatabaseGeneratedOption.None);
this.HasOptional(p => p.BillingModel).WithMany(p=>p.Projects).HasForeignKey(p => p.ProjectBillingModel).WillCascadeOnDelete();
this.HasOptional(p => p.Portfolio).WithMany(p=>p.Projects).HasForeignKey(p => p.ProjectPortfolio).WillCascadeOnDelete();
this.HasOptional(p => p.ApprovalStatus).WithMany(p=>p.Projects).HasForeignKey(p => p.ProjectIsApproved).WillCascadeOnDelete();
}
}
I am trying code first approach for database creation using fluent API.
However when I run the code I get error saying
*Introducing FOREIGN KEY constraint 'FK_dbo.User_dbo.Projects_ProjectID' on table 'User' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints. Could not create constraint. See previous errors*
The same error appears for AssignedTo column.
Here I am trying to implement logic where, A project can have many defects and a defect should have an associated Project ID (i.e one to many relationship between project and defect).
Can anyone suggest what is wrong with the code and where should I rectify the code to get things working?
Thanks in advance!!!
EF has Cascade delete on by default, and this will cause problems with your design - as per the error message.
Either add the following
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<OneToManyCascadeDeleteConvention>();
}
or include
WillCascadeOnDelete(false)
in your fluent API

Integrating SimpleMembership with Entity Framework

I'm trying to integrate the SimpleMembership tables with the rest of my Object Model - to manage all the entities from a single database and context.
Up to now the best recipe I've found for manually spinning up the SM tables (the entry point to combine SimpleMember with the rest of my Object Model) is found here. But, as cited in the comments section there are a couple mistakes in the code sample provided. The comments attempt to provide corrections but, due to formatted, really hard to follow.
I'm 80% the way there but getting stuck with the Foreign Key generation for the Membership table. Does the code within OnModelCreating block belong in the MyDbContext class? I'm getting a compile error on the .WithMany(u => u.Members) line.
Membership.cs
[Table("webpages_Membership")]
public class Membership
{
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int UserId { get; set; }
public DateTime? CreateDate { get; set; }
[StringLength(128)]
public string ConfirmationToken { get; set; }
public bool? IsConfirmed { get; set; }
public DateTime? LastPasswordFailureDate { get; set; }
public int PasswordFailuresSinceLastSuccess { get; set; }
[Required, StringLength(128)]
public string Password { get; set; }
public DateTime? PasswordChangedDate { get; set; }
[Required, StringLength(128)]
public string PasswordSalt { get; set; }
[StringLength(128)]
public string PasswordVerificationToken { get; set; }
public DateTime? PasswordVerificationTokenExpirationDate { get; set; }
<strike>public virtual ICollection<Role> Roles { get; set; }</strike>
EDIT: Originally I added the line above to remove a compiler complaint in the extraneous code block below. Removing this attempt to create a FK to Roles will align the rest of this code so that these model classes create a Migration that generates tables for SM.
OAuthMembership.cs
[Table("webpages_OAuthMembership")]
public class OAuthMembership
{
[Key, Column(Order = 0), StringLength(30)]
public string Provider { get; set; }
[Key, Column(Order = 1), StringLength(100)]
public string ProviderUserId { get; set; }
public int UserId { get; set; }
}
Role.cs
[Table("webpages_Roles")]
public class Role
{
[Key]
public int RoleId { get; set; }
[StringLength(256)]
public string RoleName { get; set; }
public virtual ICollection<UserProfile> UserProfiles { get; set; }
}
UserProfile.cs
[Table("UserProfile")]
public class UserProfile
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public virtual ICollection<Role> Roles { get; set; }
}
MyDbContext.cs
public MyDbContext()
: base("DefaultConnection")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
public DbSet<Membership> Membership { get; set; }
public DbSet<Role> Roles { get; set; }
public DbSet<OAuthMembership> OAuthMembership { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<UserProfile>()
.HasMany<Role>(r => r.Roles)
.WithMany(u => u.UserProfiles)
.Map(m =>
{
m.ToTable("webpages_UsersInRoles");
m.MapLeftKey("UserId");
m.MapRightKey("RoleId");
});
EDIT: The block below was included in one of the article's comments but seems not to be needed.
//modelBuilder.Entity<Membership>()
// .HasMany<Role>(r => r.Roles)
// .WithMany(u => u.Members)
// .Map(m =>
// {
// m.ToTable("webpages_UsersInRoles");
// m.MapLeftKey("UserId");
// m.MapRightKey("RoleId");
// });
}
}
I followed the instructions in the article, and I also took into account the the comments that suggested the article was wrong in a few ways.
I ended up with the following classes:
UserProfile.cs
[Table("UserProfile")]
public class UserProfile
{
[Key, ForeignKey("Membership")]
[DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
public int UserId { get; set; }
public string UserName { get; set; }
public ICollection<WebSecurity.Role> Roles { get; set; }
public WebSecurity.Membership Membership { get; set; }
}
You should notice right away the "ForeignKey" attribute I use on the UserId column. Since the user is first created in the Membership table, my UserProfile table is the dependent table.
Membership.cs
[Table("webpages_Membership")]
public class Membership
{
//public Membership()
//{
// Roles = new List<Role>();
// OAuthMemberships = new List<OAuthMembership>();
//}
[Key, DatabaseGenerated(DatabaseGeneratedOption.None)]
public int UserId { get; set; }
public DateTime? CreateDate { get; set; }
[StringLength(128)]
public string ConfirmationToken { get; set; }
public bool? IsConfirmed { get; set; }
public DateTime? LastPasswordFailureDate { get; set; }
public int PasswordFailuresSinceLastSuccess { get; set; }
[Required, StringLength(128)]
public string Password { get; set; }
public DateTime? PasswordChangedDate { get; set; }
[Required, StringLength(128)]
public string PasswordSalt { get; set; }
[StringLength(128)]
public string PasswordVerificationToken { get; set; }
public DateTime? PasswordVerificationTokenExpirationDate { get; set; }
public UserProfile UserProfile { get; set; }
}
Per Richard's comments in the article, I commented out the constructor. I also created a reference back to the UserProfile, but not to roles.
OAuthMembership.cs
[Table("webpages_OAuthMembership")]
public class OAuthMembership
{
[Key, Column(Order = 0), StringLength(30)]
public string Provider { get; set; }
[Key, Column(Order = 1), StringLength(100)]
public string ProviderUserId { get; set; }
public int UserId { get; set; }
//[Column("UserId"), InverseProperty("OAuthMemberships")]
//public Membership User { get; set; }
}
My OAuthMembership class remained basically the same; I commented out only the User attribute, per Richard's comment in the article.
AccountModel.cs+UsersContext
Finally, the UserContext class, where I create the association for the UsersInRoles table.
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnection")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<InternetApplication.Models.WebSecurity.Role>()
.HasMany<InternetApplication.Models.UserProfile>(r => r.UserProfiles)
.WithMany(u => u.Roles)
.Map(m =>
{
m.ToTable("webpages_UsersInRoles");
m.MapLeftKey("UserId");
m.MapRightKey("RoleId");
});
}
public DbSet<WebSecurity.Membership> Membership { get; set; }
public DbSet<WebSecurity.OAuthMembership> OAuthMembership { get; set; }
public DbSet<WebSecurity.Role> Roles { get; set; }
public DbSet<UserProfile> UserProfiles { get; set; }
}
In addition to adding the UsersInRoles mapping, I added DbSet entries for each table.
Now that everything has been created, I can use my Add-Migration and Update-Database commands and use the following code snippet that combines the Membership, UserProfile, and Roles tables:
using (var db = new UsersContext())
{
var memberships = db.Membership
.Include("UserProfile")
.Include("UserProfile.Roles")
.ToList();
foreach (var member in memberships)
{
member.IsConfirmed = true;
}
db.SaveChanges();
}
This was a long post, but I hope that helps.
I used the answer to this question to automatically generate the models from the existing 'webpage_' tables in my database. This ensures that the models are created in the exact same way that SimpleMembership creates them. This resulted in the following code:
Models:
public partial class webpages_Membership
{
public int UserId { get; set; }
public Nullable<System.DateTime> CreateDate { get; set; }
public string ConfirmationToken { get; set; }
public Nullable<bool> IsConfirmed { get; set; }
public Nullable<System.DateTime> LastPasswordFailureDate { get; set; }
public int PasswordFailuresSinceLastSuccess { get; set; }
public string Password { get; set; }
public Nullable<System.DateTime> PasswordChangedDate { get; set; }
public string PasswordSalt { get; set; }
public string PasswordVerificationToken { get; set; }
public Nullable<System.DateTime> PasswordVerificationTokenExpirationDate { get; set; }
}
public partial class webpages_Roles
{
public webpages_Roles()
{
this.webpages_UsersInRoles = new HashSet<webpages_UsersInRoles>();
}
public int RoleId { get; set; }
public string RoleName { get; set; }
public virtual ICollection<webpages_UsersInRoles> webpages_UsersInRoles { get; set; }
}
public partial class webpages_UsersInRoles
{
public int UserId { get; set; }
public int RoleId { get; set; }
public virtual webpages_Roles webpages_Roles { get; set; }
}
Fluent Mappings:
internal partial class MembershipMapping : EntityTypeConfiguration<webpages_Membership>
{
public MembershipMapping()
{
this.HasKey(t => t.UserId);
this.ToTable("webpages_Membership");
this.Property(t => t.UserId).HasColumnName("UserId").HasDatabaseGeneratedOption(new Nullable<DatabaseGeneratedOption>(DatabaseGeneratedOption.None));
this.Property(t => t.CreateDate).HasColumnName("CreateDate");
this.Property(t => t.ConfirmationToken).HasColumnName("ConfirmationToken").HasMaxLength(128);
this.Property(t => t.IsConfirmed).HasColumnName("IsConfirmed");
this.Property(t => t.LastPasswordFailureDate).HasColumnName("LastPasswordFailureDate");
this.Property(t => t.PasswordFailuresSinceLastSuccess).HasColumnName("PasswordFailuresSinceLastSuccess");
this.Property(t => t.Password).HasColumnName("Password").IsRequired().HasMaxLength(128);
this.Property(t => t.PasswordChangedDate).HasColumnName("PasswordChangedDate");
this.Property(t => t.PasswordSalt).HasColumnName("PasswordSalt").IsRequired().HasMaxLength(128);
this.Property(t => t.PasswordVerificationToken).HasColumnName("PasswordVerificationToken").HasMaxLength(128);
this.Property(t => t.PasswordVerificationTokenExpirationDate).HasColumnName("PasswordVerificationTokenExpirationDate");
}
}
internal partial class RolesMapping : EntityTypeConfiguration<webpages_Roles>
{
public RolesMapping()
{
this.HasKey(t => t.RoleId);
this.ToTable("webpages_Roles");
this.Property(t => t.RoleId).HasColumnName("RoleId");
this.Property(t => t.RoleName).HasColumnName("RoleName").IsRequired().HasMaxLength(256);
}
}
internal partial class UsersInRolesMapping : EntityTypeConfiguration<webpages_UsersInRoles>
{
public UsersInRolesMapping()
{
this.HasKey(t => new { t.UserId, t.RoleId });
this.ToTable("webpages_UsersInRoles");
this.Property(t => t.UserId).HasColumnName("UserId").HasDatabaseGeneratedOption(new Nullable<DatabaseGeneratedOption>(DatabaseGeneratedOption.None));
this.Property(t => t.RoleId).HasColumnName("RoleId").HasDatabaseGeneratedOption(new Nullable<DatabaseGeneratedOption>(DatabaseGeneratedOption.None));
this.HasRequired(t => t.webpages_Roles).WithMany(t => t.webpages_UsersInRoles).HasForeignKey(d => d.RoleId);
}
}
Database Context:
public class MembershipContext : DbContext, IDisposable
{
public DbSet<webpages_Membership> Membership { get; set; }
public DbSet<webpages_Roles> Roles { get; set; }
public DbSet<webpages_UsersInRoles> UsersInRoles { get; set; }
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Configurations.Add(new MembershipMapping());
modelBuilder.Configurations.Add(new RolesMapping());
modelBuilder.Configurations.Add(new UsersInRolesMapping());
base.OnModelCreating(modelBuilder);
}
}
Note that I have excluded the OAuthMembership table, because I didn't need it for my solution. But if you follow the steps in the link I provided above you can easily include that table as well.
Starting from a blank MVC4 Internet Template I ran the project so as to create the SimpleMembership tables in a fresh db - then used EF's Reverse Engineer tool to create POCOs from those tables. Stepped thru it line by line to find the error and edited the code block in the OP.
With that code in place I used Package Manager to 'Add-Migration' and 'Update-Database'. Initial tests confirm everything works - I suppose I'll have to revisit if I find edge-cases that expose any deeper problems.

Mapping Many-to-Many Relationship With Defined Middle Entity in Entity Framework 5

I'm trying to define a many-to-many relationship explicitly. By explicitly, I mean that I'm defining the middle entity and configuring it using the Fluent API. Below is my code:
public class ContentType
{
public Int64 Id { get; set; }
public Guid SID { get; set; }
public String Name { get; set; }
public ContentType Parent { get; set; }
public Nullable<Int64> ParentId { get; set; }
public Category Category { get; set; }
public Nullable<Int64> CategoryId { get; set; }
public Boolean IsLocked { get; set; }
public virtual ICollection<ContentTypeColumn> ContentTypeColumns { get; set; }
}
public class Column
{
public Int64 Id { get; set; }
public Guid SID { get; set; }
public String SchemaName { get; set; }
public DataType DataType { get; set; }
public Int32 DataTypeId { get; set; }
public virtual ICollection<ContentTypeColumn> ContentTypeColumns { get; set; }
}
public class ContentTypeColumn
{
public Int64 Id { get; set; }
public Int64 ColumnId { get; set; }
public Column Column { get; set; }
public ContentType ContentType { get; set; }
public Int64 ContentTypeId { get; set; }
public Boolean IsRequired { get; set; }
public Boolean IsCalculated { get; set; }
public String Title { get; set; }
public Boolean IsSystem { get; set; }
public Expression Expression { get; set; }
public Int32 ExpressionId { get; set; }
public virtual ICollection<ColumnRule> Rules { get; set; }
}
public class ContentTypeConfiguration : EntityTypeConfiguration<ContentType>
{
public ContentTypeConfiguration()
{
this.ToTable("ContentType");
this.Property(x => x.Id).HasColumnName("ContentTypeId").IsRequired();
this.Property(x => x.Name).HasMaxLength(30);
this.HasOptional(x => x.Parent)
.WithMany()
.HasForeignKey(x => x.ParentId);
this.Property(x => x.SID).IsRequired();
}
}
public class ContentTypeColumnConfiguration : EntityTypeConfiguration<ContentTypeColumn>
{
public ContentTypeColumnConfiguration()
{
this.ToTable("ContentTypeColumn");
this.HasRequired(x => x.ContentType)
.WithMany()
.HasForeignKey(x => x.ContentTypeId);
this.Property(x => x.Title).HasMaxLength(50).IsRequired();
this.Property(x => x.Id).HasColumnName("ContentTypeColumnId");
}
}
For some reason, two foreign keys are being created on the resultant ContentTypeColumn table. One is a nullable foreign key, the other is non-nullable. I want only the latter to be generated, and I have no idea where the nullable key is coming from.
Any thoughts?
This is wrong:
this.HasRequired(x => x.ContentType)
.WithMany()
.HasForeignKey(x => x.ContentTypeId);
You have reverse navigation property so you must use int in WithMany or EF will probably create two relations:
this.HasRequired(x => x.ContentType)
.WithMany(y => y.ContentTypeColumns)
.HasForeignKey(x => x.ContentTypeId);
Btw. this mapping should not be needed at all because it is discovered automatically through default conventions.