我正在尝试获取代码优先创建数据库的项目;我已经解决了所有架构错误,但现在我收到了以下错误。
未加载“EquipmentPro.Entities.Correspondence_UserFrom”关系,因为“EquipmentPro.Entities.AspNetUsers”类型不可用。
我在这里阅读了一些帖子,大多数人都说这个问题是因为'AspNetUsers'类与关系所在的类不在同一个命名空间中。起初这是事实,所以我将AspNetUsers移动到与其他模型对象相同的文件夹中并修复了命名空间但我仍然收到错误。我正在使用流畅的API来创建表和关系。
这是通信模型:
public class Correspondence : EntityBase
{
public Correspondence()
{
ChildCorrespondences = new HashSet<Correspondence>();
}
public int CorrespondenceId { get; set; }
public int CorrespondenceTypeId { get; set; }
public int? ParentCorrespondenceId { get; set; }
public string UserIdFrom { get; set; }
public string UserIdTo { get; set; }
public int LanguageId { get; set; }
public int? EquipmentItemId { get; set; }
public int? CartId { get; set; }
public string MessageText { get; set; }
public bool IsFlaggedForReview { get; set; }
public Correspondence ParentCorrespondence { get; set; }
public CorrespondenceType CorrespondenceType { get; set; }
public AspNetUsers UserFrom { get; set; }
public AspNetUsers UserTo { get; set; }
public Language Language { get; set; }
public EquipmentItem EquipmentItem { get; set; }
public Cart Cart { get; set; }
public ICollection<Correspondence> ChildCorrespondences { get; set; }
}
AspNetUsers模型是标准的.Net成员资格用户模型,这里是流畅的API代码:
public CorrespondenceMapping()
{
HasKey(t => t.CorrespondenceId);
ToTable("Correspondence");
Property(t => t.CorrespondenceId).HasDatabaseGeneratedOption(DatabaseGeneratedOption.Identity);
Property(t => t.CorrespondenceTypeId).IsRequired();
Property(t => t.ParentCorrespondenceId);
Property(t => t.UserIdFrom).HasMaxLength(128).IsRequired().HasColumnType("nvarchar");
Property(t => t.UserIdTo).HasMaxLength(128).HasColumnType("nvarchar");
Property(t => t.LanguageId).IsRequired();
Property(t => t.EquipmentItemId);
Property(t => t.CartId);
Property(t => t.MessageText).IsRequired().HasColumnType("nvarchar");
Property(t => t.IsFlaggedForReview).IsRequired();
HasOptional(t => t.ParentCorrespondence)
.WithMany(t => t.ChildCorrespondences)
.HasForeignKey(t => t.ParentCorrespondenceId)
.WillCascadeOnDelete();
HasRequired(t => t.CorrespondenceType)
.WithMany(t => t.Correspondences)
.HasForeignKey(t => t.CorrespondenceTypeId)
.WillCascadeOnDelete();
// the error is with this relaionship
HasRequired(t => t.UserFrom).WithMany(t => t.Correspondences).HasForeignKey(t => t.UserIdFrom);
HasOptional(t => t.UserTo).WithMany(t => t.Correspondences).HasForeignKey(t => t.UserIdTo);
HasRequired(t => t.Language).WithMany(t => t.Correspondences).HasForeignKey(t => t.LanguageId);
HasOptional(t => t.EquipmentItem).WithMany(t => t.Correspondences).HasForeignKey(t => t.EquipmentItemId);
HasOptional(t => t.Cart).WithMany(t => t.Correspondences).HasForeignKey(t => t.CartId);
}
这是DbContext类:
public class EquipmentDataDbContext : DbContext
{
public EquipmentDataDbContext()
: base("name=EquipmentData")
{
}
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Conventions.Remove<PluralizingTableNameConvention>();
// adding the equipment data model mappings
modelBuilder.Configurations.Add(new CartMapping());
modelBuilder.Configurations.Add(new CartItemMapping());
modelBuilder.Configurations.Add(new CartStatusTypeMapping());
modelBuilder.Configurations.Add(new CorrespondenceMapping());
modelBuilder.Configurations.Add(new CorrespondenceTypeMapping());
modelBuilder.Configurations.Add(new CurrencyTypeMapping());
modelBuilder.Configurations.Add(new EquipmentAuctionMapping());
modelBuilder.Configurations.Add(new EquipmentAuctionBidMapping());
modelBuilder.Configurations.Add(new EquipmentBidMapping());
modelBuilder.Configurations.Add(new EquipmentInventoryMapping());
modelBuilder.Configurations.Add(new EquipmentItemMapping());
modelBuilder.Configurations.Add(new EquipmentItemPropertyDefaultMapping());
modelBuilder.Configurations.Add(new EquipmentItemPropertyValueMapping());
modelBuilder.Configurations.Add(new EquipmentItemTypeMapping());
modelBuilder.Configurations.Add(new EquipmentPropertyMapping());
modelBuilder.Configurations.Add(new EquipmentPropertyDisplayTypeMapping());
modelBuilder.Configurations.Add(new EquipmentPropertyOptionMapping());
modelBuilder.Configurations.Add(new EquipmentPropertyValueTypeMapping());
modelBuilder.Configurations.Add(new MediaMapping());
modelBuilder.Configurations.Add(new MediaTypeMapping());
modelBuilder.Configurations.Add(new TagMapping());
// adding the membership data model mappings
modelBuilder.Configurations.Add(new AspNetRolesMapping());
modelBuilder.Configurations.Add(new AspNetUserClaimsMapping());
modelBuilder.Configurations.Add(new AspNetUserLoginsMapping());
modelBuilder.Configurations.Add(new AspNetUsersMapping());
}
// Equipment Data Models
public DbSet<Cart> Carts { get; set; }
public DbSet<CartItem> CartItems { get; set; }
public DbSet<CartStatusType> CartStatuses { get; set; }
public DbSet<Correspondence> Correspondences { get; set; }
public DbSet<CorrespondenceType> CorrespondenceTypes { get; set; }
public DbSet<CurrencyType> CurrencyTypes { get; set; }
public DbSet<EquipmentAuction> EquipmentAuctions { get; set; }
public DbSet<EquipmentAuctionBid> EquipmentAuctionBids { get; set; }
public DbSet<EquipmentBid> EquipmentBids { get; set; }
public DbSet<EquipmentInventory> EquipmentInventory { get; set; }
public DbSet<EquipmentItem> EquipmentItems { get; set; }
public DbSet<EquipmentItemPropertyDefault> EquipmentItemPropertyDefault { get; set; }
public DbSet<EquipmentItemPropertyValue> EquipmentItemPropertyValues { get; set; }
public DbSet<EquipmentItemType> EquipmentItemTypes { get; set; }
public DbSet<EquipmentProperty> EquipmentProperties { get; set; }
public DbSet<EquipmentPropertyDisplayType> EquipmentPropertyDisplayTypes { get; set; }
public DbSet<EquipmentPropertyOption> EquipmentPropertyOptions { get; set; }
public DbSet<EquipmentPropertyValueType> EquipmentPropertyValueTypes { get; set; }
public DbSet<Media> Media { get; set; }
public DbSet<MediaType> MediaTypes { get; set; }
public DbSet<Tag> Tags { get; set; }
// Membership Models
public DbSet<AspNetRoles> AspNetRoles { get; set; }
public DbSet<AspNetUserClaims> AspNetUserClaims { get; set; }
public DbSet<AspNetUserLogins> AspNetUserLogins { get; set; }
public DbSet<AspNetUsers> AspNetUsers { get; set; }
}
我将我的解决方案拆分为3个不同的项目,一个Web项目,一个只包含实体模型的项目以及一个包含映射和数据库上下文的项目。当我收到此错误时,我正在运行Web项目的初始创建。
有没有人知道为什么我会收到这个错误?
更新
我决定删除抛出错误的关系,看起来一切都成功但数据库中没有创建表。消息中列出的目标数据库是正确的,但没有创建表。