I have added an Entity properly to EF as below
public class VehicleInfo
{
[Key]
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int VehicleInfoId { get; set; }
public int? VehicleId { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(100)]
public string VehicleOwner { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(100)]
public string VehicleOperator { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(30)]
public string Entity { get; set; }
public int EntityId { get; set; }
public string InputFieldEntity { get; set; }
public int InputFieldEntityId { get; set; }
public string InputFieldGroup { get; set; }
public DateTime PurchasedDate { get; set; }
public string EngineFamilyName { get; set; }
public bool RegHoldSet { get; set; }
public DateTime RegHoldSetDate { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(80)]
public string RegHoldSetBy { get; set; }
public DateTime RegHoldClearDate { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(80)]
public string RegHoldClearBy { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(8000)]
public string RegHoldComment { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(30)]
public string VIN { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(20)]
public string LicensePlate { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(20)]
public string TrailerPlate { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(4)]
public string ModelYear { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(40)]
public string Manufacturer { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(4)]
public string ChassisYear { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(200)]
public string Address_Street1 { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(200)]
public string Address_Street2 { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(50)]
public string Address_City { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(2)]
public string Address_StateCode { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(15)]
public string Address_Zip { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(50)]
public string Address_Country { get; set; }
public DateTime UpdatedDate { get; set; }
[Column(TypeName = "nvarchar")]
[StringLength(80)]
public DateTime UpdatedBy { get; set; }
}
Then I tried to generate migration for it, Migration came with just Up and Down methods empty (this is one problems I sometimes face in EF not knowing what's the mistake
Then I wrote my own code to generate context for this entity in the psuedo code as below
public override void Up()
{
CreateTable(
"dbo.VehicleInfo",
c => new
{
VehicleInfoId = c.Int(nullable: false, identity: true),
VehicleId = c.Int(nullable: true),
Entity = c.String(nullable: true, maxLength: 30),
EntityId = c.Int(nullable: true),
InputFieldEntity = c.String(nullable: true, maxLength: 30),
InputFieldEntityId = c.Int(nullable: true),
InputFieldGroup = c.String(nullable: true, maxLength: 50),
VehicleOwner = c.String(nullable: true, maxLength: 100),
VehicleOperator = c.String(nullable: true, maxLength: 100),
RegHoldSet = c.Boolean(nullable: true),
RegHoldSetDate = c.DateTime(nullable: true),
RegHoldSetBy = c.String(nullable: true, maxLength: 80),
RegHoldClearDate = c.DateTime(nullable: true),
RegHoldClearBy = c.String(nullable: true, maxLength: 80),
RegHoldComment = c.String(nullable: true, maxLength: 8000),
VIN = c.String(nullable: true, maxLength: 30),
LicensePlate = c.String(nullable: true, maxLength: 20),
TrailerPlate = c.String(nullable: true, maxLength: 20),
ModelYear = c.String(nullable: true, maxLength: 4),
Manufacturer = c.String(nullable: true, maxLength: 40),
EngineFamilyName = c.String(nullable: true, maxLength: 50),
ChassisYear = c.String(nullable: true, maxLength: 4),
Address_Street1 = c.String(nullable: true, maxLength: 200),
Address_Street2 = c.String(nullable: true, maxLength: 200),
Address_City = c.String(nullable: true, maxLength: 50),
Address_StateCode = c.String(nullable: true, maxLength: 2),
Address_Zip = c.String(nullable: true, maxLength: 15),
Address_Country = c.String(nullable: true, maxLength: 50),
UpdatedDate = c.DateTime(nullable: true, defaultValueSql: "GETDATE()"),
UpdatedBy = c.String(nullable: true, maxLength: 80)
})
.PrimaryKey(t => t.VehicleInfoId);
}
When I am trying to add any rows to the Database table, it throws eror as "The entity type VehicleInfo is not part of the model for the current context." any help please.
As experts asked for dbContext I am posting here even though its huge one, thank you for all you support
public class IMSContext : IdentityDbContext<ApplicationUser>
{
#region Initializer
public IMSContext() : base(Constants.DefaultConnection, throwIfV1Schema: false)
{
}
public IMSContext(string connectionString) : base(connectionString, throwIfV1Schema: false)
{
}
#endregion
public virtual new DbSet<ApplicationRole> Roles { get; set; }
public virtual DbSet<SysLog> SysLogs { get; set; }
public virtual DbSet<Group> Groups { get; set; }
public virtual DbSet<Contact> Contacts { get; set; }
public virtual DbSet<ContactBinding> ContactBindings { get; set; }
public virtual DbSet<GridContact> GridContacts { get; set; }
public virtual DbSet<ContactType> ContactTypes { get; set; }
public virtual DbSet<JsonObject> JsonObjects { get; set; }
public virtual DbSet<Case> Cases { get; set; }
public virtual DbSet<Branch> Branch { get; set; }
public virtual DbSet<CaseStatus> CaseStatuses { get; set; }
public virtual DbSet<EnforcementSection> EnforcementSections { get; set; }
public virtual DbSet<EventLog> EventLogs { get; set; }
public virtual DbSet<EventLogAction> EventLogActions { get; set; }
public virtual DbSet<InspectionItem> InspectionItems { get; set; }
public virtual DbSet<InspectionItemSource> InspectionItemSources { get; set; }
public virtual DbSet<InspectionRequest> InspectionRequests { get; set; }
public virtual DbSet<InspectionRequestStatus> InspectionRequestStatuses { get; set; }
public virtual DbSet<InspectionResult> InspectionResults { get; set; }
public virtual DbSet<InspectionResultStatCount> InspectionResultStatCounts { get; set; }
public virtual DbSet<InspectionResultStatus> InspectionResultStatuses { get; set; }
public virtual DbSet<NOV> NOVs { get; set; }
public virtual DbSet<NOVNoticeType> NOVNoticeTypes { get; set; }
public virtual DbSet<ProofItem> ProofItems { get; set; }
public virtual DbSet<ProofReceivedType> ProofReceivedTypes { get; set; }
public virtual DbSet<Reminder> Reminders { get; set; }
public virtual DbSet<StickCount> StickCounts { get; set; }
public virtual DbSet<UploadedDocument> UploadedDocuments { get; set; }
public virtual DbSet<UploadedDocumentType> UploadedDocumentTypes { get; set; }
public virtual DbSet<UploadedPhotograph> UploadedPhotographs { get; set; }
public virtual DbSet<UploadedPhotographCategory> UploadedPhotographCategories { get; set; }
public virtual DbSet<USStates> USStatess { get; set; }
public virtual DbSet<VehicleType> VehicleTypes { get; set; }
public virtual DbSet<Violation> Violations { get; set; }
public virtual DbSet<ViolationType> ViolationTypes { get; set; }
public virtual DbSet<Sep> Seps { get; set; }
public virtual DbSet<PaymentMethod> PaymentMethods { get; set; }
public virtual DbSet<Settlement> Settlements { get; set; }
public virtual DbSet<PaymentPlan> PaymentPlans { get; set; }
public virtual DbSet<PaymentPlanItem> PaymentPlanItems { get; set; }
public virtual DbSet<Payment> Payments { get; set; }
public virtual DbSet<ItemProperty> ItemProperties { get; set; }
public virtual DbSet<UploadRelation> UploadRelations { get; set; }
public virtual DbSet<MediaFolder> MediaFolder { get; set; }
public virtual DbSet<InputField> InputFields { get; set; }
public virtual DbSet<InputDropdown> InputDropdowns { get; set; }
public virtual DbSet<InputValue> InputValues { get; set; }
public virtual DbSet<Vehicle> VehicleVehicles { get; set; }
public virtual DbSet<Note> Notes { get; set; }
public virtual DbSet<OneToMany> NOVRelation { get; set; }
public virtual DbSet<Invoice> Invoices { get; set; }
public virtual DbSet<Program> Programs { get; set; }
public virtual DbSet<Transaction> Transactions { get; set; }
public virtual DbSet<Lookup> Lookups { get; set; }
public virtual DbSet<ViolationNOV> ViolationNOVs { get; set; }
public virtual DbSet<CaseNOV> CaseNOVs { get; set; }
public virtual DbSet<ViolationTypeNOV> ViolationTypeNOVs { get; set; }
public virtual DbSet<RequestResultCaseComplaint> RequestResultCaseComplaints { get; set; }
public virtual DbSet<ViolationPenaltyAdjustment> ViolationPenaltyAdjustments { get; set; }
public virtual DbSet<LocationType> LocationTypes { get; set; }
public virtual DbSet<Location> Locations { get; set; }
public virtual DbSet<InspectionItemCategory> InspectionItemCategories { get; set; }
public virtual DbSet<InspectionItemCategoryViolation> InspectionItemCategoryViolations { get; set; }
#region Relations
protected override void OnModelCreating(DbModelBuilder modelBuilder)
{
modelBuilder.Entity<IdentityUserRole>().HasKey((IdentityUserRole r) => new { UserId = r.UserId, RoleId = r.RoleId }).ToTable("AspNetUserRoles");
modelBuilder.Entity<IdentityUserLogin>().HasKey((IdentityUserLogin l) => new { UserId = l.UserId, LoginProvider = l.LoginProvider, ProviderKey = l.ProviderKey }).ToTable("AspNetUserLogins");
modelBuilder.Entity<Group>().HasMany(c => c.Roles).WithMany().Map(
m =>
{
m.MapLeftKey("GroupId");
m.MapRightKey("RoleId");
m.ToTable("GroupRole");
});
modelBuilder.Entity<ApplicationUser>().HasMany(c => c.Groups).WithMany().Map(m =>
{
m.MapLeftKey("UserId");
m.MapRightKey("GroupId");
m.ToTable("UserGroup");
});
//-- Inspection Request can be assigned to multiple users
modelBuilder.Entity<Case>().HasMany(i => i.AssignedToInvestigators).WithMany().Map(m =>
{
m.MapLeftKey("CaseId");
m.MapRightKey("UserId");
m.ToTable("CaseAssignedToInvestigators");
});
modelBuilder.Entity<Case>().HasMany(i => i.AssignedToInspectors).WithMany().Map(m =>
{
m.MapLeftKey("CaseId");
m.MapRightKey("UserId");
m.ToTable("InvestigationAssignedToInspectors");
});
//-- Inspection Request can be assigned to multiple users
modelBuilder.Entity<InspectionRequest>().HasMany(i => i.AssignedTo).WithMany().Map(m =>
{
m.MapLeftKey("InspectionRequestId");
m.MapRightKey("UserId");
m.ToTable("InspectionRequestAssignedTo");
});
//-- Inspection Result can be Completed by multiple users
modelBuilder.Entity<InspectionResult>().HasMany(i => i.CompletedBy).WithMany().Map(m =>
{
m.MapLeftKey("InspectionResultId");
m.MapRightKey("UserId");
m.ToTable("InspectionResultCompletedBy");
});
//// Inspection Items can have multiple Categories
//modelBuilder.Entity<InspectionItem>()
// .HasMany(i => i.InspectionItemCategory).WithMany().Map(m =>
//{
// m.MapLeftKey("InspectionItemId");
// m.MapRightKey("ViolationTypeId");
// m.ToTable("InspectionItemViolationCategories");
//});
////// Inspection Items can have multiple Categories
////modelBuilder.Entity<InspectionItemViolationCategory>().Property(j => j.InspectionItemViolationCategoryId).HasColumnName("InspectionItemViolationCategoryId")
//// .hasm;
modelBuilder.Entity<InspectionItemViolationCategory>()
.HasKey(s => s.InspectionItemViolationCategoryId);
//.HasMany<InspectionItem>(s => s.InspectionItem)
//.WithMany()
//.Map(cs =>
//{
// cs.MapLeftKey("InspectionItemId");
// cs.MapRightKey("ViolationTypeId");
// cs.ToTable("InspectionItemViolationCategories");
//});
//-- NOV can be issued by multiple users
modelBuilder.Entity<NOV>().HasMany(n => n.IssuedBy).WithMany().Map(m =>
{
m.MapLeftKey("NOVId");
m.MapRightKey("UserId");
m.ToTable("NOVIssuedBy");
});
//-- Violation can be issued by multiple users
modelBuilder.Entity<Violation>().HasMany(n => n.IssuedBy).WithMany().Map(m =>
{
m.MapLeftKey("ViolationId");
m.MapRightKey("UserId");
m.ToTable("ViolationIssuedBy");
});
modelBuilder.Entity<Contact>()
.HasOptional(x => x.Parent)
.WithMany()
.HasForeignKey(x => x.ParentContactId);
modelBuilder.Entity<Contact>().HasMany(n => n.Children).WithMany().Map(m =>
{
m.MapLeftKey("ContactId");
m.MapRightKey("ParentContactId");
m.ToTable("Children");
});
//modelBuilder.Entity<ViolationNOV>()
// .HasKey(c => new { c.ViolationNOVId });
//modelBuilder.Entity<CaseNOV>()
// .HasKey(c => new { c.CaseNOVId });
//modelBuilder.Entity<ViolationTypeNOV>()
// .HasKey(c => new { c.ViolationTypeNOVId });
//modelBuilder.Entity<NOV>()
// .HasMany(c => c.ViolationNOVs)
// .WithRequired()
// .HasForeignKey(c => c.NOVId);
//modelBuilder.Entity<NOV>()
// .HasMany(c => c.CaseNOVs)
// .WithRequired()
//.HasForeignKey(c => c.NOVId);
//modelBuilder.Entity<NOV>()
// .HasMany(c => c.ViolationTypeNOVs)
// .WithRequired()
//.HasForeignKey(c => c.NOVId);
////modelBuilder.Entity<Violation>()
//// .HasMany(c => c.ViolationNOVs)
//// .WithRequired()
////.HasForeignKey(c => c.ViolationId);
////modelBuilder.Entity<ViolationType>()
//// .HasMany(c => c.ViolationTypeNOVs)
//// .WithRequired()
////.HasForeignKey(c => c.ViolationTypeId);
////modelBuilder.Entity<Case>()
//// .HasMany(c => c.CaseNOVs)
//// .WithRequired()
//// .HasForeignKey(c => c.CaseId);
//-- Inspection Request can be assigned to multiple users
base.OnModelCreating(modelBuilder);
}
#endregion Relations
}