ApplicationDbContext.cs
public class ApplicationDbContext : DbContext
{
public ApplicationDbContext(DbContextOptions<ApplicationDbContext> options) : base(options)
{
}
public DbSet<BookReadingEvent> BookReadingEvents { get; set; }
public DbSet<Register> RegisterAccount { get; set; }
public DbSet<Login> LoginAccount { get; set; }
}
Register model class:
namespace BookReadingEvents.Models
{
[Index(nameof(Email), IsUnique = true)]
public class Register
{
[Key]
public int RegisterId { get; set; }
[Required]
[DataType(DataType.EmailAddress, ErrorMessage = "Please enter a valid e-mail address")]
public string Email { get; set; }
[Required]
[Display(Name = "Full Name")]
public string Name { get; set; }
[Required]
[DataType(DataType.Password)]
[MinLength(5, ErrorMessage = "Password length cannot be less than 5")]
public string Password { get; set; }
[NotMapped] // Does not affect the database
[Compare("Password")]
[Required]
[DataType(DataType.Password)]
[Display(Name = "Confirm Password")]
public string ConfirmPassword { get; set; }
}
}
Login model class:
public class Login
{
[Key]
public int LoginId { get; set; }
[Required]
[DataType(DataType.EmailAddress, ErrorMessage = "Please enter a valid e-mail address")]
public string Email { get; set; }
[Required]
[DataType(DataType.Password)]
[MinLength(5, ErrorMessage = "Password length cannot be less than 5")]
public string Password { get; set; }
public DateTime EntryDate { get; set; } = DateTime.Now;
[ForeignKey("Registers")]
public int RegisterId { get; set; }
public virtual Register Registers { get; set; }
// Here foreign key is working fine
}
Book Reading Event model class:
public class BookReadingEvent
{
[Key]
public int Id { get; set; }
[Required]
public string Title { get; set; }
[Required]
[DataType(DataType.Date)]
public DateTime Date { get; set; }
[Required]
public string Location { get; set; }
public string StartTime { get; set; }
public string EventType { get; set; }
[Range(1,9999)]
public int Duration { get; set; }
[StringLength(50, ErrorMessage = "Description length can't be more than 50.")]
public string Description { get; set; }
[StringLength(500, ErrorMessage = "Other Details length can't be more than 500.")]
public string Details { get; set; }
public string InviteByEmail { get; set; }
[ForeignKey("Registers")]
public int RegisterId { get; set; }
public virtual Register Registers { get; set; }
// Error: The ALTER TABLE statement conflicted with the FOREIGN KEY constraint
// "FK_BookReadingEvents_RegisterAccount_RegisterId". The conflict occurred in database
// "BookReadingEvents", table "dbo.RegisterAccount", column 'RegisterId'.
}
I want to add foreign key RegisterId
to the BookReadingEventModel
as well, but I get an error while it is working fine with the Login
model. How do I add foreign key to the Book Reading Event table with code-first approach?
CodePudding user response:
This error is caused if you have data in your database conflicting your change. It is easily tested by creating a completely new migration and running it on a new empty database.
You most likely have old test data in your database which can't have a reference.
CodePudding user response:
you have required RegisterId in several tables and this causes an error. Try to make RegisterId nullable in both classes
public int? RegisterId { get; set; }
[ForeignKey("RegisterId")]
public virtual Register Register { get; set; }
and add navigaion properties to Register class too
public class Register
{
[Key]
public int RegisterId { get; set; }
....
[InverseProperty(nameof(Login.Register))]
public virtual ICollection<Register> LoginRegisters {get; set;}
[InverseProperty(nameof(BookReadingEvent.Register))]
public virtual ICollection<Register> BookReadingEventRegisters {get; set;}
}